codequest / system design
Core concept 5 of 21

Message queues & pub/sub

Decouple producers from consumers so slow work happens in the background and bursts are absorbed.

Do it later

Some work doesn't need to finish before you reply to the user: sending an email, resizing an image, updating a search index, writing analytics. Put a message on a queue and reply right away; background workers take messages off the queue and do the work at their own pace.

  • Decoupling: the producer doesn't need the consumer to be up, fast or even known.
  • Burst absorption: a spike becomes a longer line instead of an outage.
  • Retries: if a worker fails, the message is delivered again.

Queue vs pub/sub

Queue: each message goes to ONE workerProducerWorker 1Worker 2Worker 3Pub/sub: each event goes to EVERY subscriberPublishertopic: ordersEmailAnalyticsSearch index
A work queue (SQS, RabbitMQ) splits work between workers; a topic (Kafka, SNS, Pub/Sub) fans each event out to every interested service.

Delivery guarantees

  • At-least-once (the usual default): a message may be delivered twice, so consumers must be idempotent (processing twice has the same effect as once, for example by tracking a message ID).
  • At-most-once: never duplicated, but may be lost.
  • Exactly-once is expensive and only holds within specific systems; in practice, design for at-least-once plus idempotency.
  • Messages that keep failing go to a dead-letter queue for inspection.

Try it

A write-heavy app with the primary database at about 88%. Turn on the async write queue: p95 drops because users no longer wait for the database to store their writes. Then, in the Traffic tab, set Reads to 30% and requests to2k. The primary can't keep up even on average, the backlog never drains, and the simulator flags writes that were acknowledged but never stored. A queue smooths bursts; it adds no database capacity.

Healthy at 1,800 req/s
p95 latency (server)
73 ms
Error rate
0.0%
Est. cost / month
$410
excl. bandwidth
App servers
400 req/s each · $40/mo
6
Hit rate
DB read replicas
+1,250 reads/s each · also applies every write · $120/mo
0

Primary DB: 1,250 queries/s (1 query per request). How the numbers work

In the interview

  • Use a queue for anything slow or non-critical to the response: notifications, media processing, fan-out.
  • State the delivery guarantee and how consumers stay idempotent.
  • Watch the backlog: if consumers are slower than producers on average, the queue only delays the failure.