Flow Control & Limits
Memory and disk alarms, blocked publishers, queue length limits, and backpressure
When consumers fall behind, messages pile up, and something has to give. RabbitMQ protects itself in layers: length limits on individual queues, per-connection flow control, and, as a last resort, resource alarms that stop every publisher in the cluster.
Resource alarms
Two thresholds, set in config/rabbitmq.conf:
When either is crossed on any node, RabbitMQ blocks every publishing connection in the cluster until it clears. Consumers keep working, which is what lets the backlog drain.
Triggered on purpose in this lab by lowering the watermark:
docker exec rabbit-1 rabbitmqctl set_vm_memory_high_watermark 0.0001
docker exec rabbit-1 rabbitmq-diagnostics -q alarms
Memory alarm on node rabbit@rabbit-1
# a publisher that registered a callback is told why:
conn.add_on_connection_blocked_callback(lambda c, m: print(m.method.reason))
blocked callback: ['low on memory']
docker exec rabbit-1 rabbitmqctl set_vm_memory_high_watermark 0.6 # restore
Node rabbit@rabbit-1 reported no alarms, local or clusterwideA blocked connection doesn't error: the publish call simply hangs. An
application without a connection.blocked handler and without publish
timeouts looks frozen, threads pile up, and HTTP requests that publish
start timing out, far from the actual cause. Register the callback, log
it loudly, and put a timeout on publishing. The RabbitMQResourceAlarm
alert pages for exactly this.
set_vm_memory_high_watermark changes the threshold at runtime only; the
config file value returns on restart. In containers, RabbitMQ detects the
cgroup memory limit (mem_limit: 1g here) and calculates the watermark
from that.
Per-connection flow control
Before alarms, there's a gentler mechanism: if a queue can't keep up with a
publisher, RabbitMQ slows that publisher's connection using internal
credit (the connection shows state flow in the UI). It's automatic and
mostly invisible, a sign that something downstream is the bottleneck.
Queue length limits
Bound individual queues so one runaway producer can't fill the node:
| Overflow | Good for |
|---|---|
drop-head | Latest-value feeds where old messages are worthless (prices, sensor readings) |
reject-publish | Work that must not be silently lost: publishers see nacks and back off |
The lab-limits policy in definitions.json caps every lab.* queue at
100,000 messages with reject-publish. As
Publisher Confirms showed,
quorum queues enforce limits asynchronously, so a fast burst can
overshoot, and without confirms the publisher never learns which messages
were refused.
Message TTL
Expired messages are dropped, or dead-lettered with reason expired if
the queue has a DLX, the basis of delayed retries.
Backpressure, end to end
RabbitMQ works best when queues stay short. A long queue means
consumers are behind, latency for every new message is high, and the node
holds more memory and disk. Design for it explicitly: scale consumers on
queue depth (Kubernetes KEDA can do this), bound queues, make publishers
handle nacks and blocking by slowing down or shedding load, and alert on
depth that keeps growing (RabbitMQMessagesPilingUp).
Lab
- Reproduce the memory alarm with
set_vm_memory_high_watermark 0.0001. Runreliability.pywhile it's active and watch it hang. Restore the watermark and watch it continue. - Declare a queue with
x-max-length: 5and default overflow, publish 10, and read what's left. Which 5 survived? - Repeat with
reject-publishand publisher confirms. - Set a policy
message-ttl: 5000ontmp.*queues, publish totmp.test, and watch the messages expire in the UI.