Load/Bearing
Fundamentals · 08 of 22

Message queues and backpressure

Accept work now, do it at a pace you can sustain, and make sure you eventually catch up.

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.

APIvalidate + enqueueQUEUEdurable bufferWORKER 1WORKER 2WORKER 3DB600 writes/s
The queue absorbs bursts. Workers drain it at a steady rate the database can take.

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:

backlog growth during a burst = (λ − μ) × burst duration time to drain afterwards = backlog ÷ (μ − λ_after)

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.

backlog after the sale = (945 − 500) × 8 s ≈ 3,560 orders drain time afterwards = 3,560 ÷ (500 − 315) ≈ 19 s

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 queueLog / stream
ExamplesRabbitMQ, Amazon SQSApache Kafka, Kinesis, Redpanda
After it's consumedThe message is deletedThe message stays for a retention period
ConsumersCompete for messagesEach group reads the whole log at its own pace
OrderingBest effort or FIFO queuesOrdered within a partition
Use forTask distribution, jobsEvent streams, fan-out, replay

Delivery guarantees

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.

PRODUCERkey = order_idPARTITION 0PARTITION 1PARTITION 2PARTITION 3CONSUMER AP0CONSUMER BP1CONSUMER CP2CONSUMER DP3CONSUMER Eidle
One consumer group with four partitions and five consumers. Each partition goes to exactly one consumer, so the fifth gets nothing.

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 getWhen
At most onceThe 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 KafkaIdempotent 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

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

Practise it

Level 9: Retry Storm

Put this into practice against live traffic. Your progress in the game is saved in your browser.

Play level 9
Workshop drill

Do it by hand

Interactive drills for the parts the simulator can't show. Scored, with your best result saved in your browser.

Delivery guarantees

Further reading