Message broker

On this page 7

A message broker is middleware that receives messages from producers, stores them durably and delivers them to consumers, so that applications exchange data without calling each other directly. In streaming data architectures, the broker is the central hub through which events, commands and change records flow between services, pipelines and analytical systems.

The broker’s core value is decoupling. Producers do not need to know who consumes their messages, whether consumers are online, or how fast they process. The broker absorbs bursts in load, retries failed deliveries and lets new consumers subscribe without changing the producer. Brokers differ widely, from queue-oriented systems such as RabbitMQ to log-based platforms such as Kafka, and the difference shapes how data can be replayed and scaled.

How a message broker works

Every broker implements the same basic cycle: a producer publishes a message to a named destination, the broker persists it and decides where it goes, and one or more consumers receive and acknowledge it. Acknowledgment tells the broker that the message has been processed and can be removed or marked as consumed.

ComponentRoleNotes
ProducerCreates and sends messagesCan wait for broker confirmation before continuing
MessagePayload plus headers and a keyPayloads are usually JSON, Avro or Protobuf
Queue or topicNamed destination for messagesTopics may be split into partitions for parallelism
Exchange or routerDecides which queues receive a messageRouting by key, pattern or headers
ConsumerReads and processes messagesAcknowledges success or signals failure
Dead-letter queueHolds messages that repeatedly failPrevents one bad message from blocking the flow
Core components of a message broker

Queue-based and log-based brokers

Brokers fall into two architectural families. Queue-based brokers distribute each message to one consumer and delete it after acknowledgment. Log-based brokers append messages to an ordered, retained log that many consumers read independently by tracking their own position, called an offset.

AspectQueue-based brokerLog-based broker
Storage modelMessages removed after acknowledgmentMessages retained for a configured period
ReplayNot possible after consumptionConsumers can rewind to earlier offsets
OrderingPer queue, weakened with many consumersStrict within each partition
Routing flexibilityRich routing rules and prioritiesSimple, by topic and key
Scaling consumersAdd competing consumers on one queueUp to one active consumer per partition in a group
Typical useTask distribution, commands, request workloadsEvent streams, data pipelines, change data capture
Two broker architectures compared

For streaming data, log-based brokers dominate because retention and replay allow new consumers to process history, failed pipelines to recover from a known offset, and several independent systems to read the same event stream. Queue-based brokers remain the better fit for work distribution where each task must be handled exactly once by one worker.

Delivery guarantees and ordering

A broker’s delivery semantics define what happens when producers, consumers or the network fail. The choice is a trade-off between duplicates, loss and complexity.

GuaranteeBehavior on failureWhat the consumer must do
At most onceMessages may be lost, never duplicatedAccept occasional loss
At least onceMessages are never lost but may repeatProcess idempotently or deduplicate by message key
Exactly onceEach effect occurs once within a defined scopeUse transactional features end to end
Delivery guarantees

At-least-once delivery is the most common practical default. Exactly-once processing exists in some platforms but usually holds only within the broker and its integrated processing framework; once a consumer writes to an external database or calls an API, idempotent design is still required. Ordering is likewise limited: log-based brokers guarantee order per partition, so related messages must share a key that routes them to the same partition.

Key parameters in production

Operating a broker reliably depends on a handful of settings and metrics. The right values depend on message volume, tolerance for loss and recovery requirements.

  • Replication factor: the number of copies of each partition or queue; three copies is a common choice for production data.
  • Producer acknowledgment level: waiting for all in-sync replicas protects against loss at the cost of higher latency.
  • Retention period: hours to weeks for most event streams, longer for topics that serve as a replayable source of record.
  • Partition count: sets the upper limit of consumer parallelism and is hard to change later without affecting key ordering.
  • Consumer lag: the gap between the newest message and the consumer’s position, the primary signal of a pipeline falling behind.
  • Message size limits: large payloads belong in object storage, with the message carrying only a reference.

When consumers cannot keep up, the broker buffers messages, but buffers are finite. Sustained growth in consumer lag requires backpressure handling: scaling consumers, slowing producers or shedding lower-priority traffic before retention limits delete unprocessed data.

Common design mistakes

Many broker problems stem from treating the broker as a simple pipe rather than a stateful system with its own failure modes.

MistakeSymptomFix
Non-idempotent consumersDuplicate orders or double counts after restartsDeduplicate by unique message identifiers
No dead-letter handlingOne malformed message blocks a partitionRoute repeated failures to a dead-letter queue
Unversioned payload schemasConsumers break when producers change fieldsUse a schema registry and compatible evolution
Too few partitionsConsumers cannot scale with volumePlan partitions for expected peak throughput
Unmonitored lagData arrives hours late without alertsAlert on lag thresholds per consumer group
Mistakes, symptoms and fixes

Another frequent mistake is using a broker for synchronous request and response where the caller needs an immediate answer. Brokers excel at asynchronous flows; forcing them into tight request cycles adds latency and complexity without the benefits of decoupling.

In plain words

A message broker works like a post office between computer systems. One system drops off a message. The post office keeps it safe and delivers it to every system that should receive it.

The sender does not have to wait for the receiver to be ready. If the receiver is busy or offline, the message waits in its mailbox. This keeps systems independent, so one slow or broken system does not stop all the others.

A real-world example

An online grocery service processes orders through separate systems for payment, warehouse picking, delivery planning and customer notifications. Originally, the order service called each system directly, and a slow payment provider froze checkout for everyone during evening peaks.

The team introduces a log-based message broker. The order service now publishes an order-placed event to a topic partitioned by order identifier and returns immediately. Payment, warehouse, delivery and notification services each consume the topic independently, with idempotent processing keyed on the order identifier and alerts on consumer lag.

During the next holiday peak, the notification service falls behind by about twenty minutes, but checkout and picking continue unaffected. Later, the analytics team adds a new consumer that replays the last two weeks of retained events to build a demand forecast, without any change to the order service.