Sub topic 16
Choosing a broker
This chapter assumes you have read the earlier chapters on acknowledgements, delivery guarantees, ordering, durability, publish/subscribe, and log-based brokers. It does not teach a product. It gives you a way to compare brokers against the requirements of your system.
Start with requirements, not products
Teams often pick a broker because it is popular or already known. That works until a requirement the broker handles badly shows up. Write down the answers to these questions first:
- Work queue or event stream? Does each message describe a task that one worker should perform once? Or is it an event that several independent consumers need to read, possibly again later?
- What ordering do you need? Is there no ordering requirement, ordering per entity (for example per order ID), or a single global order?
- How durable must messages be? Can you lose the last few seconds of messages on a crash, or must every accepted message survive a node failure?
- How much throughput and how many messages in flight? Think of steady rate, peak rate, and message size.
- Do you need replay? Can a new consumer, or a consumer that fixed a bug, re-read old messages?
- Who will run it? A managed service and a self-operated cluster have very different costs.
The answers narrow the field quickly. A requirement for replay rules out a design where messages are deleted on acknowledgement. A requirement for per-message routing rules out a bare log.
The three broker families
Earlier chapters introduced these shapes. Here they are as decision options.
| Family | Examples | Message lifetime | Consumer model | Typical strength |
|---|---|---|---|---|
| Queue broker with exchanges | RabbitMQ, ActiveMQ | Removed after acknowledgement | Broker pushes messages, per-message ack | Flexible routing, per-message retries and dead-lettering |
| Managed cloud queue | Amazon SQS, Google Pub/Sub, Azure Service Bus | Removed after ack or deleted; retention is capped | Pull or push, per-message ack or visibility timeout | Little to operate, scales without capacity planning |
| Log-based broker | Apache Kafka, Redpanda, Apache Pulsar | Kept until the retention limit, regardless of consumption | Consumers track an offset per partition | High throughput, replay, per-key ordering |
Lightweight systems fit alongside these. NATS gives low-latency messaging, and its JetStream layer adds persistence. Redis lists and streams suit small systems that already run Redis, but their durability depends on how you configure Redis persistence and replication. Check the exact guarantees of each product in its documentation, because defaults differ and change between versions.
Comparing on the dimensions that matter
Ordering
A queue with several competing consumers gives you no order across consumers, even if messages leave the queue in order. A log-based broker orders messages within a partition, and you choose the partition with a key. All events for one order ID then arrive in order at one consumer, while different orders run in parallel. The cost is that one partition is read by one consumer in a group, so the partition count limits your parallelism. A hot key can overload one partition.
Managed queues offer a similar option. Amazon SQS FIFO queues order messages within a message group. Standard SQS queues give only best-effort ordering. RabbitMQ preserves order on a single queue with a single consumer, but redelivery after a failure can reorder messages.
If you need strict global order, accept one partition or one consumer, and the throughput ceiling that comes with it. Ask whether you really need global order. Per-entity order is usually enough.
Delivery guarantees
Almost every broker gives you at-least-once delivery in practice. Some advertise exactly-once features, but these apply within a defined boundary, such as Kafka transactions between Kafka topics, or SQS FIFO deduplication within a limited time window. They do not cover a side effect such as sending an email or charging a card. Plan on idempotent consumers whichever broker you choose. A broker with weaker guarantees is not a reason to skip this, and a broker with stronger ones does not remove it.
Durability
Durability is a combination of broker settings and your producer code. A durable queue, a persistent message, and replication across nodes are separate settings. In RabbitMQ, quorum queues replicate across nodes. In Kafka, the replication factor and the producer acks setting decide whether a write survives a broker loss. Managed services replicate for you, but you still need to know the retention period, because a message that waits longer than the retention period is deleted.
Failure handling
Here the families differ most in daily work.
- RabbitMQ and managed queues handle failures per message. You reject a message, and it goes to a dead-letter queue after a limit. Retry with delay is built in or easy to assemble.
- Kafka tracks a position, not individual messages. A consumer that cannot process one message either blocks the partition or must publish the message to a separate retry or dead-letter topic itself. This is a code and design cost you take on.
If your workload is a pile of independent tasks where some fail for their own reasons, per-message acknowledgement fits better. If your workload is a stream of events where order matters and failures are rare, a log fits better.
Operations
A self-managed Kafka or RabbitMQ cluster needs capacity planning, upgrades, disk monitoring, and a plan for node loss and rebalancing. A managed service moves much of this to the provider and replaces it with a bill and with provider limits, such as maximum message size, retention, and rate quotas. Count the people and the on-call time, not only the server cost.
A decision flow
Treat this as a first filter. Real systems often fail the neat split. A common result is two brokers: a log for the event backbone and a queue for background jobs. That is fine when each solves a different problem. It is a cost when one broker would have been enough.
Scoring candidates
When two options both look reasonable, a weighted score makes the trade-offs explicit and gives the team something to argue about. The scores below are invented for the example. They are not benchmarks. Replace them with your own findings from reading documentation and running tests.
WEIGHTS = {"ordering": 3, "replay": 2, "routing": 2, "low_ops": 3}
# Scores run from 0 (poor fit) to 5 (strong fit). Illustrative only.
CANDIDATES = {
"queue_broker": {"ordering": 2, "replay": 1, "routing": 5, "low_ops": 3},
"log_broker": {"ordering": 5, "replay": 5, "routing": 2, "low_ops": 2},
"managed_queue": {"ordering": 3, "replay": 1, "routing": 2, "low_ops": 5},
}
def total(scores):
return sum(WEIGHTS[name] * scores[name] for name in WEIGHTS)
ranked = sorted(CANDIDATES.items(), key=lambda item: total(item[1]), reverse=True)
for name, scores in ranked:
print(f"{name}: {total(scores)}")With these weights the log broker scores highest, because ordering and replay carry heavy weights. Change low_ops to 6 and ordering to 1 and the ranking changes. The point is that the weights encode your priorities, so agree on them before you look at the totals.
Common mistakes
- Choosing for peak throughput you will never reach. Most systems handle their load on any mainstream broker. Pick for the requirement that is hardest to change later, such as replay or ordering.
- Using a queue as a database. A broker holds messages in transit. If you need to query history, store the data in a database and use the broker to announce changes. The transactional outbox pattern from an earlier chapter connects the two.
- Ignoring message size limits. Managed services cap message size. Large payloads belong in object storage, with a reference in the message.
- Skipping a failure test. Before you commit, kill a broker node, stop a consumer mid-message, and fill the disk in a test environment. Watch what happens to unacknowledged messages and to ordering.
- Locking in through client code. Wrap the broker in a small interface in your own code, with
publishandhandlefunctions. This does not make a migration free, since semantics differ, but it keeps broker details out of business logic.
A short checklist
Before you decide, confirm that you can state each of these in one sentence:
- The ordering rule, and the key it applies to.
- The durability requirement, and which settings deliver it.
- How a failing message is retried and where it ends up.
- How consumers scale, and what limits their parallelism.
- Who operates the broker, and what the alert is when queue depth grows.
If you cannot answer one, that is the area to test first.
Exercise
A team processes payment events. Events for the same account must be handled in order, accounts are independent, a fraud-detection service added next quarter must read the same events from the beginning of last month, and the team has no one to run a broker cluster. Which broker family would you shortlist, and what is the main trade-off you accept?
Show answer
Shortlist a log-based broker, preferably a managed offering, with the account ID as the partition key. The key gives per-account ordering while accounts run in parallel, and retention lets the fraud service replay last month's events as a separate consumer group. The trade-offs are that parallelism is limited by the partition count, a hot account can overload one partition, and failed messages need application-level retry or dead-letter topics because the broker tracks offsets rather than individual acknowledgements. A plain managed queue would not support replay, so it fails the fraud requirement.