Home / System Design / Message Queues & Async Processing

๐Ÿ“จ Message Queues & Async Processing

Fundamentals โฑ 6 min read

A message queue lets one part of your system hand off work to another without waiting for it to finish. This decouples services, smooths traffic spikes, and makes systems more resilient โ€” a fundamental building block in large designs.

The producer-consumer model

A producer puts a message on the queue; a consumer reads and processes it later. The producer doesn't wait for the work to complete โ€” it just needs the message to be accepted. If consumers are slow, messages simply wait in the queue instead of dropping requests.

Why use a queue

  • Decoupling โ€” services don't need to know about each other or be online at the same time.
  • Load levelling โ€” absorb spikes; consumers drain the backlog at their own pace.
  • Resilience โ€” if a consumer crashes, messages stay in the queue and get retried.
  • Async work โ€” offload slow tasks (emails, image processing, analytics) so the user request returns fast.

Queues vs pub/sub

A queue delivers each message to one consumer (work distribution). Publish/subscribe broadcasts each message to every subscriber (fan-out). Kafka is a log-based system supporting both patterns at huge scale; RabbitMQ is a classic message broker.

Delivery guarantees

Most systems offer at-least-once delivery, which means duplicates are possible โ€” so consumers should be idempotent (processing the same message twice has no extra effect). Exactly-once is expensive and rare; at-most-once risks losing messages.

Frequently Asked Questions

What problem does a message queue solve?
It decouples services and lets slow or spiky work be processed asynchronously, so a user request can return immediately while the heavy work happens in the background, with retries if a consumer fails.
What is the difference between a message queue and pub/sub?
A queue delivers each message to a single consumer to distribute work. Pub/sub broadcasts each message to all subscribers, which is useful for fan-out to many services.
Why should consumers be idempotent?
Because most queues deliver at-least-once, meaning the same message can arrive more than once. Idempotent consumers produce the same result no matter how many times a message is processed.