Sub topic 3
Event brokers and what they do for you
This chapter assumes you know what producers and consumers are, and that you have used HTTP APIs and database transactions. It explains why a broker sits between them and which jobs it takes over.
The problem a broker solves
Suppose a producer calls each consumer directly over HTTP. The producer must know every consumer's address. It must wait for each one, and it must handle each one being slow or down. Adding a consumer means changing the producer.
An event broker is a service that sits between producers and consumers. It accepts events from producers, stores them, and delivers them to consumers. Examples are Apache Kafka, RabbitMQ, Amazon SNS and SQS, Google Pub/Sub, and NATS JetStream.
What the broker does for you
A broker provides several services that you would otherwise build into every producer and consumer.
- Decoupling in space. The producer knows only the broker and a topic or queue name. It does not know who consumes the event.
- Decoupling in time. The broker stores the event. A consumer that is down for ten minutes can catch up when it returns.
- Buffering. If events arrive faster than a consumer can process them, they wait in the broker. This absorbs traffic spikes.
- Routing. The broker decides which consumers receive which events, based on topic names, subscriptions, or filters.
- Delivery tracking. The broker tracks what each consumer has received or acknowledged. An acknowledgement (ack) is a message from the consumer that says "I finished this event."
- Durability. Most brokers write events to disk and can replicate them across machines, so a broker node failing does not lose them.
Two storage models
Brokers differ mainly in what happens to an event after delivery.
| Model | Event after delivery | Replay | Typical examples |
|---|---|---|---|
| Message broker | Removed once acknowledged | Not possible | RabbitMQ, SQS |
| Log-based broker | Kept for a set time or size | A consumer can re-read from any position | Kafka, Pulsar |
In a log-based broker, events are appended to a log, which is an ordered, append-only sequence. Each consumer keeps an offset, which is its position in the log. Resetting the offset to an earlier position replays old events. This helps when you fix a consumer bug and want to reprocess history, or when you add a new consumer that needs past events.
What the broker does not do for you
A broker is infrastructure, and it does not remove the hard parts of event-driven design.
- It does not make consumers correct. Most brokers can deliver an event more than once, so consumers still need to handle duplicates. Later chapters cover this.
- It does not give global ordering by default. Ordering usually holds only within a partition or a single queue.
- It is a new dependency. If the broker is down, producers cannot publish, so you must operate it, monitor it, and plan for its failure.
Choosing a broker
Start from your requirements instead of the product name. Ask whether you need replay, how many events per second you expect, how long events must be kept, and whether consumers need per-message routing rules. A team that only needs a background job queue is often well served by a simple message broker. A team that needs replay and many independent consumers is better served by a log-based one.
Exercise
Your team fixes a bug in a consumer that has miscalculated discounts for three days. You want to reprocess those three days of events. Which broker model supports this directly, and why?
Show answer
A log-based broker such as Kafka. It keeps events after delivery, so you can reset the consumer's offset to the start of the three-day window and replay. A message broker that deletes events after acknowledgement no longer has them.