In a synchronous design, the user waits while every step runs: validate, charge, write, email. If the slowest step can handle 600 operations a second and 1,000 people click "Buy" at once, 400 of them get errors.
A message queue splits accepting work from doing it. The API checks the request, puts a message on the queue, and immediately answers "order received". Workers pull messages off the queue and do the slow part at whatever rate the downstream system can handle.
Load levelling: the math
Let λ be the arrival rate and μ the rate your workers can process. During a burst where λ > μ, the backlog grows. Afterwards it shrinks:
The rule that matters: μ must exceed the average arrival rate, not the peak. If it doesn't, the backlog grows forever and "processed a bit later" turns into "processed tomorrow".
Flash sale, worked
Writes arrive at 315/s normally and 945/s during an 8-second sale. Two workers process 500/s together.
No errors, and the database never sees more than 500 writes per second. With one worker (250/s), the drain rate is below even the normal 315/s arrival rate, so the backlog never clears.
Queues vs logs
| Message queue | Log / stream | |
|---|---|---|
| Examples | RabbitMQ, Amazon SQS | Apache Kafka, Kinesis, Redpanda |
| After it's consumed | The message is deleted | The message stays for a retention period |
| Consumers | Compete for messages | Each group reads the whole log at its own pace |
| Ordering | Best effort or FIFO queues | Ordered within a partition |
| Use for | Task distribution, jobs | Event streams, fan-out, replay |
Delivery guarantees
- At most once: fire and forget. Messages can be lost.
- At least once: the usual default. A worker that crashes before acknowledging gets the message redelivered, so duplicates happen.
- Exactly once: in practice, at-least-once delivery plus idempotent processing. Give each message an id (for example an idempotency key from the client), and have the worker skip ids it has already processed, using a unique constraint in the same transaction as the write.
Kafka specifically
A Kafka topic is split into partitions. Each partition is an ordered, append-only log. The message key decides which partition a message lands in, so all messages with the same key stay in order.
Consumer groups set your parallelism
Consumers that share a group.id split the partitions between them, and each partition is read by exactly one consumer in the group. So the partition count is the maximum parallelism for a group: with 12 partitions, a 13th consumer gets nothing to do. Separate groups each read the whole topic independently, which is how one event feeds billing, search and analytics at the same time.
Choose the partition count with headroom. Adding partitions later changes which partition each key maps to, which breaks per-key ordering for messages in flight. When consumers join or leave, the group rebalances and briefly pauses consumption.
What Kafka actually guarantees
| You get | When |
|---|---|
| At most once | The consumer commits its offset before processing. A crash after the commit skips the message. |
| At least once (the usual default) | The consumer commits after processing. A crash before the commit means the message is processed again. |
| Exactly once, inside Kafka | Idempotent producers (on by default since Kafka 3.0) drop duplicates caused by retries, and transactions commit the output messages and the consumer offsets together. This covers reading from Kafka, processing, and writing back to Kafka. |
Exactly-once stops at Kafka's edge. If your consumer charges a card or writes to Postgres, a crash between that side effect and the offset commit still repeats it. You still need an idempotency key in the external system.
When things go wrong
- Retries with exponential backoff and jitter: wait 1 s, 2 s, 4 s and so on, with randomness so retries don't arrive in synchronised waves.
- Dead-letter queue: after N failed attempts, or on an error that can never succeed (such as a malformed payload), move the message aside so it can't block the queue. Someone has to own draining it: alert when it isn't empty, fix the cause, then replay the messages.
- Visibility timeout: a message being processed is hidden from other workers until it's acknowledged or the timeout expires.
- Backpressure: when consumers fall behind, you can add consumers (up to the partition count), make each message cheaper (for example by batching database writes), slow producers down, reject new work (HTTP 429 or 503), or shed low-priority work. Buffering without limit only delays the failure.
- Outbox pattern: write the business row and an "outbox" row in one database transaction, then publish from the outbox. This avoids "saved but never published" and "published but never saved".
The product cost
Asynchronous work changes what users see. The UI shows "processing", emails arrive a few seconds late, and you need a way to tell the user if something failed after you said "received". That's usually a good trade, but it's a product decision, not only an engineering one.
In the game: with a queue, writes are acknowledged in about 4 ms whatever the database load. Workers drain 250 writes/s each, capped by database write capacity. The level checks that the queue is less than 10 seconds behind at the end, which fails if workers can't beat the average arrival rate. Retry Storm adds a flaky card processor and poison messages: you need backoff, idempotency keys and a dead-letter queue together. Partition Ceiling shows workers sitting idle once they outnumber the partitions.
Interview checklist
- Say which work becomes asynchronous and what the user sees meanwhile.
- Size workers above the average arrival rate, and the queue for the burst.
- State the delivery guarantee and how processing stays idempotent.
- Pick a partition key for ordering and enough partitions for parallelism.
- Cover retries, the dead-letter queue and backpressure.
- Choose a queue or a log and explain why.
Level 9: Retry Storm
Put this into practice against live traffic. Your progress in the game is saved in your browser.
Play level 9Do it by hand
Interactive drills for the parts the simulator can't show. Scored, with your best result saved in your browser.
Further reading
- Martin Kleppmann, Designing Data-Intensive Applications (O'Reilly). The standard book on replication, partitioning and consistency.
- Site Reliability Engineering, Google's free book on running reliable systems.
- The Amazon Builders' Library: practical articles on timeouts, retries, caching and load shedding.
- The System Design Primer: a broad, community-maintained overview.