System design concepts · Doing Things Later

Message Queues and Delivery Guarantees: At-Least-Once vs Exactly-Once

A queue lets one part of the system say "please do this" without waiting for it to be done. That single idea absorbs traffic spikes, isolates failures and lets teams move independently.

What you'll be able to explain
  • Explain when to go async
  • Compare point-to-point queues and pub/sub
  • Reason about at-most-once, at-least-once and "exactly-once"
  • Use acks, visibility timeouts, retries and dead-letter queues
  • Handle ordering and backlog growth

Why asynchronous?

CLIENTUserSERVICECheckout APIDATABASEOrders DBQUEUE / LOGQueueWORKEREmailWORKERAnalyticsplace ordermust succeed nowOrderPlaced
Synchronous vs asynchronous: the user only waits for what they need.

Go async when the work is slow (video encoding), unreliable (third-party APIs), bursty (flash sales), fan-out (one event, many consumers), or simply not needed for the response.

Analogy

A restaurant ticket rail. The waiter clips the order and goes back to the customers; cooks pick tickets at their own pace. A rush of orders makes the rail longer, not the waiter slower.

Two messaging models

Point-to-point (work queue) Publish/subscribe
Who receives One consumer per message Every subscriber group gets a copy
Use for Distributing tasks across workers Broadcasting events to many services
Examples SQS, RabbitMQ queues, Celery SNS, Kafka topics, Google Pub/Sub, RabbitMQ fanout

Delivery guarantees

Guarantee Meaning How Risk
At-most-once Delivered 0 or 1 times Ack before processing Lost messages
At-least-once Delivered 1+ times Ack after processing; redeliver on timeout Duplicates
Exactly-once (effectively) Effect happens once At-least-once + idempotent consumer, or transactional processing Complexity
Say it in the interview

"Delivery is at-least-once, and every consumer is idempotent: it records processed message IDs in the same transaction as its side effect, so redeliveries are harmless. That gives us effectively-once processing without a distributed transaction."

The lifecycle of a message

ProducerQueueWorker AWorker B1send(msg 77)2deliver 77 (hidden for 30 s)3crashes, never acks430 s visibility timeout expires5redeliver 776ack 77 → deleted
At-least-once with a visibility timeout: an un-acked message reappears for another worker.

Key mechanisms:

  • Ack / nack: a message is deleted only after the consumer confirms success.
  • Visibility timeout / lease: a delivered message is hidden for a period; if not acked in time, it's redelivered. Set it longer than the worst-case processing time, or extend it while working.
  • Retries with backoff: transient failures retry after 1 s, 4 s, 16 s… with jitter.
  • Dead-letter queue (DLQ): after N failed attempts, park the message for inspection instead of retrying forever (a "poison message" can otherwise block a queue indefinitely).