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.

The producer P sends basic.publish with routing key order.eu.created to the topic exchange orders. Three bindings lead to the queues billing (order.*.created), audit (order.#) and shipping (order.eu.*); all three match and each gets a copy. billing pushes messages to consumers C1 and C2 with basic.deliver; they answer with basic.ack or basic.nack. A rejected message goes from billing to the dead-letter exchange dlx and into billing.dlq
One publish, three copies: the exchange routes by binding key, the queue holds the message until a consumer acks it, and a rejected message goes to the dead-letter queue.

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.

TypeMatch ruleBindings on the page
directBinding key equals the routing keybilling order.eu.created, audit order.us.created, shipping order.eu.shipped
fanoutEvery bound queue; the key is ignoredbilling, audit, shipping
topicWords split on dots; * = exactly one word, # = zero or more wordsbilling 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.

The producer P sends basic.publish m1 with routing key order.eu.created to the topic exchange orders. Three bindings match, so the exchange makes 3 copies; one copy of m1 goes into the queue billing, and audit and shipping get one each. billing holds m1 as Ready 1, then pushes it to consumer C1 with basic.deliver, delivery_tag = 1, and counts it as Unacked 1. C1 handles m1 and sends basic.ack 1; billing deletes m1.
The producer only talks to the exchange; the queue pushes the copy to a consumer and keeps it until the ack, then deletes it.

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.nack with requeue = true: failed, try again. The message goes back to the head of the queue with redelivered = true (drawn as m1*) and is delivered again.
  • basic.nack with requeue = false (or basic.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.

Left, a RabbitMQ queue: m1 is acked and deleted, m2 is unacked at a consumer, m3 and m4 are ready. Fan-out to two services needs two queues with two copies. Right, a Kafka log with offsets 0 to 4: all records stay; group billing has committed offset 2 and group audit offset 4; replay is a seek to an older offset
A queue deletes a message once it is acked (✓ simple work sharing, ✗ no replay); a log keeps it, and each group moves its own offset (✓ replay, ✓ many readers).
RabbitMQ queue (AMQP 0-9-1)Kafka log
After a message is handledDeleted on basic.ackKept until retention.ms; the group commits an offset
RoutingExchange + bindings decide which queues get a copyProducer picks the topic; the key picks the partition
DeliveryBroker pushes, limited by prefetch_countConsumer pulls with poll()
A failed messagebasic.nack: requeue or dead-letter one messageNo per-record nack: skip, retry or write it to a retry topic
ReplayNo: an acked message is goneYes: seek to an older offset
Fan-out to N servicesN queues, N copiesOne 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.

On the mind map

Open the Distributed Systems mind mapDistributedSystemsReplication: copies of data · what reads seeReplicationPartitioning: split data over nodesPartitioningTime and membership: order events, know who is aliveTime and membershipConsensus: agree on one value or logConsensusTransactions: one change across many servicesTransactionsMessaging: send events between systemsMessagingKafka internals: a replicated append-only logKafka internalsRedis internals: one thread, data in memoryRedis internalsKubernetes: reconcile desired stateKubernetesResilience and speed: stay up and fast under loadResilience and speedDelivery semanticsDelivery semanticscommit before or after processingMQTT QoS levelsMQTT QoS levelsQoS 2: PUBREC/PUBREL drop resent copiesCoAP over UDPCoAP over UDPCON resent at 2, 4, 8 s until an ACKIoT Device ShadowIoT Device Shadowdelta = desired keys unlike reportedPush vs pullPush vs pullbroker pushes with prefetch vs pollAMQP in RabbitMQexchange routes by binding; ack deletesChange data captureChange data capturetail the WAL, publish each row changeETL and SCD typesETL and SCD typesType 2 = new row, new key, history keptRedis StreamsRedis Streamsconsumer group, PEL until XACKLog vs queueLog vs queuequeue deletes on ack; log keeps offsetsOpen the Distributed Systems mind mapDistributed Systemssend events between systemsMessagingDelivery semanticsDelivery semanticscommit before or after processingMQTT QoS levelsMQTT QoS levelsQoS 2: PUBREC/PUBREL drop resent copiesCoAP over UDPCoAP over UDPCON resent at 2, 4, 8 s until an ACKIoT Device ShadowIoT Device Shadowdelta = desired keys unlike reportedPush vs pullPush vs pullbroker pushes with prefetch vs pollAMQP in RabbitMQexchange routes by binding; ack deletesChange data captureChange data capturetail the WAL, publish each row changeETL and SCD typesETL and SCD typesType 2 = new row, new key, history keptRedis StreamsRedis Streamsconsumer group, PEL until XACKLog vs queueLog vs queuequeue deletes on ack; log keeps offsets
This page on the Distributed Systems mind map · 104 pages in 10 branches · open it to see the whole map and test yourself in recall mode.