Learn to Code
All tracks
Learn to Code

Free lessons from scratch to pro — every language, databases and DevOps.

AboutContactPrivacy PolicyTerms of Use

Your progress is saved in this browser. © 2026 Learn to Code

SD
System Design
0/13 completed
Beginner
What is system design?How the internet worksBack-of-the-envelope estimation
Intermediate
Scaling basicsLoad balancersCachingDatabases at scale
Advanced
Sharding and consistent hashingMessage queues and event streamingConsistency, CAP and distributed transactionsReliability and observability
Pro
Designing a URL shortenerDesigning a chat app
Tracks / System Design / Advanced

Message queues and event streaming

Not every piece of work has to happen while the user waits. A message queue lets one part of a system hand work to another asynchronously: the producer drops a message in the queue and moves on; consumers process it when they can. Queues absorb traffic spikes, decouple services, and make slow or unreliable work (emails, payments, video encoding) safe to retry.

  web app ──▶ [ queue: send-email, resize-image, … ] ──▶ worker 1
 (producer)                                        ├──▶ worker 2   (consumers)
                                                   └──▶ worker 3

Why queues help

Python · editable

A queue turns a spike into a backlog that drains later, instead of errors. The system's job becomes keeping the backlog (and so the delay) acceptable, by adding workers when it grows.

Benefits:

  • Decoupling: producers don't know or wait for consumers; either side can be deployed, scaled or fail independently.
  • Load levelling: absorb bursts; process at a steady rate.
  • Reliability: messages wait safely while a consumer is down, and failed jobs can be retried.
  • Fan-out: one event, many independent consumers (email, analytics, search indexing).

Delivery guarantees

Networks and consumers fail, so the important question is: what happens when a consumer crashes mid-job?

Guarantee Meaning How
At most once Never duplicated; may be lost Acknowledge before processing
At least once Never lost; may be duplicated Acknowledge after processing (the common default)
Exactly once Neither Only within special systems (Kafka transactions), or approximated by at-least-once + idempotency

With at-least-once delivery, the same message can arrive twice (the consumer processed it but crashed before acknowledging). So consumers must be idempotent: processing a message twice has the same effect as once.

Python · editable

Failed messages and dead-letter queues

If a message keeps failing (a bug, bad data), retrying forever blocks the queue. After a few attempts with exponential backoff, move it to a dead-letter queue (DLQ) for humans to inspect, and alert when the DLQ isn't empty.

Queues vs event streams

Message queue (RabbitMQ, Amazon SQS) Event log / stream (Kafka, Kinesis, Redpanda, Pulsar)
Model Tasks: each message is processed by one consumer, then deleted An append-only log: events are kept (days, or forever)
Consumers Compete for messages Each consumer group reads the whole log at its own pace
Replay No: once handled, gone Yes: re-read history, add new consumers later
Ordering Usually per queue, weak with many consumers Strict within a partition
Typical use Background jobs: emails, thumbnails, payments Event-driven architecture, analytics, change data capture, activity feeds

Partitions and ordering

A Kafka topic is split into partitions for parallelism. Each partition is an ordered log; events with the same key always go to the same partition, so events for one order stay in order:

Python · editable

Within a consumer group, each partition is read by one consumer, so the number of partitions caps the group's parallelism.

Patterns

  • Work queue: offload slow tasks from web requests (the background-tasks lessons in the Node and Python tracks).
  • Publish/subscribe: one event, many subscribers. "OrderPlaced" → inventory, email, analytics.
  • Event sourcing: store every change as an event; current state is derived by replaying them.
  • Change data capture (CDC): stream the database's changes (Debezium) to search indexes, caches and warehouses.
  • Transactional outbox: write the event to an outbox table in the same transaction as the business change, then a relay publishes it. This avoids the "saved the order but failed to publish the event" inconsistency.

Practice

  1. In the spike simulation, how many workers keep the worst backlog under 1,000?
  2. Change the idempotent consumer to store processed IDs with a timestamp and forget them after 24 hours. What risk does that introduce?
  3. Design the events and consumers for a food-delivery order, from "placed" to "delivered".
✎ Check your understanding

1.Why must consumers in an at-least-once system be idempotent?

2.What's a key difference between a queue like SQS and a log like Kafka?

3.How does Kafka keep all events for one order in order?

← Sharding and consistent hashing