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.
1processed = set()
2balance = 0
3deliveries = [("pay-1", 50), ("pay-2", 20), ("pay-1", 50)] # pay-1 was redelivered
4for message_id, amount in deliveries:
5 if message_id in processed:
6 continue
7 processed.add(message_id)
8 balance += amount
9print(balance)70
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.
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.
Practice: simulate system design building blocks
Use small Python programs to estimate capacity and simulate caches, load balancers, hash rings, and rate limiters. These exercises run locally in your browser.
Build an idempotent consumer
Read a count, then that many lines of message_id amount. Add each amount to a total the first time its id appears, and skip any repeated id. Print total=T, then duplicates=D with the number of skipped messages.
- One redelivery
- Repeated message
Python runs in a sandboxed browser worker with a 60 second time limit. Its runtime loads from the Pyodide CDN; your code stays in this browser.
Questions about this lesson
Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.
Loading posts…