Message queue vs event stream
Both carry messages between systems asynchronously, and both appear under the word “messaging”. They behave very differently.
- Message queue (for example RabbitMQ (opens in a new tab), Amazon SQS (opens in a new tab), Azure Service Bus (opens in a new tab)): a message is delivered to one consumer, which processes it and acknowledges it. Then it’s gone. Queues distribute work.
- Event stream (for example Apache Kafka (opens in a new tab), Amazon Kinesis (opens in a new tab), Azure Event Hubs (opens in a new tab)): events are appended to an ordered, durable log. Many consumers read it independently, each tracking its own position, and can go back and re-read. Streams record facts.
At a glance
Section titled “At a glance”| Aspect | Message queue | Event stream |
|---|---|---|
| Model | Work to be done | Facts that happened |
| After consumption | Message is removed | Event stays for the retention period (or forever) |
| Consumers | Competing consumers share the work | Each consumer group reads everything |
| Replay | Not normally possible | Built in: reset a consumer’s position |
| Ordering | Often not guaranteed, or per group with special queue types | Guaranteed within a partition |
| Scaling consumers | Add consumers freely | Limited by the number of partitions |
| Per-message features | Delays, priorities, per-message retries and dead-lettering | Fewer; failure handling is up to the consumer |
| Typical uses | Background jobs, task distribution, commands to one service | Event-driven integration, audit, analytics feeds, CDC, rebuilding read models |
Choose a queue when
Section titled “Choose a queue when”- Each message is a task that exactly one worker should do: send this email, resize this image, process this payment.
- You want to add workers to drain a backlog faster, with no ordering requirement.
- You need per-message delays, priorities and dead-letter queues without building them yourself.
Choose a stream when
Section titled “Choose a stream when”- Several systems need the same events, and new consumers will appear later.
- Order matters, for example every change to one account in sequence.
- You need to replay history: rebuild a read model, backfill a new service, or recover from a consumer bug.
- The events are part of your record of what happened.
Things that catch teams out
Section titled “Things that catch teams out”- Replay is powerful and dangerous. Replaying events into a consumer that sends emails or charges cards repeats those side effects unless the consumer is idempotent.
- Partitions cap parallelism. A stream with 12 partitions can’t be consumed by more than 12 consumers in one group. Choosing partition keys and counts is a design decision.
- A slow message blocks its partition. In a stream, one poison event stops everything behind it in that partition. Plan how to park it.
- Queues aren’t faster by themselves. A queue moves waiting from the caller to the queue; it doesn’t add capacity.
The verdict
Section titled “The verdict”Use a queue to hand out work, and a stream to publish facts. If you find yourself copying messages to several queues so different systems can see them, or wishing you could replay last Tuesday, you want a stream. If you’re building retries, delays and dead-lettering on top of a stream for a job system, you want a queue.
Read more
Section titled “Read more”Written and edited by Abhinav Taduka, enterprise software architect.
New pages are added regularly. Follow Abhinav on LinkedIn to see them first.