Message Queues
A message queue decouples a producer (the service that generates work) from a consumer (the service that processes it) by placing a durable buffer between them. Instead of service A calling service B directly and waiting for a response, A drops a message into the queue and returns immediately; B picks it up when ready. This pattern enables async processing, load leveling (the queue absorbs traffic spikes), retry on failure, and loose coupling — B can go down and restart without A caring. Message queues are fundamental to resilient microservices and any system that needs to offload slow, heavy, or unreliable work. This page covers the core concepts before diving into RabbitMQ and Kafka.
Queues vs direct calls
SYNCHRONOUS (direct call) ASYNC (message queue)
A ──HTTP/gRPC──► B A ──publish──► [Queue] ──consume──► B
waits for reply A returns immediately B processes when ready
Problems with direct: Benefits of queue:
• B slow → A blocks • A never blocks on B's speed
• B down → A errors immediately • B can restart; messages wait
• Traffic spike → B overwhelmed • Queue absorbs spikes; B processes at pace
• A must know B's address • A doesn't know B exists (loose coupling)Key concepts
Concept | Meaning |
|---|---|
Producer | Publishes messages to a queue or topic |
Consumer | Reads and processes messages, then acknowledges them |
Acknowledgement (ack) | Consumer signals "I processed this" — broker removes it from the queue |
Negative ack / nack | Consumer signals failure — broker requeues or dead-letters the message |
Dead-letter queue (DLQ) | Holds messages that failed too many times for manual inspection |
At-least-once delivery | The broker may redeliver — consumers must be idempotent |
Exactly-once | Hard to guarantee end-to-end; most systems aim for at-least-once + idempotency |
Queue vs pub/sub vs stream
Pattern | Semantics | Use when |
|---|---|---|
Queue | Each message consumed by ONE consumer (competing consumers) | Distribute work across worker pool (email sending, jobs) |
Pub/sub | Each message delivered to ALL subscribers (fan-out) | Notify multiple services of an event (user created → email + analytics) |
Stream / log | Ordered, persistent log; consumers read at their own position | Event sourcing, replay, audit log (Kafka) |