Message queues enable asynchronous communication between services by decoupling producers (senders) from consumers (receivers). They're essential for building resilient, scalable systems that handle load spikes and service failures gracefully.
Synchronous communication (direct HTTP calls) creates tight coupling:
A message queue solves all three:
Publishes messages to the queue. Doesn't know or care who consumes them or when.
The durable buffer that holds messages. Messages persist until consumed and acknowledged.
Reads messages from the queue and processes them. Acknowledges successful processing.
After processing, the consumer sends an ACK. The queue then deletes the message. If the consumer crashes before ACKing, the queue re-delivers the message (at-least-once delivery).
| Guarantee | Description | Trade-off | |---|---|---| | At-most-once | Message delivered 0 or 1 times | Can lose messages | | At-least-once | Message delivered 1+ times | Can have duplicates | | Exactly-once | Message delivered exactly once | Complex, slower |
Most systems use at-least-once delivery and design consumers to be idempotent (processing the same message twice has no extra effect).
Multiple consumers compete for messages from a single queue. Each message is processed by exactly one consumer. Used for task distribution and parallel processing.
Producer → [Queue] → Consumer A
→ Consumer B (one of them gets each message)
→ Consumer C
Best for: Background job processing, image resizing, email sending
One message is delivered to all subscribers. Producer publishes to a topic; each subscriber gets a copy.
Producer → [Topic] → Consumer A (all get the message)
→ Consumer B
→ Consumer C
Best for: Event broadcasting, cache invalidation, notifications to multiple services
| System | Strengths | Best For | |---|---|---| | Kafka | High throughput, durable log, replay | Event streaming, audit logs, millions of msg/sec | | RabbitMQ | Flexible routing, mature | General task queues, complex routing needs | | Amazon SQS | Fully managed, simple | AWS-native apps, serverless | | Amazon SNS | Pub/sub fan-out | Notifications, multi-subscriber events | | Redis Streams | Simple, fast, in-memory | Low-latency queuing |
Kafka is the industry standard for high-throughput event streaming.
Key concepts:
Why Kafka is fast: