Message Queues & Event Streaming
Decouple producers from consumers, absorb traffic spikes, and guarantee reliable delivery.
In plain words
A message queue is a to-do list between services: one service drops a task in, another picks it up when it’s ready. Neither has to wait for the other.
Think of it like…
A traditional queue is a customer service ticket machine (one agent handles each ticket and throws it away); Kafka is a shared DVR log that multiple teams can watch and rewind independently.
Where you’ve seen it
When you place an order, the shop puts "send confirmation email" on a queue. The email service handles it a moment later, so checkout stays fast.
Words you’ll hear
- Producer / consumer
- The producer sends messages; the consumer processes them.
- Queue vs log
- A queue removes messages once handled; a log (Kafka) keeps them for replay.
- At-least-once
- A message may be delivered more than once, so consumers must handle duplicates.
- Dead-letter queue
- Where messages go after failing too many times.
Visual diagram
Theory & deep dive
Why async decoupling protects user latency
When a user uploads a video or completes checkout, they want an instant "Success!" response in 100 ms - they shouldn’t wait 30 seconds for video transcoding, push notifications, and invoice PDFs. Dropping an event onto a queue lets your API return fast while workers process smoothly at their own pace.
Queues also absorb traffic spikes ("load leveling"): if 50,000 events arrive in 1 second, Kafka/SQS buffers them safely so downstream databases never crash.
How it works, step by step
Step 1 of 41. Buffer bursty writes asynchronously
The API validates the request, appends an event/job to the broker, and returns 202 Accepted immediately without waiting for slow workers.
- API Latency
- <10 ms
- Spike Cushion
- Queue depth absorbs
When to use it & trade-offs
What it is
Message queues (SQS, RabbitMQ) distribute discrete tasks across competing workers, while event streaming logs (Kafka, Pulsar) persist ordered, replayable partitions of events for multiple independent consumers.
Why it matters: Synchronous calls fail under spikes or slow downstream work (emails, video encoding, fan-out); queues smooth bursts and isolate failures.
When to reach for it
- Decoupling slow work (email, push notifications, billing PDFs, media transcoding)
- Streaming user activity/CDC events to search indexes, warehouses, and fraud engines
- Rate-smoothing bursty traffic before hitting fixed-capacity downstream services
When to avoid or be careful
- Making a user wait synchronously on an HTTP call that triggers 5 serial worker queues
- Running 20 Kafka consumers in one group on a topic with only 4 partitions (16 sit idle)
Work Queue vs. Event Log
Strengths: Per-message ack, built-in visibility timeouts, easy horizontal worker scaling.
Watch out: Messages are deleted once acked; no multi-consumer replay history.
Strengths: Retains ordered log for days/weeks; multiple consumer groups read independently & rewind.
Watch out: Scaling consumers beyond partition count is idle; requires partition-key planning.
Strengths: Preserves failing payloads for inspection and replay.
Watch out: Needs monitoring alerts so silent DLQ growth does not go unnoticed.
Interview signals & numbers to know
When this topic comes up
- async processing
- traffic spike smoothing
- fan-out to multiple services
- Kafka
- DLQ
Benchmarks & rules of thumb
- Kafka partition throughput
- ~10–50 MB/s per partition
- Consumer group rule
- Active consumers ≤ partitions
- Typical retention
- 7–30 days (Kafka)
- Max retries before DLQ
- 3–5 attempts
Common mistakes & follow-ups
Common interview mistakes
- Assuming Kafka guarantees global ordering across all partitions (it only orders within each partition)
- Acking a message before saving its result to the DB (loses data on worker crash)
- Skipping idempotency on consumers - at-least-once delivery WILL deliver duplicates during rebalances
Likely interviewer follow-ups
- What happens if a Kafka topic has 6 partitions and you scale the consumer group to 12 instances?
- How do you prevent a single corrupt “poison pill” message from stalling a Kafka partition?
- Explain how consumer offsets and consumer groups work in Kafka.
Knowledge check
Put it into practice
Design full architectures with AI feedback
Apply Message Queues & Event Streaming inside real High-Level Design prompts or review Q&A flashcards.