Message Broker

Escrito por Darjus Kazakevic · revisado el

A server that stores messages and delivers them, with guarantees you must run.

A message broker is a server between services: RabbitMQ, Kafka, NATS JetStream, Amazon SQS. It accepts a message, writes it to disk or to replicas, and delivers it until a consumer acknowledges it (confirms it is done). That durability is what an in-process bus cannot give.

It is also a system you operate: a cluster to size, upgrade and monitor, consumer lag to watch, dead-letter queues to drain. Message Queue and Pub/Sub, in Core Building Blocks, show how to size and run its two shapes. This guide covers what your consumer code must do: ack at the right time, survive duplicates and route failures.

En esencia — A broker stores messages durably and redelivers them until a consumer acknowledges them, so delivery is at-least-once and every consumer must handle duplicates.

Cuándo recurrir a él

Recurre a él cuando

  • Messages must survive a restart of the sender or the receiver
  • Services need buffering, retries and delivery between them
  • Several services need the same events, each at its own pace

Reconsidéralo cuando

  • Everything runs in one process: an in-process bus is enough
  • Nobody can own the cluster, consumer lag and dead letters
  • The caller needs an answer now: use a direct request

The guarantees

Acknowledgement (ack)
The consumer's signal that a message is done. Ack after the work succeeds, and a crash means redelivery. Ack before the work, and a crash loses the message.
Delivery guarantees
At-most-once may lose a message but never repeats it. At-least-once never loses one but may repeat it, and is the usual default. Exactly-once holds only inside one system, such as Kafka transactions.
Queue or log
RabbitMQ and SQS are queues: a message is removed once acked. Kafka is a log: messages stay for a retention period (7 days by default), and each consumer group keeps its own offset, so it can replay.
Dead-letter queue (DLQ)
Where a message goes after it fails N times, so one bad message does not block everything behind it. Someone must watch it, then fix and replay, or discard.

Applying it well

Do the work, then ack
Acknowledge only after the handler succeeds, so a crash replays the message instead of losing it. That makes duplicates normal under at-least-once delivery, so every consumer must be idempotent.
Give poison messages an exit
A message that always fails is redelivered forever and blocks the queue or partition behind it. Cap the attempts and move it to a dead-letter queue that someone watches.
Limit unacked messages per consumer
Set RabbitMQ's prefetch, for example to 20. Without a limit the broker pushes the whole queue to one consumer: others sit idle, and a crash sends all of it back.
Publish from a database write via an outbox
A broker cannot join your database transaction. Write the message to an outbox table in the same transaction and let a relay publish it, or a crash between the two loses it.

Errores habituales

  • Acking before the work, so a crash silently loses the message.
  • No dead-letter queue, so one bad message is retried forever and blocks the rest.
  • Assuming global order. Kafka keeps order only inside one partition.
  • Publishing inside a database transaction, so a rollback still sends the message.