22. Message Queues
1
The picture to keep in your head2
22.1 What are producers and consumers?3
22.2 Why go async?4
22.3 What are the delivery guarantees?5
22.4 How do acks and visibility timeouts work?6
22.5 What are retries and dead-letter queues?7
22.6 What is an idempotent consumer?8
22.7 How does ordering work?9
22.8 What is backpressure?10
22.9 How do you scale consumers?11
22.10 Log (Kafka) vs queue (RabbitMQ, SQS): what's the difference?12
22.11 How do delayed and scheduled jobs work?13
Memory card and where the analogy breaks14
Practice: five levels15
Interview questions16
Solutions17
Sources18
Code lab22. Message Queues
Practice: five levels
3 min read+10 XP
Assignments
Five levels. Do them in order. Answers are in "Solutions" at the end, so try first.
Level 1: Think (no code)
A. Sync or async? One line each.
- Charging the customer's card during checkout.
- Sending the order confirmation email.
- Updating the search index after a product's price changes.
- Checking stock before accepting an order.
- Generating a monthly sales report.
B. Name the problem, or the fix.
- A worker acks, then crashes before sending the SMS.
- A customer gets two "order confirmed" SMS for one order.
- One message with a malformed address is retried forever, and messages behind it wait.
- "Shipped" is processed before "paid" for the same order.
- A Kafka topic has 6 partitions; adding a 7th consumer to the group doesn't speed anything up.
Level 2: Calculate
- With the figure's timings (save 60 ms, SMS 800 ms, slip 600 ms, points 300 ms), how long does checkout take synchronously? Asynchronously?
- 1,200 messages a second arrive for 1 hour; workers handle 1,000 a second. How big is the backlog at the end? If arrivals then drop to 400 a second, how long to drain?
- Messages arrive at 1,000 a second and each takes 200 ms. How many concurrent workers? With 50% headroom?
- A message is retried with waits of 2, 4, 8, 16, 32 seconds before going to the DLQ. How long from the first failure to the DLQ (ignoring processing time)?
- A Kafka topic has 12 partitions. What's the most consumers one group can use? Two different services each want every message: how many consumer groups?
Level 3: Build
In Python:
- A
Queuewithsend,receive(now),ack, a visibility timeout and a dead-letter queue aftermax_receives. - A
deliver(mode, crash_points)that shows at-most-once losing messages and at-least-once repeating them. - A loyalty-points consumer with
award_naiveandaward_idempotent, and a test that redelivers a message. - A
Logwith per-group offsets,poll(group)andseek(group, offset). - A
Schedulerwithschedule(run_at, job)anddue(now).
Level 4: Debug
During the Big Sale, some customers got double loyalty points. Logs show the points worker sometimes takes 45 seconds per message (the points service is slow under load). The setup:
python
- Explain exactly how one message produces two point awards.
- Fix it in two ways: one about the timeout, one that makes duplicates harmless whatever the cause.
Level 5: Design
Design the background job system for the grocery app's checkout: after each order, send an SMS, print a packing slip at the right store, award loyalty points, and update analytics. Normal load is 200 orders a second; Big Sale peaks hit 1,200 for an hour. The SMS gateway allows 500 messages a second.
Deliver:
- The queues or topics, and which consumers read which.
- Delivery guarantee and idempotency for each consumer.
- What happens during the Big Sale, given the SMS gateway limit.
- Ordering needs (if any), and how you meet them.
- Failure handling: retries, DLQs, alerts and redrive.