Sub topic 16
Choosing a broker
This chapter assumes you know producers, consumers, acknowledgements, delivery guarantees, idempotent consumers, ordering, and the difference between work queues and log-based brokers. Here you use that model to pick a broker for a real system.
Start from requirements, not products
Brokers differ less in features than in the model they are built around. Decide your needs first, then match them to a model.
- Ordering. Do you need order across all messages, or only per key, such as per customer?
- Durability. Can you lose messages on a crash, or must every accepted message survive a restart?
- Throughput. Do you handle hundreds of messages per second or hundreds of thousands?
- Replay. Do consumers need to re-read old messages, or is a message finished once it is acknowledged?
- Routing. Do messages go to one worker, to many subscribers, or to subscribers chosen by rules?
- Operations. Who runs the broker, and how much tuning and monitoring can your team take on?
Two models
A queue broker such as RabbitMQ holds messages until a consumer acknowledges them, then deletes them. It has flexible routing, per-message acknowledgement, retries, and dead-letter queues. It suits task distribution.
A log-based broker such as Apache Kafka appends messages to a partitioned log and keeps them for a set time. Consumers track their own position, so they can replay. Order holds within a partition, and throughput scales by adding partitions. It suits event streams and several independent readers of the same data.
Comparing the options
| Concern | Queue broker | Log-based broker | Managed queue service |
|---|---|---|---|
| Message after consumption | Removed | Kept until retention ends | Removed |
| Ordering | Per queue, weakened by competing consumers and requeues | Per partition | Often optional or per group |
| Replay | Not built in | Built in | Not built in |
| Routing | Rich (exchanges, bindings) | Topic and partition | Basic |
| Operations | Moderate | Heavier | Light |
A managed service, for example Amazon SQS, trades control for less operational work. You do not patch servers or size disks, but you accept its limits on features and message size.
Common mistakes
- Choosing for scale you do not have. A log-based cluster is more work to run than a single queue broker. If one queue handles your load, start there.
- Expecting global order. Both models give order only within a queue or partition. Parallel consumers weaken it. Pick a partition key, or design consumers so order does not matter.
- Assuming the broker gives exactly-once. Whatever you choose, plan for duplicates and keep consumers idempotent.
- Ignoring durability settings. Persistent messages need a durable queue and, in most brokers, replication or disk flushing configured to match your loss tolerance.
A worked choice
An online shop needs to resize uploaded images in the background. Each job is independent, order does not matter, and a job should retry a few times and then go to a dead-letter queue. Nobody needs to replay old jobs.
That points to a queue broker or a managed queue service. If the team has no one to run a broker, choose the managed service.
Later the shop wants order events read by billing, analytics, and shipping, each at its own pace, with analytics able to reprocess last week. That needs replay and several independent readers, so a log-based broker fits.
Exercise
A team must process 200,000 click events per second. Several services read the same events independently, and one service must be able to reprocess the last three days after a bug fix. Which broker model fits, and why?
Show answer
A log-based broker. It handles high throughput by adding partitions, it keeps messages for a retention period so a service can replay three days of events, and each consumer tracks its own position, so independent readers do not interfere.