Message Queue & Event Log
By Ankur NayakReviewed
A message queue lets one service hand work to another without waiting for it. Producers write messages, consumers process them at their own pace, and the queue absorbs spikes and failures in between. Event logs like Kafka keep messages so many consumers can replay them.
How it works
Asynchronous messaging decouples services in time: the producer only needs the queue to be up, not the consumer. If consumers fall behind or crash, messages wait in the queue instead of being lost, and the producer's latency stays low.
There are two broad families. Classic brokers (RabbitMQ, SQS) deliver each message to one consumer and delete it once acknowledged, which suits task queues. Log-based systems (Kafka, Pulsar) append messages to partitioned, replicated logs and keep them for a retention period; each consumer group tracks its own offset, so many independent consumers can read the same stream and replay history.
Delivery is usually at-least-once, so consumers must be idempotent: processing the same message twice must have the same effect as processing it once. Ordering is typically only guaranteed within a partition, so related messages share a partition key.
Key points
- Decouples producers and consumers and smooths load spikes
- Queues delete on ack; logs retain and allow replay
- Order holds per partition; choose the partition key carefully
- Design consumers to be idempotent (at-least-once delivery)
When to use it, and pitfalls
Use it for
- Work that can finish later (emails, thumbnails, analytics)
- Fanning one event out to several independent consumers
- Buffering bursts in front of a slower system
Watch out for
- Unbounded queues hide a consumer that can never catch up; alert on lag
- Poison messages that crash a consumer forever; use retries plus a dead-letter queue
- Assuming global ordering across partitions
Trade-offs
Broker queue vs. event log
Use a log when several consumers need the same events or replay matters; a queue for simple task distribution.
Pros: Log: replay, many consumer groups, high throughput; Queue: per-message acks, simple routing, delayed delivery
Cons: Log: per-partition ordering, consumer offsets to manage; Queue: a message is gone once consumed
Used in
Related building blocks
Frequently asked questions
What is the difference between Kafka and RabbitMQ?
RabbitMQ is a broker: it routes messages to queues and removes each message once a consumer acknowledges it. Kafka is a distributed log: messages are appended to partitions and retained, and each consumer group reads at its own offset, which enables replay and many independent consumers.
How do you get exactly-once processing?
In practice you combine at-least-once delivery with idempotent consumers: deduplicate by message ID or make the write naturally idempotent (an upsert keyed by event ID). Some systems offer transactional guarantees inside their own boundaries, but side effects outside them still need idempotency.
Sources
- Apache Kafka documentation · Apache Software Foundation
- AMQP 0-9-1 Model Explained · RabbitMQ Docs
- Publisher-Subscriber pattern · Microsoft Azure Architecture Center