Publisher Confirms
Knowing a message was accepted, persistence, and what fire-and-forget hides
Consumer acks protect messages on the way out. Publisher confirms
protect them on the way in. Without confirms, basic_publish writes
bytes to a socket and returns. The publisher has no idea whether the
message was routed, stored, or refused.
Turning confirms on
ch.confirm_delivery() # this channel is now in confirm mode
ch.basic_publish(...) # the broker replies with basic.ack (stored) or basic.nack (refused)For a quorum queue, the broker confirms after a majority of replicas have written the message. For a classic durable queue with a persistent message, after it's written to disk. A confirm is the broker's promise that it has taken responsibility for the message.
What fire-and-forget hides
labs/reliability.py publishes 20 messages to a quorum queue limited to 10
(x-max-length: 10, x-overflow: reject-publish):
2. publishing 20 messages to a queue limited to 10 (overflow=reject-publish)
burst, no confirms: queue holds 20 (quorum queues apply the limit asynchronously: a burst can overshoot)
steady, no confirms: queue holds 11; the publisher saw no error for the 9 it lost
with confirms: 9 publishes were nacked; the publisher knows exactly whichThree findings in one experiment:
- Without confirms, refused messages vanish silently. At a steady rate the queue kept 11 and refused 9, and the publisher saw no error at all.
- With confirms, each refusal is reported (a
NackErrorin pika), so the publisher can retry, slow down, or alert. - Quorum-queue limits are approximate. A fast burst of 20 was all
accepted, because the leader applies
max-lengthasynchronously. Treat length limits as a safety net, not an exact cap.
The cost, and how to pay less
1. publishing 2000 persistent messages to a quorum queue
no confirms: 38,543 msg/s (no idea if they arrived)
confirm each (sync): 833 msg/s (every message acknowledged)Waiting for each confirm before the next publish is about 45x slower
here, because every message pays a full round trip plus Raft replication.
That's a property of synchronous publishing, not of confirms: pika's
BlockingChannel waits for each confirm. Clients that keep many publishes
in flight and handle confirms asynchronously (aio-pika, the Java, Go,
and .NET clients) get close to the no-confirm rate with full safety:
Persistence
Both are needed for a classic queue's messages to survive a broker restart. Quorum queues and streams are always durable and always persist messages. Persistence alone isn't enough: without confirms, a message can be lost between the socket and the disk.
mandatory: routed, not just accepted
A confirm says the exchange accepted the message, which is true even if
it matched no binding and was dropped. Add mandatory=True so unroutable
messages are returned instead (see
Exchange Types). Confirms plus
mandatory together mean "stored in at least one queue."
End-to-end at-least-once
The full chain: publisher confirms (and mandatory) so nothing is
lost going in, quorum queues so nothing is lost if a node dies,
manual consumer acks after processing so nothing is lost going out, and
idempotent consumers because each link retries, so duplicates happen.
Leave out any one and "reliable" stops being true somewhere. It's the same
checklist as Kafka's, with different names.
Lab
- Run
reliability.py. - Publish to the pre-loaded
ordersexchange with an unmatched routing key, with confirms but withoutmandatory. It's confirmed. Addmandatoryand see theUnroutableError. - Write an async publisher with aio-pika (or Java) that keeps 500 messages in flight, and compare its confirmed throughput with the sync numbers.
- Stop two of three nodes, then publish with confirms to a quorum queue. What happens without a majority?