Skip to content
System Design Fundamentals

Lesson 6 of 12 · 20 min

x
6/12

Lesson position in the course — not completion. Use Mark Complete to track finished lessons (saved in this browser).

Message Queues & Async Communication

Synchronous coupling creates chains of failure: if the email service is slow, the order service is slow. Message queues decouple producers from consumers — the order service publishes an event and returns immediately; the email service processes it when it can. This improves availability, absorbs traffic spikes, and enables independent scaling of each service.

Kafka is the dominant choice for high-throughput event streaming. AWS SQS and RabbitMQ handle task queues — background jobs, email sending, file processing. The key design decision is whether consumers need to replay events (Kafka retains messages for days) or just process once (SQS deletes after acknowledgement). Dead-letter queues catch repeatedly failing messages so they can be inspected without blocking the main queue.

Before
Synchronous — slow email blocks order response
1// User waits for ALL of these before getting a response2async function placeOrder(order: Order) {3  await db.saveOrder(order);          // 20ms4  await emailService.sendConfirm();   // 800ms (if slow)5  await inventoryService.reserve();   // 150ms6  await analyticsService.track();     // 300ms7  return { success: true };           // total: ~1,270ms8}
After
Async — order confirmed in 25ms, rest runs in background
1async function placeOrder(order: Order) {2  await db.saveOrder(order);                    // 20ms3  await queue.publish('order.placed', order);   // 5ms4  return { success: true };                     // total: 25ms5}6 7// These run independently, in parallel, at their own pace:8queue.subscribe('order.placed', emailService.sendConfirm);9queue.subscribe('order.placed', inventoryService.reserve);10queue.subscribe('order.placed', analyticsService.track);

Check your understanding

  • What does a queue buy the caller?Show answer

    Answer

    It can return after publish; slow consumers no longer block the request path.
  • Kafka vs SQS-style queues — one distinction?Show answer

    Answer

    Kafka retains a stream for replay/consumers at their pace; many task queues delete after ack.
  • What are dead-letter queues for?Show answer

    Answer

    Parking repeatedly failing messages for inspection without blocking the main queue.
Previous

Progress is saved in this browser.

Next Lesson