Message Queues
Hand work to background consumers without assuming exactly-once effects.
system design, queues, asynchronous workHand work off
A queue holds messages for workers to process later. A producer submits a job; a consumer receives it and acknowledges it after handling. This decouples request time from processing time, but the result is not immediately ready. Durability and delivery guarantees depend on the chosen queue configuration.
Image resizing
- An upload is stored durably and a resize job is enqueued.
- A worker receives the job and writes the resized image.
- It acknowledges the job. If it crashes after writing but before the acknowledgement, the job may be delivered again. Use a stable image version and safe repeatable writes.
See the reasoning
Follow the example one step at a time
Trace a resize job and its acknowledgement. This is a prepared teaching demonstration, not live code execution.
Enqueue
durable image → queue: resize job 42
The upload is stored and a job is submitted.
Order-confirmation emails
An order service can enqueue an email job and return without waiting for an email provider. A worker sends it later. If the provider accepted a send but the response was lost, retrying may send a duplicate: use provider-supported idempotency if available, or explicitly tolerate and reconcile duplicates. If order commit and enqueue are separate, a crash between them can lose the notification; a transactional outbox can record the event with the order.
Trade-offs and failure
Queues need capacity, monitoring of oldest job and backlog, retry limits, and a way to inspect failed jobs. Ordering is not guaranteed by every queue. At-least-once delivery is not exactly-once business effect; make consumers idempotent where possible. A queue smooths bursts but cannot cure sustained arrival rates above processing capacity.
Reflect on the concept
Four fixed questions to reinforce the reasoning. Answers are not saved or scored.
