Queue, log and pub/sub
A broker stores a message until a consumer reads it. But what does "reads" mean? Does the message disappear afterwards, do all consumers get it or only one, can a new service read yesterday's messages? Brokers answer differently, and almost all the answers come down to three models.
A courier service's warehouse can also work in different ways. It can put parcels on a shelf and give each one to the first free courier. It can, like a magazine publisher, send a copy to every subscriber. Or it can write down each incoming parcel in a ledger that any department reads.
Queue
A queue is a list of messages from which the broker gives each message to one consumer and deletes it after the acknowledgement. An acknowledgement is the consumer's short reply to the broker: "message processed".
This is the shelf of parcels. A free courier takes the next parcel, and it is gone from the shelf. No parcel goes out with two couriers.
If several consumers read one queue, they share its messages: each message goes to one of them. Such consumers are called competing consumers. To work through the queue faster, it is enough to start one more copy of the consumer.
A queue fits tasks each of which exactly one worker must do: send an email, shrink an image, build a report.
Pub/sub
Pub/sub is the "publish–subscribe" model: a sender publishes a message, and every subscriber gets a copy. The sender does not know how many subscribers it has and waits for none of them.
This is a magazine mailing: the publisher prints an issue, and couriers bring a copy to every subscriber. Sending one message to many receivers is called fan-out.
In its simplest form, pub/sub stores nothing. The message reaches the subscribers that are connected right now. Whoever subscribed later or was down will never get this message.
Pub/sub fits events that interest many services and go stale fast: a new exchange rate, a price change, a "refresh the cache" signal.
Log
A log is a list of messages that is only ever appended to at the end. Each message gets a number, its position in the log, and reading deletes nothing.
This is the warehouse ledger. Each new entry goes on the next numbered line. Accounting, delivery and analytics read one ledger, and each department has its own bookmark. A department can go back a hundred lines and reread them, and a new department can start from the first page.
Each reading service keeps its own position. In Kafka such a reader is a consumer group: the copies of one service share the messages among themselves, while different groups read the log independently.
Here the analogy breaks down: a ledger is kept for years, while a log deletes old messages over time. It deletes them not after reading but by age or size, for example after seven days, even if nobody has read them.
Kafka calls such a named log a topic. The word "topic" comes up at interviews too: unlike a queue, a topic is a sequence of messages that every subscriber gets in full.
The three models side by side
| Model | Who gets a message | What happens to it after reading |
|---|---|---|
| Queue | one of the consumers | deleted after the acknowledgement |
| Pub/sub | every subscriber connected now | in the simplest form, not stored at all |
| Log | every reader, from its own position | kept until the retention period runs out |
Step through the script and switch the model.
A is connected to the queue; B will connect later.
Interviewers like to ask this: "How does a queue differ from a topic?" A queue gives each message to one of the competing consumers and deletes it. A topic gives each message to every subscriber, and each one reads from its own position.
Which model each broker builds on
The brokers of this course grew out of different models:
- Kafka is a log: a topic stores messages, and each consumer group reads it from its own position;
- RabbitMQ is queues: the broker puts copies of a message into queues, one per receiving service, and inside a queue a message goes to one consumer;
- NATS is pub/sub: core NATS passes a message to the subscribers connected now and stores nothing, while JetStream saves messages to a stream that works as a log.
The borders are not strict: each broker can also work by another model, and later in the course you will see how.
Common mistake
Running three copies of the billing service as pub/sub subscribers to charge money faster. Each copy gets every message, and one order is charged three times. Sharing work among the copies of one service is a queue's job: each message goes to only one of its competing consumers.
Course map
- Chapter 2: delivery guarantees — what happens to a message on a failure and how not to process it twice.
- Chapters 3–7: Kafka — topics and partitions, producers, consumer groups, replication, transactions.
- Chapters 8–10: RabbitMQ — exchanges and queues, acknowledgements, failures and retries.
- Chapters 11–12: NATS — subjects and queue groups, then JetStream.
- Chapter 13: patterns around a broker — retries with a pause, outbox, order per key, sagas.
- Chapter 14: how to choose a broker and solve messaging tasks at an interview.
Key points
- A queue gives each message to one of the competing consumers and deletes it after the acknowledgement.
- Pub/sub gives a copy to every subscriber; in its simplest form it stores nothing.
- A log keeps messages after reading: each reader tracks its own position and can reread old ones; in Kafka such a log is called a topic.
- Kafka builds on a log, RabbitMQ on queues, NATS on pub/sub, and JetStream adds a stored stream to NATS.