Programming
We provide developers and engineers the guides and tutorials they...
Search
We provide developers and engineers the guides and tutorials they...
Content is king. It's not just about how you're going to write, b...
Looking for an engaging connection with thousands of other people...
How message queues decouple systems: producers, consumers, queues vs logs, Kafka partitions and offsets, delivery guarantees, ordering and dead letters.
Quick answer: A message queue sits between services. A producer writes a message to the queue and moves on; a consumer reads and processes it later. Neither needs to know about the other, be running at the same moment, or work at the same speed. That decoupling absorbs traffic spikes, survives downstream failures, and lets many systems react to the same event. Traditional brokers such as RabbitMQ deliver each message to a worker and then delete it. Log-based systems such as Kafka keep an ordered, durable log that many consumers read at their own pace and can replay.
Also Read: How to Design a URL Shortener Like Bitly - How To's
Suppose placing an order must charge the card, send a confirmation email, update inventory and notify analytics. With direct synchronous calls:
With a queue, the checkout service publishes one "order placed" message and returns. Each downstream service consumes it independently.
Each message goes to one consumer. Several workers pull from the same queue and share the load. This is ideal for background jobs: resize this image, send that email.
Each message goes to every subscriber. The order service publishes "order placed"; inventory, email and analytics each receive their own copy.
Most systems support both.
RabbitMQ, Amazon SQS and ActiveMQ follow the classic model:
If the consumer crashes before acknowledging, the broker redelivers the message to another consumer.
RabbitMQ adds flexible routing. Producers publish to an exchange, which routes to queues according to rules: by exact key, by pattern, or to all bound queues. The AMQP concepts guide explains the model.
Kafka, along with systems like Amazon Kinesis and Apache Pulsar, works differently. It is a distributed, append-only log, described in the Kafka documentation.
This design has important consequences:
Kafka guarantees order within a partition, not across a topic. Producers choose a partition by hashing a key, so all messages with the same key (say, one customer's ID) land in the same partition and stay in order.
| Traditional queue (RabbitMQ, SQS) | Log (Kafka) | |
|---|---|---|
| After consumption | Message is deleted | Message is retained |
| Replay | No | Yes |
| Multiple independent consumers | Needs a queue each | Built in via consumer groups |
| Ordering | Per queue, weaker with several consumers | Strict per partition |
| Routing | Rich | Simple: topic and key |
| Typical use | Task queues, request routing | Event streams, pipelines, high volume |
Networks fail and processes crash, so messaging systems offer one of three guarantees:
Also Read: Caching Strategies: Cache-Aside, Write-Through and More
| Guarantee | Meaning | Risk |
|---|---|---|
| At most once | Send and never retry | Messages can be lost |
| At least once | Retry until acknowledged | Messages can be duplicated |
| Exactly once | Each message takes effect once | Hard; achieved only within limits |
At least once is the practical default. A consumer might process a message, crash before acknowledging, and receive it again. So consumers must be idempotent: processing the same message twice must have the same effect as processing it once. Typical techniques are deduplicating by message ID or using naturally idempotent writes. See how payment systems avoid charging you twice.
"Exactly once" in systems like Kafka means exactly-once effect within Kafka's own transactions. Once you call an external system, such as an email provider, you are back to at-least-once plus idempotency. The reason is fundamental; see the Two Generals' Problem.
A queue adds a component to operate and makes behaviour asynchronous, which is harder to debug and reason about.
RabbitMQ is a message broker that routes messages to queues and deletes them once consumed. Kafka is a distributed log that retains messages so many consumers can read and replay them.
A set of consumers that share the work of reading a topic. Each partition is read by one member of the group, and the group tracks its position with offsets.
Only within limits. Kafka guarantees order within a partition. With several competing consumers on one queue, processing order is not guaranteed.
A holding queue for messages that repeatedly failed to process, so they do not block others and can be examined later.
A message queue replaces "do this now and wait" with "this happened; deal with it when you can". That one change decouples services in time, in speed and in failure. Choose a broker for task distribution and routing, a log for event streams and replay, assume at-least-once delivery, and make every consumer idempotent.