RabbitMQ
RabbitMQ from the service's side: exchanges, queues and bindings, routing and unroutable messages, acks and prefetch, publisher confirms, quorum queues, dead-lettering with delayed retries, and streams.
10 questions
JuniorTheoryVery commonWhat do ack, nack with requeue and reject do to a delivered message?
What do ack, nack with requeue and reject do to a delivered message?
ack marks the message processed, and the broker deletes it. nack with requeue puts it back, flagged redelivered. reject settles one message; on a quorum queue its requeue also raises x-delivery-count, and nack's does not. Without requeue, both drop it or dead-letter it.
Common mistakes
- ✗Acking on receipt, before the work is done: if the consumer then crashes, the broker has already deleted the message
- ✗Requeuing a message that always fails: by default it comes straight back and the consumer loops on it, and on a quorum queue a
nackwith requeue does not count toward the delivery limit
Follow-up questions
- →What happens to unacknowledged messages when a consumer's channel closes?
- →Why does RabbitMQ add
nackwhen AMQP 0-9-1 already hasreject?
JuniorTheoryVery commonWhat do an exchange, a queue and a binding each do in RabbitMQ?
What do an exchange, a queue and a binding each do in RabbitMQ?
A producer publishes to an exchange, which stores nothing and only routes the message to queues by its bindings. A queue holds messages until a consumer takes and acknowledges them. A binding is the rule that links an exchange to a queue; an exchange can have many.
Common mistakes
- ✗Thinking a producer writes straight into a queue: it always publishes to an exchange, and even a publish by queue name goes through the default exchange
- ✗Expecting an exchange to keep messages: if no binding matches, the message has nowhere to stay and is dropped
Follow-up questions
- →How does the default exchange route a message published with a queue's name as its key?
- →What happens when two bindings send the same message to one queue?
JuniorTheoryVery commonHow do direct, fanout, topic and headers exchanges choose queues?
How do direct, fanout, topic and headers exchanges choose queues?
Direct sends to queues whose binding key equals the routing key. Fanout ignores the key and sends a copy to every bound queue. Topic matches dot-separated words, where * is exactly one word and # is zero or more. Headers ignores the key and compares message headers.
Common mistakes
- ✗Swapping the topic wildcards:
*matches exactly one word and#matches zero or more, soa.#matchesabuta.*does not - ✗Expecting fanout to spread messages among queues: it sends a copy to every bound queue, and only the consumers of one queue share its messages
Follow-up questions
- →Why does
a.#match the routing keyawhilea.*does not? - →What do
x-match=allandx-match=anychange in a headers exchange?
MiddleTheoryCommonIn a retry loop of a TTL queue and dead-lettering, how does a failed message wait and return to work?
In a retry loop of a TTL queue and dead-lettering, how does a failed message wait and return to work?
The consumer rejects a failed message without requeue, and the work queue dead-letters it to a retry queue with x-message-ttl and no consumers. When the TTL ends, that queue dead-letters it back to the work exchange; x-death counts rounds, and past a limit it goes to a parking lot.
Common mistakes
- ✗Using one retry queue with different per-message TTLs: a message expires only at the head, so a long delay holds up shorter ones behind it
- ✗Counting retries with the
redeliveredflag: it only says the message was delivered before; the number of rounds is thecountin thex-deathentry
Follow-up questions
- →Why does a separate retry queue per delay avoid head-of-line blocking?
- →Which
x-deathentry tells you how many times processing failed?
MiddleTheoryCommonHow does a message reach a dead-letter exchange, and what does x-death record?
How does a message reach a dead-letter exchange, and what does x-death record?
A queue with x-dead-letter-exchange republishes a message there when it is rejected without requeue, it expires, a drop-head queue (the default) overflows or a quorum queue hits its delivery limit. x-death lists one entry per queue and reason with a count, the newest first.
Common mistakes
- ✗Expecting a
nackwith requeue to dead-letter the message: only a refusal without requeue, an expired TTL, an overflow or the delivery limit does - ✗Counting
x-deathentries to get the number of retries: a repeat of the same queue and reason incrementscountin the existing entry
Follow-up questions
- →Why can a quorum queue lose a dead-lettered message on its way by default?
- →How does
x-dead-letter-routing-keychange where the message lands?
MiddleTheoryCommonWhat does prefetch limit, and what goes wrong without it?
What does prefetch limit, and what goes wrong without it?
Prefetch (basic.qos) caps how many unacknowledged deliveries the broker sends one consumer. Without it the broker pushes messages as fast as it can: the first consumer to subscribe takes the backlog into memory, later ones get only new messages, and its crash requeues it all.
Common mistakes
- ✗Reading prefetch as a rate limit: it caps unacknowledged deliveries per consumer, so a consumer that acks quickly still gets messages at full speed
- ✗Leaving prefetch unset with several workers: the first one to subscribe takes the backlog, and the others idle until it acks
Follow-up questions
- →How do you choose a prefetch value for slow and for fast handlers?
- →What does a per-channel
globalprefetch mean, and what does RabbitMQ 4.4 do with it?
MiddleTheoryCommonWhat do publisher confirms guarantee, and what does multiple mean?
What do publisher confirms guarantee, and what does multiple mean?
In confirm mode the broker numbers a channel's publishes from 1 and acks each once it has taken responsibility: for a quorum queue, after a majority of replicas wrote it. An ack with multiple confirms every tag up to and including its own. A confirm says nothing about consumers.
Common mistakes
- ✗Treating a confirm as proof of processing: it means only that the broker took responsibility, having stored the message or found it unroutable
- ✗Handling a
multipleack as one tag: it confirms every outstanding tag up to and including the one it carries
Follow-up questions
- →What should a producer do when a publish comes back as a
nack? - →Why is waiting for each confirm one by one slower than tracking them in batches?
MiddleTheoryCommonWhy are quorum queues the recommended replicated queue type in RabbitMQ 4, and what do they cost?
Why are quorum queues the recommended replicated queue type in RabbitMQ 4, and what do they cost?
A quorum queue keeps replicas on several nodes and commits each message through Raft, so it survives losing a minority; classic mirrored queues were removed in 4.0. The cost: every write waits for a majority, a queue without one stops, and each replica keeps its own copy, so disk and network use grow.
Common mistakes
- ✗Still planning classic mirrored queues for replication: RabbitMQ 4.0 removed them, and quorum queues or streams replace them
- ✗Expecting a quorum queue to work with any number of nodes down: without a majority of its replicas it stops accepting writes
Follow-up questions
- →What does the quorum queue delivery limit do with a message that keeps failing?
- →How many replicas does a quorum queue need to survive two failed nodes?
MiddleTheoryCommonWhat happens to a message no binding matches, and how do you keep it?
What happens to a message no binding matches, and how do you keep it?
By default the exchange drops it silently, and in confirm mode the ack still arrives. Publish with mandatory and the broker returns the message to the producer with basic.return; or give the exchange an alternate exchange, which receives every message it cannot route.
Common mistakes
- ✗Treating a publisher confirm as proof that a queue got the message: the broker also acks a message it dropped as unroutable
- ✗Expecting an unroutable message to reach a dead-letter exchange: dead-lettering starts from a queue, and the message never got into one
Follow-up questions
- →In what order do
basic.returnand the confirm arrive for amandatorymessage? - →How do you set an alternate exchange with a policy rather than a declaration argument?
MiddleTheoryOccasionalHow does a RabbitMQ stream differ from a queue?
How does a RabbitMQ stream differ from a queue?
A stream is an append-only log: reading deletes nothing, and each consumer picks where to start with x-stream-offset (first, last, next, an offset or a time). Many consumers can reread the same messages, and old ones go only by size or age retention.
Common mistakes
- ✗Expecting an ack to delete a message from a stream: messages stay until size or age retention removes them, whoever has read them
- ✗Thinking a late consumer misses old messages: it can start from
first, an offset or a timestamp withx-stream-offset
Follow-up questions
- →Why does an AMQP 0-9-1 consumer of a stream need a prefetch value?
- →When would you pick a stream over a quorum queue for fan-out to many readers?