Module 5 · Distributed systemsLesson 53 of 887 minAdvanced
At-most-once, at-least-once, exactly-once
Messaging systems can promise to never duplicate, or to never lose. True "exactly once" comes from never losing plus making duplicates harmless.
Play each tab. Watch where the message is lost, where it's duplicated, and how deduplication fixes the duplicate.
When one system sends a message to another (a queue to a worker, a service to a service), failures can strike at any point: before processing, during, or after processing but before the "done" (ack) gets back. Each delivery guarantee picks which failure you'd rather have.
At most once
The sender sends once and moves on (or the queue deletes the message as soon as it's handed out).
- Never duplicated.
- Can be lost if the consumer crashes or the message is dropped.
- Fine for: metrics, logs, live location updates where the next one replaces it.
At least once
The message stays until the consumer acknowledges it. No ack in time? Deliver it again.
- Never lost (as long as the queue is durable).
- Can be duplicated: if processing succeeds but the ack is lost, it's processed again.
- The default for SQS, RabbitMQ with acks, and Kafka consumers committing after processing.
Exactly once
Truly delivering a message exactly once over an unreliable network isn't possible in general. What systems offer is exactly-once effect:
- at-least-once delivery, plus
- idempotent processing or deduplication by message id, or
- a transaction that stores the result and the "processed" marker together.
Kafka's exactly-once feature works this way inside Kafka (producer ids and transactions). Once the effect leaves the system (sending an email, charging a card), you're back to idempotency keys.
Ordering
A related promise: are messages processed in the order sent? Usually only within a partition or a message group. If order matters per user, route all of a user's messages to the same partition.