How does RabbitMQ handle split-brain scenarios, and what to do?

How a two-second network partition turns into a split-brain

Network: I’ll only be a couple seconds!
RabbitMQ: *panic*

A network partition

Your network is managed by network engineers, yours, or the MSP’s. Your hypervisor, your virtual machine, your Docker swarm or Kubernetes clusters, by different teams.

You sit on top of those stacks, installing your messaging clusters. You see scoping, and like the arrangement: letting the experts work their field! Yours is configuring RabbitMQ, so that it can survive the worse.

Your RabbitMQ looks at you, and thumps, loudly: They’ve done it again! Are you prepared?

  • A VM live migration,
  • A switch upgrade,
  • A network cable unplugged, a fibre going down,
  • A firewall, slightly too eager.

This is a non-exhaustive list of reasons your node might think the network failed. Now, there are two cases: The node lost network fully, or not.

A network, unplugged

RabbitMQ’s least favourite song!

Whether for a few seconds or hours, the network going down is always a source of partitions, and that means worry, on-calls, and a bad night of sleep. Appropriate configuration, adapted topology and a modern RabbitMQ version will have you with your ears down in no time!

Detection

The net_ticktime is the main detector: nodes stay in contact all the time, through normal messages, and heartbeats. Nodes should exchange 4 messages per net_ticktime period, after which nodes are excised from the running node list. With the 60 seconds default, that’s 45-75 seconds from incident to detection. Still, we do not recommend reducing that value – while it would produce a faster split detection, the cost of dangerous false positives is too high.

The TCP connections getting disconnected are also used as an indicator of partition: a FIN or RST TCP message will cut the connection, which will initiate a reconnection attempt. Sometimes, your firewall, eager to cut on carrot costs, will reset a connection! Intra-cluster stream replication connections, for instance, have no heartbeat by default, so they can stay fully idle for a long time, and get blocked waiting for a TCP timeout when traffic comes back.

Finally, the aten library provides early detection for ra. Leadership change is not as loud as a full node cutover, and a leaderless quorum queue is not functional. Therefore, forcing an early, perhaps-extraneous, election, a few milliseconds of work, is more beneficial than waiting for a full decision on the node status tens of seconds later.

Those latter two mechanisms can act way quicker than the base timeout, meaning even sub-second interruptions can become trouble.

Full network unavailability

This is the best case scenario: Your VM loses contact with the outside world, RabbitMQ is fully cut away from its cluster, but also from the consumers, producers, and your orchestration infrastructure. It will sit, idling safely, until it rejoins the cluster once networking capabilities are re-established.

Partial network unavailability

The inter-rack link just disappeared, a switch crashed, and now, RabbitMQ nodes 1 and 3 on side A are talking behind node 2’s back on side B! And both are still connected to some clients? And your IaC is about to run, and reaches all 3 nodes?! This is the recipe for disaster IT books warn you about.

Asymmetric partitions

Now, node 1 sees node 2 and 3, but node 3 disagrees: no signs of node 2. Where even is side A, or side B? This is the most devious partition: no clean cut, nodes disagreeing, clients connected everywhere.

Until version 4.2 and Khepri, the RabbitMQ nodes had an innate ability to turn such a partition into a clean partition, through an internal detection. With RabbitMQ 4.3, the behaviour has changed, the cluster will try to maintain availability, and only the distribution link messages that have to reach a leader on the other side of the partition will either time out, or get sent as soon as possible after the partition resolves.

Doing the network split

Why is it such a bad thing? Well, today, a well-meaning engineer found a half-dead fibre on a cross-rack bond during a routine port isolation: It has been causing issues for weeks, the transceiver was losing your packets! It should not have cut the link, but it went down, just an instant.

Now, your cluster is split: 1 & 3 on side A, 2 on side B: neither are in full network isolation. On side A, clients A1 and A2 are connected, to node 1 & 3, publishing and consuming a queue. On side B, your IaC just connected to node 2, and ordered that queue purged.

The issue is finally noticed, within seconds, a few hundreds messages, a few metadata operations later: the partition resolves, what is the state of the queue?

This is a typical split-brain scenario, and there are ways to make RabbitMQ more resilient to them.

Safety first

Side A and B are currently disagreeing on what the queue contains, what if you created, deleted queues? What about consumers getting stuck not seeing the messages flowing on the other side? The situation is getting more and more hypothetical, because RabbitMQ protects itself (or, tries to). And you, as the operator, are tasked to configure it as best as possible to avoid issues:

The Creation of Khepri (& PauseR)

In version 4.3, RabbitMQ removed the old Erlang metadata store, Mnesia, and replaced it with the Raft-backed Khepri. As part of that change, the old cluster_partition_handling parameters have been effectively deprecated, and partition handling is fully delegated to the Raft algorithm instead. The upside is that many operations are now gated behind the quorum, a split-brain becomes nearly impossible to achieve. Despite its usefulness, we do not recommend enabling the Khepri feature flag before version 4.2.0, where it becomes a stable feature.

There are still cases where you want the old pause_minority behaviour back, and Seventh State has just the right plugin: PauseR .

Mnesia & split-brain

Prior to RabbitMQ 4.2, Mnesia was the default metadata store, and it is not Raft based! It comes with a set of settings to control the behaviour of nodes when a network partition happens.

cluster_partition_handling: ignore

The fact that there is a partition in progress is fully ignored… until the partition resolves! Then, reconciliation is about hope: this is a prime setting to have your cluster go into a split-brain situation, leading to data loss! With this setting, partition recovery becomes a mandatory operator intervention: pick which side is the source of truth who keeps its data, restart the other, who loses it – until then, the partition is still active! This can, very bluntly, lead to bad data loss: metadata, messages, runtime configuration.

cluster_partition_handling: pause_minority

Our recommendation for odd node count clusters. The partition has to resolve to a situation where a node is in minority, and we can just power the rabbit down until it’s resolved! The drawback is: one node or more will go down, and be unavailable for the duration of the partition, which can leave your whole cluster unavailable. In an even node count cluster, it is prone to making the whole cluster unavailable, as both sides can end up in minority very easily.

cluster_partition_handling: autoheal

Whenever the partition resolves and all nodes are back together, a winner is chosen by the client connections count, and every other node discards their state. This is ignore on auto-pilot, same risks, slightly better availability.

Common partition Issues

  • Needing to re-synchronise large amounts of data: an out-of-sync replica needs to catch up
  • Needing to reconnect the clients to recover from specific bugs
  • Leader & client connection imbalance
  • Reconnection storms
  • Message duplication and re-deliveries
  • Split-brain: metadata or message loss

What Raft didn’t solve

Classic mirrored queues, removed in RabbitMQ 4.0, are quite susceptible to split-brains, and never benefited from Raft. For a node-local solution, classic (non-mirrored) queues are perfect, as long as the path from the client to the queue host works. For your replicated needs, the Raft-backed quorum queue is your best option! It is immune to split-brains: the orders get delayed until quorum can be achieved, or a timeout is reached.

Workload spread is still not solved: leadership load, client connections and replicas are not spread optimally. With AMQP, the client doesn’t connect directly to the leader, ra dealing with forwarding messages. Leader rebalance is not automated, and replicas are not evenly spread for availability.

Election storms, when a node goes down, the leadership has to be redistributed to the remaining nodes, all at once. While that is a weak point, the expectation is that an election is a small enough event that the disruption is minimal.

at-most-once vs at-least-once: at-most-once can lead to message loss, using systems like auto-ack and no publishing confirmation. We recommend striving for at-least-once, as it guarantees message safety, the cost being message duplication: publish confirm, mandatory flag, and manual ack of persistent messages sent to replicated queues, with alternate exchange (AE) to collect undelivered messages. Dead-lettering (DLX/DLQ) completes the message safety rounds by ensuring dropped messages are not unduly discarded.

Protecting a cluster

Raft is a consensus protocol, it is the first line of protection: a fully Raft-backed cluster, from Khepri to quorum queues, RabbitMQ 4.2+ ensures your cluster is in an optimal position.

One of the main hurdles with consensus algorithms is the requirement for a quorum, that is best achieved with an odd number of nodes. Even number of nodes can create a situation where no leader is able to be elected.

Network resilience is very important for RabbitMQ. It thrives on low-latency, stable links, and monitoring those at a network level is important. The best protection you can get against a partition is it never happening! The second best you can get is failure simulation: break your cluster on purpose, and see what happens to your clients. For when RabbitMQ gnaws at your (virtual) network cable too much, look at Seventh State SafeConX.

Placement is also an important topic: different hosts, in different racks, with anti-affinity and disruption budget. Your VMs should also never use live migration (sometimes known as vMotion, XenMotion) on a running RabbitMQ.

This leads us to the last piece of the puzzle: the producers and consumers. As RabbitMQ, with at-least-once guarantees, can lead to creation of duplicate messages, producers and consumers should strive to be idempotent. This is, however, generally outside of the scope of the RabbitMQ operator.

The safest option after a steady network: adequate configuration, version upgrades (and plenty of carrots, in .png format!) will ensure your RabbitMQ cluster is at its safest from split-brain scenarios.