Last modified: March 23, 2026
This article is written in: 🇺🇸
Message queues enable asynchronous, decoupled communication in distributed systems by allowing publishers to send messages to a queue that consumers process independently, typically in first-in, first-out order. This approach reduces direct dependencies between services, enhances reliability and scalability, and supports multiple publishers and consumers. Additionally, the publish-subscribe pattern uses message brokers to distribute messages to interested subscribers without requiring publishers to manage subscriptions. The following notes include diagrams and best practices for effectively implementing message queues and pub-sub systems.
A message queue is a fundamental building block for asynchronous, decoupled communication in distributed systems. Publishers send messages to a queue, and one or more consumers retrieve those messages in a controlled fashion—often in first-in, first-out (FIFO) order—to process them independently. Below are expanded notes and diagrams illustrating message queues, along with the publish-subscribe pattern.
Basic Message Queue
+----------------+ +----------------+
| | | |
| Publisher | | Consumer |
| | | |
+-------+--------+ +--------+-------+
| ^
| (Send Message) |
v |
+-------+-------------------------------------------+-------+
| |
| Message Queue |
| |
+-----------------------------------------------------------+
(FIFO Retrieval)
Multiple Publishers and Consumers
+----------------+ +----------------+
| | | |
| Publisher A | | Publisher B |
| | | |
+-------+--------+ +--------+-------+
| |
| (Push Msg) | (Push Msg)
v v
+-------+-----------------------------------+-------+
| |
| Message Queue |
| |
+--------+------------------+--------------+--------+
^ ^ ^
| | |
+--------+-------+ +------+-------+ +---+------------+
| | | | | |
| Consumer 1 | | Consumer 2 | | Consumer 3 |
| | | | | |
+----------------+ +--------------+ +----------------+
In the pub-sub paradigm, publishers send messages on a topic (or channel) without needing to know who subscribes. Subscribers register interest in one or more topics, receiving all relevant messages without interfering with other subscribers.
ASCII DIAGRAM: Basic Pub-Sub
+------------------+
| |
| Publisher |
| |
+--------+---------+
|
(Publish to Topic)
v
+--------+---------+
| |
| Message Broker |
| |
+--------+---------+
|
+------------+------------+
| |
v v
+-------+--------+ +-------+--------+
| | | |
| Subscriber 1 | | Subscriber 2 |
| | | |
+----------------+ +----------------+
Multiple Topics & Subscribers
+------------------+
| |
| Publisher |
| |
+--------+---------+
|
(Publish: TopicA, TopicB)
v
+--------+---------+
| |
| Pub-Sub |
| Broker |
| |
+----+--------+----+
/ \
/ \
v v
+---------+--------+ +----+--------------+
| | | |
| Topic A | | Topic B |
| Subscribers | | Subscribers |
| | | |
+------------------+ +-------------------+
| Aspect | Message Queue | Pub-Sub |
| Delivery | Each message consumed by one consumer | Each message delivered to all subscribers |
| Coupling | Producer aware of queue; consumer pulls | Publisher unaware of subscribers; broker fans out |
| Ordering | Typically FIFO within a single queue | Ordering per-topic or per-partition, varies by broker |
| Scaling | Add consumers to share the workload | Add subscribers without affecting others |
| Use case | Task distribution, job processing | Event notification, broadcasting |
| Backpressure | Queue depth grows; consumers throttle naturally | Slow subscribers may miss messages or need buffering |
| Persistence | Messages removed after consumption | Messages can be retained for replay (e.g., Kafka) |
Delivery guarantees define how many times a message is processed by a consumer and are critical for designing reliable distributed systems.
Delivery Semantics Overview
Producer Broker Consumer
| | |
|--- send (fire&forget) ->| |
| (at-most-once) |--- deliver once ------->|
| | (may be lost) |
| | |
|--- send + wait ack ---->| |
| (at-least-once) |--- deliver + retry ---->|
|<------ ack -------------|<------ ack -------------|
| | (may duplicate) |
| | |
|--- send transactional ->| |
| (exactly-once) |--- deliver + dedup ---->|
|<------ ack -------------|<------ ack -------------|
| | (no loss, no dups) |
A consumer group allows multiple consumers to cooperate on processing messages from the same topic or queue, with each partition or message assigned to exactly one member of the group.
Consumer Group with Partitioned Topic
+------------------+
| Producer |
+--------+---------+
|
(Publish to Topic)
v
+--------+---------+---------+---------+
| Partition 0 | Partition 1 | Partition 2 |
+-------+-------+-------+-------+-------+-------+
| | |
v v v
+-------+------+ +------+-------+ +-----+--------+
| Consumer A | | Consumer B | | Consumer C |
| (Group X) | | (Group X) | | (Group X) |
+--------------+ +--------------+ +--------------+
A dead-letter queue (DLQ) captures messages that fail processing after a configured number of retry attempts, preventing poison messages from blocking the main queue.
Dead-Letter Queue Flow
+----------------+ +-------------------+ +----------------+
| | | | | |
| Producer +-------->| Main Queue +-------->| Consumer |
| | | | | |
+----------------+ +--------+----------+ +-------+--------+
| |
(Max retries (Processing
exceeded) fails)
| |
v |
+--------+----------+ |
| |<----------------+
| Dead-Letter Queue | (Nack / Reject)
| |
+--------+----------+
|
v
+--------+----------+
| |
| Alert / Manual |
| Investigation |
| |
+-------------------+
| System | Model | Delivery Guarantee | Ordering | Best For |
| Kafka | Log-based | At-least-once / Exactly-once | Per-partition | High-throughput event streaming |
| RabbitMQ | Broker-based | At-least-once | Per-queue FIFO | Complex routing, RPC patterns |
| Amazon SQS | Managed queue | At-least-once / Exactly-once (FIFO) | Best-effort / FIFO | Serverless task processing |
| Google Pub/Sub | Managed pub-sub | At-least-once | Per-ordering-key | Global event distribution |
| Apache Pulsar | Unified | At-least-once / Exactly-once | Per-partition | Multi-tenant, geo-replicated messaging |
| Redis Streams | In-memory log | At-least-once | Per-stream | Low-latency lightweight messaging |