12 / 25 · Asynchronous work
Message queues
A message queue holds work until a consumer can process it. It separates accepting work from finishing it. Durability, retries, ordering and duplicate delivery depend on the queue and configuration.
What you will practise
- Trace publish/process/acknowledge
- predict duplicates
- calculate backlog growth
Before you start: Producer, consumer and durable side effect.
About 4 minutes · offline-generated English female narration · captions included. Diagrams are conceptual; timings are illustrative.
Download MP4CaptionsListen without the video
Download audioChapters and full transcript
Let us understand message queues. A restaurant writes orders on tickets. The cook takes a ticket when ready instead of making every customer stand at the stove. A ticket can be picked up twice after confusion, so the kitchen needs an order number to avoid preparing and charging the same order twice. A message queue holds work until a consumer can process it. It separates accepting work from finishing it. Durability, retries, ordering and duplicate delivery depend on the queue and configuration.
Follow the normal path. A booking succeeds and records a receipt job reliably. Coordinate this with business storage, for example using an outbox, so a crash does not lose the obligation. The queue stores work while consumers catch up. Stored backlog buys time, but retention, storage and acceptable delay remain finite. SeatSpot sends receipts asynchronously after booking. The booking request should not wait for an email provider, but it must reliably record that a receipt needs to be sent.
Now change one condition. Workers pull jobs at a safe rate. Their combined concurrency must respect email provider quotas and other downstream capacity. A common misunderstanding is this: At-least-once delivery means exactly one business effect. Delivery and the external effect have different failure gaps. Think about this. A worker sends a receipt then crashes before acknowledgment. What can happen next? Pause the video and make your prediction. The job may be delivered again. A stable event ID and appropriate idempotency or reconciliation are needed around the external effect.
Let us reason with a concrete example. Jobs arrive at 90/second and each worker safely processes 20/second. Four workers provide 80/second, so backlog grows by about 10/second. Five provide 100/second and drain only about 10/second of existing backlog in the simplified model. Add safe capacity or shed work, respecting the email provider quota. More consumers are not safe if they collectively exceed the next dependency limit. These figures are teaching examples, not production guarantees.
Turn the idea into a design decision. A worker can send an email and fail before acknowledging its job. The queue may deliver that same logical job again. Use stable job identity and an appropriate idempotency or reconciliation strategy. A delivery guarantee alone does not guarantee exactly one external effect. When you try the practice task, look for this result. At a constant arrival rate, five workers leave only about ten jobs/second to drain the backlog.
Check your understanding. After bounded attempts, move a poison job to a monitored dead-letter path. Investigate and repair before controlled replay instead of retrying forever. Here is a challenge. A worker sends an email then crashes before acknowledging. What happens? Pause before the answer. Redelivery may send another email unless the consumer/provider has an idempotency or deduplication strategy. A durable order/event identity and reconciliation make this case explicit. For an interview, discuss backlog age, retries, poison jobs and idempotency. Say what you would measure, not only which tool you would buy.
Start with an everyday picture
A restaurant writes orders on tickets. The cook takes a ticket when ready instead of making every customer stand at the stove. A ticket can be picked up twice after confusion, so the kitchen needs an order number to avoid preparing and charging the same order twice.
Why SeatSpot needs this
SeatSpot sends receipts asynchronously after booking. The booking request should not wait for an email provider, but it must reliably record that a receipt needs to be sent.
Trace one step at a time
Move through the diagram at your pace. Teal paths are active; unavailable components are labelled.
Record outgoing work
A booking succeeds and records a receipt job reliably. Coordinate this with business storage, for example using an outbox, so a crash does not lose the obligation.
Read all six mechanism steps
Record outgoing work
A booking succeeds and records a receipt job reliably. Coordinate this with business storage, for example using an outbox, so a crash does not lose the obligation.
Buffer a burst
The queue stores work while consumers catch up. Stored backlog buys time, but retention, storage and acceptable delay remain finite.
Process within limits
Workers pull jobs at a safe rate. Their combined concurrency must respect email provider quotas and other downstream capacity.
A worker crashes
A worker can send an email and fail before acknowledging its job. The queue may deliver that same logical job again.
Protect the effect
Use stable job identity and an appropriate idempotency or reconciliation strategy. A delivery guarantee alone does not guarantee exactly one external effect.
Handle persistent failure
After bounded attempts, move a poison job to a monitored dead-letter path. Investigate and repair before controlled replay instead of retrying forever.
Pause and make a prediction
A worker sends a receipt then crashes before acknowledgment. What can happen next?
Correct a common misunderstanding
Claim to question: At-least-once delivery means exactly one business effect.
Better explanation: Delivery and the external effect have different failure gaps.
Calculate a queue backlog
- Use arrivals of 90 jobs/second and four workers at 20 each.
- Compute the 10-jobs/second backlog growth.
- Add a fifth worker and calculate how slowly the old backlog drains.
Check what you can explain
This is your self-check, not an automated grade. Try again without the diagram tomorrow.
0 of 3 goals checked. Choose one remaining idea to practise next.
Teach it back
Without looking, explain what this component changes, what it does not solve and what happens if it fails. Use the animation to check your explanation, then switch to Engineering.
Engineering model and trade-offs
Producers publish jobs; consumers receive, process and acknowledge them. A crash before acknowledgment can cause redelivery. Design idempotent business effects using a stable job ID and durable deduplication where required.
Backlog is stored delay, not free capacity. Monitor oldest-message age, arrival rate, service rate, retention and dead-letter messages. If arrivals exceed safe processing over time, the queue grows until a limit is reached.
To coordinate a database commit and publication, consider a transactional outbox: commit business state and an outgoing record together, then relay it with duplicate-safe consumers. A database transaction cannot automatically include an unrelated broker.
Work through an example
Jobs arrive at 90/second and each worker safely processes 20/second.
Reason with the numbers
Four workers provide 80/second, so backlog grows by about 10/second. Five provide 100/second and drain only about 10/second of existing backlog in the simplified model.
Make a decision
Add safe capacity or shed work, respecting the email provider quota. More consumers are not safe if they collectively exceed the next dependency limit.
Implementation sketch
BEGIN
insert booking
insert outbox(event_id, type="ReceiptRequested", booking_id)
COMMIT
Relay: publish outbox event; retry safely
Consumer: apply effect once per event_id; acknowledgeConceptual outbox flow. A real implementation needs polling/CDC, durable state, concurrent ownership and an external side-effect reconciliation strategy.
Where this is used
- Background receipts, exports and image processing.
- Smoothing bursts within measured processing capacity.
Common mistakes
- Acknowledging before durable work is safe.
- Assuming at-least-once delivery prevents duplicates.
- Treating the queue as an unlimited buffer.
Practise a reasoned answer
Answer aloud or write notes before opening the reference. These prompts teach reasoning and trade-offs; they are not claimed questions from a particular company.
How do you handle poison messages?
Choose a practice depth
- Foundation
- Explain producer, queue, consumer and acknowledgment.
- Practical
- Discuss backlog age, retries, poison jobs and idempotency.
- Advanced
- Defend outbox ownership, replay, provider quotas and reconciliation across crash gaps.
Self-check rubric
- State the required user outcome and the relevant invariant.
- Explain the normal path and one failure path.
- Name a cost or consistency/latency trade-off.
- Say what you would measure or test before trusting it.
For deeper practice, connect this topic to the previous lesson and identify a shared bottleneck. A longer answer is not automatically a better answer.
Try it before viewing the solution
A worker sends an email then crashes before acknowledging. What happens?
Check one key idea
Your explanation and interview notes
Saved only in this browser when storage is available. Export a backup from the course page before clearing browser data or moving devices.
Loading your local notes…
Official references and next reading
The explanation, fictional examples and animations are original teaching material. Vendor behavior depends on the specific product/version; use its documentation before deploying.