stackbook.io
🔍
← Back to Syllabus
Day 9Level 1 — Building Blocks

Queues and Asynchronous Messaging

Learn: 35 minMap: 25 minDesign: 45 min

Core Concepts (5)

  • 1.A queue decouples producers from consumers
  • 2.Async work shortens user-facing latency
  • 3.Delivery is usually at least once, so consumers must be idempotent
  • 4.Failed messages go to a dead-letter queue
  • 5.Queues absorb bursts but hide backlog

Map to Your Experience

Think of a task that your app does while the user waits, like sending an email. Does the user really need to wait for it?

Design Brief: Make Order Confirmation Asynchronous

Learn

In synchronous communication, the caller waits for the answer. In asynchronous communication, the caller hands over a message and carries on. A message queue is the usual tool: producers put messages in, consumers take them out later, and the queue holds them in between.

This decoupling has three benefits. First, speed: a checkout can save the order, enqueue a send-confirmation message, and respond immediately, rather than waiting for the email provider. Second, resilience: if the email service is down, messages wait in the queue instead of failing the checkout. Third, smoothing: a sudden burst of work piles up in the queue and consumers work through it at a steady pace.

The price is complexity. The work finishes later, so the user interface must cope with in-progress states. Ordering may not be guaranteed. And most queues promise at-least-once delivery, meaning a message can arrive twice, for example if a consumer crashes after doing the work but before acknowledging it.

That is why consumers should be idempotent: handling the same message twice must give the same result as handling it once. A simple technique is to give each message a unique id, record the ids already processed, and skip repeats. Sending an invoice becomes safe: check whether invoice 123 was already sent.

Some messages will fail every time, perhaps because of malformed data. Retrying forever blocks the queue and wastes resources. The standard answer is to retry a limited number of times, then move the message to a dead-letter queue where a person can inspect it, fix it, and replay it.

Finally, watch queue depth. A queue that grows steadily means consumers cannot keep up. Because producers never feel the pain, the backlog can grow unnoticed until messages are hours late, so alert on depth and on the age of the oldest message.

Map

List four tasks in an online shop checkout. Mark each as must be synchronous or can be asynchronous and explain the user impact if it is delayed by five minutes.

Design

Redesign checkout so that confirmation email and invoice generation run asynchronously. Describe the message format, including an id, the consumer steps, and how duplicates are detected. Define the retry count and the dead-letter handling, and name two metrics you would alert on. State what the user sees if the invoice is not yet ready.

Requirements

  • •Show what stays synchronous and what moves
  • •Handle duplicate messages safely
  • •Define a dead-letter policy

Constraints

  • •Checkout must respond in under one second
  • •No customer may receive two invoices

Must Cover

sync-vs-asyncidempotent-consumerdead-letter

Trade-off Axis

latency vs complexity and eventual completion

Competencies Advanced

distribution communication → L1reliability ops → L1

Ready to Design?

Open this day in the Design Studio. The studio follows this track: its days, concepts, patterns and trade-offs are loaded, and you keep one design across the whole track.

Open in Studio →