The idea: publish to an exchange, not to a queue
RabbitMQ speaks AMQP 0-9-1. In AMQP a producer never writes into a queue. It sends basic.publish to an exchange with a routing key, such as order.eu.created. Queues are tied to the exchange by bindings. The exchange looks at the routing key, finds every binding that matches, and puts one copy of the message into each of those queues. Consumers then read from the queues. So the producer does not need to know who listens, and new listeners can be added by adding a binding.
Exchange types and binding keys
The page has one exchange, orders, and three queues bound to it. The exchange type decides how a routing key is compared with a binding key.
| Type | Match rule | Bindings on the page |
|---|---|---|
| direct | Binding key equals the routing key | billing order.eu.created, audit order.us.created, shipping order.eu.shipped |
| fanout | Every bound queue; the key is ignored | billing, audit, shipping |
| topic | Words split on dots; * = exactly one word, # = zero or more words | billing order.*.created, audit order.#, shipping order.eu.* |
With the topic exchange, order.eu.created matches all three bindings, order.us.created goes to billing and audit, order.eu.shipped to audit and shipping, and order.us.shipped only to audit, because order.# catches everything that starts with order (Demo: topic exchange, one key to many queues: audit ends with 4 messages, shipping with 2). Each copy is separate: billing can delete its copy while audit still holds its own.
With the direct exchange, order.us.shipped matches no binding. The message is unroutable and is dropped. The producer is not told, because the mandatory flag is false by default; with mandatory = true the broker sends it back with basic.return (Demo: direct exchange, exact match, unroutable). The fanout demo shows the other extreme: every queue gets every message.
Push with a limit: basic.deliver and prefetch_count
A consumer subscribes with basic.consume. From then on the broker pushes messages to it with basic.deliver, round robin between the consumers of a queue. Each delivery carries a delivery tag, a number that counts up from 1 on that channel; the consumer uses it to say which message it is answering.
A pushed message is unacked until the consumer answers. basic.qos(prefetch_count = 2) tells the broker: never let more than 2 of my messages be unacked. When both consumers are full, new messages wait in the queue as Ready. In Demo: prefetch_count = 2 holds messages back, five messages arrive: C1 gets m1 and m3, C2 gets m2 and m4, and m5 waits. When C1 acks m1, the broker pushes m5 to C1 at once as delivery tag 3.
Settling a message: ack, nack and requeue
The broker keeps a message until the consumer settles it. There are three answers:
basic.ack: done. The broker deletes the message. A queue keeps nothing after an ack.basic.nackwithrequeue = true: failed, try again. The message goes back to the head of the queue withredelivered = true(drawn as m1*) and is delivered again.basic.nackwithrequeue = false(orbasic.reject): failed, do not try again. The message is dead-lettered.
A queue declared with the argument x-dead-letter-exchange = dlx republishes its dead messages to the exchange dlx, with an x-death header that says why (reason = rejected), from which queue, and how often. Here dlx is a fanout exchange bound to billing.dlq, where a person or a repair job can look at the message later (Demo: nack, requeue, then dead-letter: m1 is requeued once, comes back to C1 as delivery tag 2, and then ends in billing.dlq). Messages that expire (x-message-ttl) or overflow a length limit are dead-lettered the same way.
Requeueing a message that always fails makes a hot loop: deliver, fail, requeue, deliver again. Use requeue = false and a dead-letter queue for messages that cannot succeed.
A consumer crash: at-least-once delivery
If a consumer dies, its TCP connection closes and with it the channel. The broker then requeues every message that was delivered on that channel but not acked, with redelivered = true, and pushes them to the remaining consumers. Nothing is lost. But the dead consumer may have done part of the work (charged a card, sent an email) before it died, so the next consumer can do it a second time. That is at-least-once delivery: handlers must be idempotent, for example by remembering which order ids they have already handled. In Demo: consumer crash, redelivery to C2, C1 dies holding m1 and m3; C2 gets m1 at once and m3 after it acks m2, because it has only 2 prefetch slots.
With auto-ack (no-ack = true on basic.consume), the broker deletes a message as soon as it sends it. That is faster, but a crash then loses every message in flight: at-most-once.
RabbitMQ queues vs a Kafka log
A RabbitMQ queue and a Kafka topic both carry messages, but they store them in opposite ways. A queue is a to-do list: a message leaves it when it is acked. A Kafka partition is a log: records stay for the retention time whether anyone read them or not, and each consumer group only moves its own offset.
| RabbitMQ queue (AMQP 0-9-1) | Kafka log | |
|---|---|---|
| After a message is handled | Deleted on basic.ack | Kept until retention.ms; the group commits an offset |
| Routing | Exchange + bindings decide which queues get a copy | Producer picks the topic; the key picks the partition |
| Delivery | Broker pushes, limited by prefetch_count | Consumer pulls with poll() |
| A failed message | basic.nack: requeue or dead-letter one message | No per-record nack: skip, retry or write it to a retry topic |
| Replay | No: an acked message is gone | Yes: seek to an older offset |
| Fan-out to N services | N queues, N copies | One log, N consumer groups |
See also Push vs Pull Messaging: RabbitMQ vs Kafka for prefetch against a slow consumer, Log vs Queue for ordering and replay side by side, How Kafka Moves a Message for the log path end to end, and Delivery Semantics for at-most-once, at-least-once and exactly-once.
What the page leaves out
One exchange at a time, one channel per client, and consumers only on billing. It leaves out the AMQP frames under each method (connection.start, channel.open, content header and body frames), exchange.declare / queue.declare / queue.bind calls, headers exchanges, the default exchange (every queue is bound by its own name), publisher confirms (confirm.select, basic.ack from broker to producer), multiple = true acks, basic.get, consumer priorities, durable and persistent messages, quorum queues (and their x-delivery-limit), streams, and the fact that RabbitMQ requeues a message at its old position, which is the head only when it was the oldest. Steps are a teaching clock, not real timing.