Patterns
Event-driven work queues
Move slow work out of the request path and onto workers. The pattern is one box on a diagram; the interview is about what happens when a worker dies halfway, a job fails forever, or the queue grows faster than you drain it.
A user uploads a video. Transcoding takes four minutes. You do not hold an HTTP request open for four
minutes — you accept the upload, return 202 Accepted, put a job on a queue, and let a pool of
workers do it. The same shape covers sending emails, generating PDFs, resizing images, calling slow
third-party APIs, recomputing recommendations and fanning out notifications.
Almost every design question has one of these, and almost every candidate draws it the same way: API → queue → workers. This lesson is about the parts the diagram hides.
When work belongs on a queue
Move work to a queue when any of these is true:
- It is slow — seconds or more — and the user doesn’t need the result to continue.
- It is bursty and the downstream can’t absorb peaks: the queue is a shock absorber, and workers drain at the rate the downstream can take.
- It talks to something unreliable, so it needs retries with backoff that a request can’t wait for.
- It fans out: one event (an order placed) triggers several independent reactions (email, analytics, inventory, fraud check).
Keep it in the request when the user needs the result to proceed — a payment authorisation, a stock reservation at checkout. Putting those on a queue just moves the wait into polling.
A queue decouples when work happens from when it was requested. That buys resilience and smoothing, and costs you the thing a synchronous call gave for free: an immediate, definite answer. Every queue design has to say how the user eventually learns the outcome — polling a status endpoint, a webhook, a push notification, or an email.
The two families of queue
| Message queue | Log | |
|---|---|---|
| Examples | SQS, RabbitMQ, Redis lists | Kafka, Kinesis, Redis Streams |
| A message is… | A task; deleted when acknowledged | An entry in an append-only log; kept for the retention period |
| Consumers | Compete: each message goes to one worker | Each group has its own cursor; groups read independently |
| Per-message ack | Yes, with a visibility timeout | No — a committed offset per partition |
| Ordering | None (SQS standard) or per group (SQS FIFO) | Per partition |
| Replay | No | Yes, by moving the cursor |
For jobs — independent tasks, any worker can take any one, failures retried individually — a message queue is the natural fit. For events that several consumers react to, or that you may need to replay, a log is. The Kafka lesson covers why a log is “a cursor, not a queue”.
The shape
API ──enqueue──▶ queue ──▶ worker pool ──▶ downstream (S3, email provider, DB)
│ │ │
└─ 202 + job id └─ DLQ ◀─────┘ (after N failures)
What makes this production-grade is everything in the members’ section: the delivery guarantees and how workers die safely, retries and the dead-letter queue, the enqueue-with-the-database problem, priorities and fairness, backpressure, and how to size the worker pool.
The rest of this lesson is ₹1
Visibility timeouts, safe retries and DLQs, the transactional outbox, priorities and fairness, backpressure, sizing the worker pool, and the interview follow-ups.
PhonePe, Google Pay, Paytm or any UPI app. Cards and net banking work too. One payment unlocks this lesson on this device.