Message Queue and Broker in Distributed Systems
A message broker (or message queue) is dedicated middleware that accepts messages from producers, durably buffers them, and delivers them asynchronously to consumers. By placing a broker between services, you eliminate temporal coupling — producers keep responding quickly even when downstream workers are slow, scaling up, or temporarily offline.
(For the conceptual side of message payloads — Command, Event, and Document messages — see the Messaging section. This guide focuses on the broker infrastructure itself.)
Two Core Broker Architectures
Most production message brokers fall into one of two models: a traditional task queue (messages are deleted once acknowledged) or a distributed append-only log (messages stay retained on disk and each consumer group tracks its own offset):
- Point-to-Point Queue (Competing Consumers): Multiple workers pull from the same queue, and each message is processed once by whichever worker claims it first. Once the worker sends an acknowledgment (
ACK), the broker removes the message. Ideal for background jobs (thumbnail generation, sending emails, invoice PDFs). - Pub/Sub Partitioned Log (Event Streaming): Producers append events to an immutable, partitioned log (topic). Multiple independent consumer groups (e.g., Billing, Search Indexer, Analytics) each receive every event and track their own read position (offset). Because messages are retained for days or weeks, a new service or bug fix can replay historical events from offset
0.
Kafka vs. RabbitMQ vs. Amazon SQS
| Dimension | Apache Kafka / Redpanda | RabbitMQ | Amazon SQS (+ SNS) |
|---|---|---|---|
| Core model | Distributed partitioned append-only log | Smart broker with exchanges & routing queues | Fully managed cloud queue (pair with SNS for fan-out) |
| Message retention | Retained for configurable time/size (replayable) | Deleted once acknowledged by consumer | Deleted once acknowledged (up to 14 days max wait) |
| Ordering guarantee | Strict order within a partition (by partition key) | FIFO per single queue (single consumer) | Best-effort (Standard) or strict FIFO (.fifo queues) |
| Throughput | Millions of msgs/sec horizontally across partitions | Tens of thousands of msgs/sec per node | Virtually unlimited managed scale (Standard) |
| Best fit | Event streaming, CDC, audit logs, high-volume pipelines | Complex routing rules, per-message TTL, task queues | Serverless / AWS teams wanting zero broker ops |
Delivery Semantics and Idempotency
Because networks drop packets and workers crash mid-task, brokers offer three delivery guarantees:
- At-Most-Once: Message is marked done before processing finishes. Fast, never duplicates, but crashes lose messages (acceptable for non-critical metrics).
- At-Least-Once (Standard Default): Broker redelivers any unacknowledged message after a visibility timeout. Zero data loss, but duplicates will happen when a worker finishes work and crashes right before sending
ACK. - Effectively-Once (At-Least-Once + Idempotent Consumer): In practice, distributed systems achieve safe “exactly-once” outcomes by combining at-least-once delivery with an idempotent consumer that deduplicates using a unique
messageId(stored in a database unique constraint or Redis cache).
Operational Safeguards You Need in Production
- Dead-Letter Queues (DLQs): If a malformed “poison pill” message crashes a worker 5 times in a row, retrying forever blocks healthy traffic. Configure a max-retry threshold that routes failing messages into a separate DLQ for inspection and replay.
- Backpressure & Consumer Lag Monitoring: A queue absorbs traffic spikes (load leveling), but if producers permanently outpace consumers, lag grows until disk fills or SLAs break. Alert on consumer lag (or oldest-message age) and autoscale consumers up to the partition count.
- Transactional Outbox: Writing to a database and publishing to a broker are two separate systems — if the process crashes in between, they drift out of sync. Use the Outbox pattern (covered in Temporal Coupling and Distributed Transactions) so both happen atomically.