Queues & Pub/Sub
Before this, it helps to know: API Design Basics.
Read a little, play a little. No scary maths, and no rush.
Dinner rush, engineered
Friday night: 500 orders hit a food app in ten minutes. The restaurant can't cook 500 meals at once — and it shouldn't have to receive them all at once either. Orders land in a queue; the kitchen pulls them at its own pace. If the kitchen closes mid-order, nothing is lost — the tickets are still on the spike. That spike of tickets is a message queue, and nearly every large system has one somewhere.
What a queue buys you
- Decoupling: the order service doesn't know or care how cooking works — it just drops a message. Either side can deploy, scale, or crash independently.
- Buffering: traffic spikes flatten into a steady lane the consumer can handle.
- Retry for free: if cooking fails, the message goes back in line instead of vanishing.
Tools of the trade: SQS or RabbitMQ for simple queues, Kafka when you need a replayable, high-throughput log (see our Kafka intro).
At-least-once means "maybe twice"
Most queues guarantee at-least-once delivery: your handler will see each message, possibly twice. So handlers must be idempotent — processing the same "charge $20" message twice must not charge $40. Design every consumer as if duplicates are normal, because they are.
Pub/sub: one event, many fans
A queue is one message, one worker. Pub/sub is one event, many subscribers: order_placed fans out to notifications ("your food is coming!"), analytics ("Friday peak detected"), and the restaurant dashboard — all independent, all from the same event. Same plumbing, different shape.
Poison and the dead-letter queue
Some messages are cursed — malformed, referencing deleted data — and will fail every retry forever, blocking everyone behind them. After N failures (say 5), route them to a dead-letter queue (DLQ) for humans to inspect, and let the line move on.
A worked example: one order, many hands
- The checkout service saves the order and publishes
order_placed. - A payment worker takes the message from its queue and charges the card, using the order ID as an idempotency key.
- The kitchen queue receives the order once payment succeeds.
- Notification and analytics subscribers react to the same events independently; if analytics is down for an hour, it catches up later and nobody waits for it.
While a worker processes a message, the queue hides it from other workers for a set time, often called the visibility timeout. If the worker crashes before confirming, the message reappears and another worker picks it up. Set that timeout longer than the normal processing time, or slow jobs will be handed out twice.
What to watch
Two numbers tell you a queue's health. Depth is how many messages are waiting. Age of the oldest message is how long the worst one has waited. Steady depth is fine; depth that keeps growing means consumers cannot keep up, so add workers or find the slow step.
Ordering is not free
Many queues do not guarantee messages arrive in the order they were sent, especially with several workers pulling at once. If order matters, for example "created" must be handled before "canceled," use a queue that offers ordering per key, or design consumers to cope with events arriving out of order.
Remember this
- Queue = decouple + buffer + retry. Pub/sub = one event, many independent fans.
- Idempotent consumers survive at-least-once delivery; DLQs quarantine poison messages.
Check your understanding
5 questions · first-try answers earn the most XP
My notes
Saved in this browser. Highlight a line above and save it, or write it in your own words.
Nothing saved yet. Your highlights will live here.
References
Finished reading?
Ticking it here also ticks the chapter in the sidebar, the section count and your streak — it is all one number.
Practice this in an incident
Try a design drill
Related chapters
Spotted a mistake or want a topic covered? Report an issue