SDSystem DesignConcepts to case studies
System DesignDSA
x
0/311
Part 1: Foundations
Part 2: APIs and Communication
  • Message Queues
  • Pub/Sub
  • Message Brokers
  • Delivery Semantics
  • Dead Letter Queues
  • Kafka Deep Dive
  • Retries with Backoff
  • RabbitMQ and SQS
  • Event Streaming vs Message Queues
  • Message Ordering
  • Task Queues, Workers and Scheduled Jobs
Part 3: Caching
Part 4: Data and Storage
Part 5: Architecture
Part 6: Distributed Systems
Part 7: Advanced Data Topics
Part 8: Operations and Security
Part 9: Estimation and Interview Method
Part 10: Case Studies
System Design/APIs and Communication/Asynchronous Communication

Delivery Semantics

At most once, at least once or exactly once: what a message promise really means.

⏱ 14 min readAdvancedInterview FavouriteHas visualizer
SummaryConceptReal-Life ExampleWhy?Where?How It WorksStepsKey TermsPros & ConsMistakesReal WorldRead NextInterview Q&ACheat-Sheet

01Quick Summary

Delivery semantics tell you how many times a message will be delivered when things go wrong. There are three promises:

  • At most once: 0 or 1 time. It may be lost, but never repeated.
  • At least once: 1 or more times. Never lost, but may be repeated.
  • Exactly once: the effect happens one time. In most systems this means at-least-once delivery plus a consumer that ignores duplicates.

02Concept: What is it?

Networks drop packets and machines crash. So after you send a message, you may not know if it arrived. Did the message get lost, or did only the reply get lost? The delivery promise decides what the system does next.

The problem: no reply
📤Producer
→↓message
📥Consumermaybe did the work
←ack ❓
❌Case 1message was lost
✅Case 2done, but the ack was lost
No ack arrived. The producer cannot tell which of these happened.

The three promises

🔥
At most once
Send once, do not retry.
+ Fast, no duplicates
− Can lose messages
🔁
At least once
Retry until an ack arrives.
+ Nothing lost
− Duplicates possible
🎯
Exactly once
At least once + ignore duplicates.
+ One effect
− More work and cost

03Real-Life Example

🍵Sending a parcel

At most once: you throw a letter in a mailbox. If it is lost, you never know and you do not send another.

At least once: you send the parcel and wait for a signed receipt. No receipt in a week? You send another. Your friend may now get two parcels.

Exactly once: same as above, but each parcel has a number. Your friend’s home keeps a list and refuses parcel #42 the second time.

Parcel
Messaging
Throw in a mailbox
Fire and forget (at most once)
Signed receipt
Ack
Sending again after a week
Retry (at least once)
Two parcels arrive
Duplicate message
Parcel number checked against a list
Idempotent consumer / dedupe by id
⚠️Why duplicates hurt
A payment message processed twice charges the customer twice. A “send email” message processed twice sends two emails. This is why you must plan for at-least-once.

04Why Do We Need It?

💸Money and data
Lost or doubled payments are serious bugs.
🧭Design choice
Different features need different promises.
⚖️Cost vs safety
Stronger promises cost more speed and effort.
🧪Be honest about failure
Networks fail. A system must say what happens then.
You cannot get “never lost” and “never repeated” for free. Pick at-least-once and make the consumer safe to repeat.

05Where Do We Use It?

Use caseUsually pickWhy
Metrics, logs, “likes” countAt most onceLosing one sample is fine. Speed matters.
Emails, notifications, background jobsAt least onceBetter a rare duplicate than a lost email. Often made idempotent.
Payments, orders, inventoryAt least once + idempotent consumerMust happen once, and must not be lost.
Stream processing with transactions“Exactly once” featuresSome systems offer it inside their own boundary.

06Types / Parts / How It Works

How each promise works

  1. 0-1
    At most once
    Send. Do not wait for an ack. Do not retry. Or: the consumer acks first and then works, so a crash loses the job.
  2. 1+
    At least once
    Send. Wait for an ack. No ack in time? Send again. The consumer acks AFTER the work, so a crash means the job is repeated.
  3. 1
    Exactly once (effect)
    At least once + the consumer remembers message ids (or uses an idempotent write) and skips repeats.

The ack order matters

Ack before work
At most once
  • Consumer says “got it”, then starts
  • A crash during the work loses the message
  • No duplicates
Ack after work
At least once
  • Consumer works, then says “done”
  • A crash before the ack means a retry
  • Duplicates possible

Making a consumer idempotent

Dedupe by message id
📥Message 421st copy
→↓
🗒️Seen ids?no → do work, save 42
→↓then
📥Message 422nd copy
→↓
🗒️Seen ids?yes → skip
The second copy of message 42 arrives. The consumer finds 42 in its list and skips it.
🧠Remember
“Exactly once” usually does not mean the network sends it once. It means the result happens once. The message may still arrive more than once.
VisualizerA flaky network and three promisesRaise the loss slider and turn ack loss on. Compare the three promises, then click a message to read its story.
Delivery promise
Can the ack be lost too?
📤Producerretries up to 5 times
↔30% loss
📥Consumerdoes the work
Press “Send 12 messages”.
Lost
–
never processed
Done once
–
Done twice+
–
duplicate work
Network sends
–
for 12 messages
Keep sending until an ack arrives. Nothing is lost, but the consumer can get the same message twice.

07How to Use It / Steps / Implementation

  1. 1
    Choose at-least-once by default
    For anything important, retry until acked.
  2. 2
    Give every message a unique id
    A UUID or an id built from the business data (order id + step).
  3. 3
    Make the consumer idempotent
    Store processed ids in a table with a unique key, or use an upsert, or a conditional update.
  4. 4
    Do the work and save the id in one transaction
    Then a crash cannot leave work done but the id missing.
  5. 5
    Ack after success
    Never ack before the work is saved.
  6. 6
    Retry with backoff
    Wait longer after each failure (1s, 2s, 4s…) and stop after a limit.
  7. 7
    Send failures to a dead letter queue
    So repeated failures do not block good messages.

Idempotent writes: easy examples

Not idempotentIdempotent version
balance = balance + 100Insert payment id 42 once (unique key), then add 100
Send a new email each timeSend only if “email for order 17” is not marked sent
INSERT a rowUpsert (insert or update) by a unique key
💡Related: API idempotency keys
Payment APIs often take an idempotency key from the client. A repeated request with the same key returns the first result. The idea is the same.

08Key Terms to Know

TermMeaning in easy words
AckA reply saying the message was received or handled
RetrySending again after no ack or a failure
At most onceDelivered zero or one time
At least onceDelivered one or more times
Exactly onceThe effect happens one time (usually at-least-once plus dedupe)
IdempotentRepeating it gives the same result as doing it once
DeduplicationSpotting and dropping repeated messages
Message idA unique label for a message
BackoffWaiting longer after each retry
TransactionA group of steps that all happen or none happen

09Pros, Cons and Trade-offs

✅ Pros
  • At most once is fast and simple
  • At least once never loses messages
  • Idempotent consumers make retries safe
  • Clear promises make failures predictable
⚠️ Cons
  • At most once can lose data
  • At least once creates duplicates
  • Exactly once needs extra storage and care
  • Retries add load and delay
  • Hard to test failure cases
PromiseWe gainWe lose
At most onceSpeed, no duplicatesMay lose messages
At least onceNo lossMay get duplicates, need idempotency
Exactly once (effect)Safe resultsExtra work, state and latency

10Common Problems and Mistakes

Common mistakes

  1. Believing “exactly once” means no duplicates on the network. Read what the product really promises and where.
  2. Acking before the work is saved. A crash then loses the job.
  3. Saving the id after the work, in a separate step. A crash between them repeats the work.
  4. Non-idempotent updates like “add 100 to the balance”.
  5. Retrying forever. A bad message loops. Use a retry limit and a dead letter queue.
  6. Dedupe list that grows forever. Keep ids for a limited time that is longer than the longest retry window.
  7. Retries without jitter. Many clients retry at the same moment and overload the service.
👯Duplicates
Retry after a lost ack.
🕳️Lost messages
Fire and forget, or ack before work.
🔀Out of order
A retry arrives after a newer message.

11Real-World Examples

☁️Amazon SQS
Standard queues are described as at-least-once, so duplicates are possible. FIFO queues add deduplication features.
🧵Kafka
Commonly used at-least-once. It also offers idempotent producers and transactions for stronger guarantees in a defined scope.
💳Payment APIs
Use idempotency keys so a retried request does not charge twice.
📈Metrics pipelines
Often accept at-most-once, since one lost sample is fine.
📝Note
General descriptions. Check the official docs of a product for the exact guarantees.

12Related Topics to Read Next

Idempotency→

The key tool for safe retries.

Message Queues→

Where delivery promises show up.

Message Brokers→

Brokers decide which promises they can keep.

Dead Letter Queues→

Handle messages that keep failing.

CAP Theorem→

Another place where failure forces a trade-off.

Reliability→

Delivery promises are part of a reliable system.

13Interview Questions with Short Answers

14Quick Revision Cheat-Sheet

  • ✔At most once: maybe lost, never repeated. Fast.
  • ✔At least once: never lost, maybe repeated. Retry until ack.
  • ✔Exactly once: one effect = at-least-once + dedupe / idempotence.
  • ✔Ack after work for safety; ack before work risks loss.
  • ✔Every message needs a unique id.
  • ✔Make consumers idempotent: unique keys, upserts, save the id with the work.
  • ✔Retries: exponential backoff + jitter + a limit, then a dead letter queue.
  • ✔One line: “Assume messages can repeat, and make repeating them harmless.”
← Previous
Message Brokers
Next →
Dead Letter Queues