Loading
0x80Lesson 9 of 13

Decouple work with message queues

Absorb bursts and move slow work off the request path.

20 min 6-question quiz
By the end of this lesson you can
  • 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 XP

What can a queue absorb?

Interviewers often ask what happens when consumers fall behind.

  1. Turn on the burst. Does the queue recover?
  2. Turn it off, then set arrivals above the service rate.
  3. Add backpressure. What’s the trade-off?
00.30.50.81061218243036424854Time (ticks)Waiting
Show the numbers as a table
Time (ticks)Messages waiting
00
10
20
30
40
50
60
70
80
90
100
110
120
130
140
150
160
170
180
190
200
210
220
230
240
250
260
270
280
290
300
310
320
330
340
350
360
370
380
390
400
410
420
430
440
450
460
470
480
490
500
510
520
530
540
550
560
570
580
590
Largest backlog
0
Longest wait
0 ticks
Rejected
0

Workers keep up, so the queue stays empty.

Try it

Up to 15 XP

On the request path or in a queue?

Does the user have to wait for this? If not, it’s a candidate for a queue.

0 of 5 sortedScore 0/0
  • “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…