Event Streams and Message Brokers
How events move from producers to consumers through a broker: queues that delete a message once handled, logs that keep every message, and what each means for ordering, scaling and delivery guarantees.
Overview
An event is a small, immutable record that something happened: a click, a payment, a sensor reading. An event stream is the never-ending sequence of such events. Producers write events; consumers read and act on them.
Producers and consumers could talk directly, but then a slow or crashed consumer loses events, and every producer must know every consumer. A message broker sits in between. Producers hand events to the broker, the broker stores them, and consumers read at their own pace. The broker absorbs bursts, survives consumer restarts, and lets you add consumers without touching producers.
Two kinds of broker: queues and logs
Brokers differ mainly in what happens after a consumer handles a message. The two common designs answer that question in opposite ways, and each answer leads to a different set of strengths.
- After a message is handled: a queue broker, such as a classic RabbitMQ queue or ActiveMQ, deletes it once the consumer acknowledges it. A log broker, such as Kafka, keeps it until the retention limit, whoever has read it.
- Where the read position lives: a queue broker tracks it in the broker, per message (delivered, acknowledged). A log broker leaves it to the consumer, as one offset per partition.
- Several consumers on one stream: on a queue they compete, and each message goes to one of them. On a log, each consumer group reads every message.
- Replaying old messages: a queue cannot, once a message is acknowledged. A log lets you rewind the offset and read again.
- Ordering: a queue can change the order when messages are redelivered. A log keeps the order within each partition.
So a queue is good for task queues where each job runs once, and a log is good for event history, many independent readers and rebuilding state.
RabbitMQ hands each message in a queue to the next consumer in turn and deletes it after a positive acknowledgment. If a consumer's connection closes before it acknowledges, the message goes back on the queue and is delivered again with a redelivered flag. Kafka instead appends every event to a log and keeps it for a set time; its docs note that, unlike classic messaging systems, events are not deleted after consumption.
The line has blurred. RabbitMQ now offers streams, an append-only log that consumers can read repeatedly. Kafka 4.2 made share groups production-ready: a queue-style mode where several consumers take records from the same partition and acknowledge them one by one. The two models are still the clearest way to reason about the choice.
Key concepts
- Message broker
- Log-based broker
- Partition
- Offset
- Consumer group
- At-least-once delivery
- Log compaction
- Idempotent producer
Interview questions
- How does Kafka guarantee message ordering?
- When would you choose RabbitMQ-style queues over a Kafka-style log?
- A topic has 8 partitions and a consumer group has 10 consumers. What happens, and how would you get more throughput?
- How do you make sure a consumer neither loses nor double-applies events?
- Your team shipped a bug that wrote wrong values for three days. How do you use the log to fix it?
Sign up free to read the full topic
More DDIA topics
- Column-Oriented Storage and Compression
- Consensus Algorithms: Paxos and Raft
- Consistency Guarantees: Eventual, Causal and Linearizable
- Declarative vs Imperative Query Languages
- Encoding Formats: JSON, Protobuf, Avro, Thrift
- End-to-End Correctness and the Exactly-Once Myth
- Fan-Out and the Twitter Timeline Problem
- Fault Tolerance in Stream Processing
- Faults and Partial Failures
- Graph Data Models
- Implementing Serializability
- Isolation Levels
Start free on Cariara · Coding problems · System design studio