Decouple work with message queues
Absorb bursts and move slow work off the request path.
- Explain what a queue adds between producers and consumers.
- Compare at-most-once, at-least-once, and exactly-once delivery.
- Make consumers idempotent so retries are safe.
A message queue (SQS, RabbitMQ) or log (Kafka) sits between services that produce work and services that consume it. The producer returns as soon as the message is stored, so slow work - sending emails, transcoding video, updating feeds - happens asynchronously. Queues also level load: a burst piles up in the queue instead of overwhelming consumers, which drain it at their own pace.
Delivery guarantees
- At-most-once: a message may be lost but is never redelivered.
- At-least-once: a message is redelivered until acknowledged, so it may arrive more than once. This is the common default.
- Exactly-once: very hard end to end. In practice you get effectively-once by combining at-least-once delivery with idempotent consumers.
An operation is idempotent if applying it twice has the same effect as once. Consumers achieve this by recording processed message ids (or using an idempotency key) and skipping duplicates.
A worked example: deliveries arrive as pay-1 for $50, pay-2 for $20, then pay-1 again. A consumer that records processed ids skips the repeat, so the balance is $70, not $120.
Other queue patterns come up often. Work queues give each message to one consumer; pub/sub delivers each message to every subscriber. Kafka keeps order only within a partition, so you pick a partition key (such as user id) for events that must stay ordered. A dead-letter queue collects messages that keep failing so one bad message does not block the rest. Watch consumer lag: if producers outpace consumers for long, the backlog grows without bound.
Practice: load a queue
Try it
Up to 15 XPWhat can a queue absorb?
Interviewers often ask what happens when consumers fall behind.
- Turn on the burst. Does the queue recover?
- Turn it off, then set arrivals above the service rate.
- Add backpressure. What’s the trade-off?
Show the numbers as a table
| Time (ticks) | Messages waiting |
|---|---|
| 0 | 0 |
| 1 | 0 |
| 2 | 0 |
| 3 | 0 |
| 4 | 0 |
| 5 | 0 |
| 6 | 0 |
| 7 | 0 |
| 8 | 0 |
| 9 | 0 |
| 10 | 0 |
| 11 | 0 |
| 12 | 0 |
| 13 | 0 |
| 14 | 0 |
| 15 | 0 |
| 16 | 0 |
| 17 | 0 |
| 18 | 0 |
| 19 | 0 |
| 20 | 0 |
| 21 | 0 |
| 22 | 0 |
| 23 | 0 |
| 24 | 0 |
| 25 | 0 |
| 26 | 0 |
| 27 | 0 |
| 28 | 0 |
| 29 | 0 |
| 30 | 0 |
| 31 | 0 |
| 32 | 0 |
| 33 | 0 |
| 34 | 0 |
| 35 | 0 |
| 36 | 0 |
| 37 | 0 |
| 38 | 0 |
| 39 | 0 |
| 40 | 0 |
| 41 | 0 |
| 42 | 0 |
| 43 | 0 |
| 44 | 0 |
| 45 | 0 |
| 46 | 0 |
| 47 | 0 |
| 48 | 0 |
| 49 | 0 |
| 50 | 0 |
| 51 | 0 |
| 52 | 0 |
| 53 | 0 |
| 54 | 0 |
| 55 | 0 |
| 56 | 0 |
| 57 | 0 |
| 58 | 0 |
| 59 | 0 |
- Largest backlog
- 0
- Longest wait
- 0 ticks
- Rejected
- 0
Workers keep up, so the queue stays empty.
Try it
Up to 15 XPOn the request path or in a queue?
Does the user have to wait for this? If not, it’s a candidate for a queue.
“Checking the password at login.”
“Sending a welcome email.”
“Transcoding an uploaded video.”
“Checking stock before confirming an order.”
“Updating followers’ feeds after a post.”
Key takeaways
Queues decouple services, level bursts, and move slow work off the request path.
Assume at-least-once delivery and design idempotent consumers.
Plan for ordering (partition keys), poison messages (DLQs), and lag.
Lesson quiz
6 questions · pass with 5 correct · up to 50 XP
Passing this quiz completes the lesson and keeps your streak going. Questions you miss come back in review sessions later.
Questions about this lesson
Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.
Loading posts…