Clustering and Network Partitions
RabbitMQ clusters do not tolerate network partitions well. If you are thinking of clustering across a WAN, don’t. You should use federation or the shovel instead.
However, sometimes accidents happen. This page documents how to detect network partitions, some of the bad effects that may happen during partitions, and how to recover from them.
RabbitMQ stores information about queues, exchanges, bindings etc in Erlang’s distributed database, Mnesia. Many of the details of what happens around network partitions are related to Mnesia’s behaviour.
叢集和網絡分區
RabbitMQ叢集并不能很好的“忍受”網絡分區。如果你想将RabbitMQ叢集建立在廣域網上,記住那是行不通的,除非你使用federation或者shovel等插件。
然而有時候會有一些意想不到的事情發生。本文主要講述了RabbitMQ叢集如何檢測網絡分區,發生網絡分區帶來的影響以及如何恢複。
RabbitMQ會将queues, exchanges, bindings等資訊存儲在Erlang的分布式資料庫——Mnesia中,許多圍繞網絡分區的一些細節都和這個Mnesia的行為有關。
Detecting network partitions
Mnesia will typically determine that a node is down if another node is unable to contact it for a minute or so (see the page on net_ticktime). If two nodes come back into contact, both having thought the other is down, Mnesia will determine that a partition has occurred. This will be written to the RabbitMQ log in a form like:
RabbitMQ nodes will record whether this event has ever occurred while the node is up, and expose this information through rabbitmqctl cluster_status and the management plugin.
rabbitmqctl cluster_status will normally show an empty list for partitions:
However, if a network partition has occurred then information about partitions will appear there:
The management plugin API will return partition information for each node under partitions in /api/nodes. The management plugin UI will show a large red warning on the overview page if a partition has occurred.
檢測網絡分區
如果另一個節點在一分鐘(或者一個net_ticktime時間)内不能連接配接上一個節點,那麼Mnesia通常任務這個節點已經挂了。就算之後兩個節點連通(譯者注:應該是指網絡上的可連通),但是這兩個節點都認為對方已經挂了,Mnesia此時認定發送了網絡分區的情況。這些會被記錄在RabbitMQ的日志中,如下所示:
當一個節點起來的時候,RabbitMQ會記錄是否發生了網絡分區,你可以通過rabbitmqctl cluster_status這個指令或者管理插件看到相關資訊。正常情況下,通過rabbitmqctl cluster_status指令檢視到的資訊中partitions那一項是空的,就像這樣:
然而當網絡分區發生時,會變成這樣:
通過管理插件的API(under partitions in /api/nodes)可以擷取到在各個節點的分區資訊.
通過Web UI可以在Overview這一頁看到一個大的紅色的告警視窗,就像這樣:

During a network partition
While a network partition is in place, the two (or more!) sides of the cluster can evolve independently, with both sides thinking the other has crashed. Queues, bindings, exchanges can be created or deleted separately.Mirrored queues which are split across the partition will end up with one master on each side of the partition, again with both sides acting independently. Other undefined and weird behaviour may occur.
It is important to understand that when network connectivity is restored, this state of affairs persists. The cluster will continue to act in this way until you take action to fix it.
網絡分區期間
當一個叢集發生網絡分區時,這個叢集會分成兩部分(或者更多),它們各自為政,互相都認為對方分區内的節點已經挂了, 包括queues, bindings, exchanges這些資訊的建立和銷毀都處于自身分區内,與其他分區無關。如果原叢集中配置了鏡像隊列,而這個鏡像隊列又牽涉到兩個(或者多個)網絡分區的節點時,每一個網絡分區中都會出現一個master節點(譯者注:如果rabbitmq版本較新,分區節點個數充足,也會出現新的slave節點。),對于各個網絡分區,此隊列都是互相獨立的。當然也會有一些其他未知的、怪異的事情發生。
當網絡(這裡隻網絡連通性,network connectivity)恢複時,網絡分區的狀态還是會保持,除非你采取了一些措施去解決他。
Partitions caused by suspend / resume
While we refer to “network” partitions, really a partition is any case in which the different nodes of a cluster can have communication interrupted without any node failing. In addition to network failures, suspending and resuming an entire OS can also cause partitions when used against running cluster nodes - as the suspended node will not consider itself to have failed, or even stopped, but the other nodes in the cluster will consider it to have done so.
While you could suspend a cluster node by running it on a laptop and closing the lid, the most common reason for this to happen is for a virtual machine to have been suspended by the hypervisor. While it’s fine to run RabbitMQ clusters in virtualised environments, you should make sure that VMs are not suspended while running. Note that some virtualisation features such as migration of a VM from one host to another will tend to involve the VM being suspended.
Partitions caused by suspend and resume will tend to be asymmetrical - the suspended node will not necessarily see the other nodes as having gone down, but will be seen as down by the rest of the cluster. This has particular implications for pause_minority mode.
挂起/恢複導緻的分區
當我們涉及到“網絡分區”時,當叢集中的不同的節點發生互動失敗中斷(communication interrupted)等,但是又沒有節點挂掉這種情況下,才是發生了分區。然而除了網絡失敗(network failures)原因,作業系統的挂起或者恢複也會導緻叢集内節點的網絡分區。因為發生挂起的節點不會認為自身已經失敗或者停止工作,但是叢集内的其他節點會這麼認為。
如果一個叢集中的一個節點運作在一台筆記本上,然後你合上了筆記本,這樣這個節點就挂起了。或者說一種更常見的現象,節點運作在某台虛拟機上,然後虛拟機的管理程式挂起了這個虛拟機節點,這樣也可能發生挂起。
由于挂起/恢複導緻的分區并不對稱——挂起的節點将看不到其他節點是否消失,但是叢集中剩餘的節點可以觀察到,這一點貌似暗示了pause_minority這種模式(下面會涉及到)。
Recovering from a network partition
To recover from a network partition, first choose one partition which you trust the most. This partition will become the authority for the state of Mnesia to use; any changes which have occurred on other partitions will be lost.
Stop all nodes in the other partitions, then start them all up again. When they rejoin the cluster they will restore state from the trusted partition.
Finally, you should also restart all the nodes in the trusted partition to clear the warning.
It may be simpler to stop the whole cluster and start it again; if so make sure that the first node you start is from the trusted partition.
從網絡分區中恢複
未來從網絡分區中恢複,首先需要挑選一個信任的分區,這個分區才有決定Mnesia内容的權限,發生在其他分區的改變将不被記錄到Mnesia中而直接丢棄。
停止(stop)其他分區的節點,然後啟動(start)這些節點,之後重新将這些節點加入到目前信任的分區之中。
最後,你應該重新開機(restart)信任的分區中所有的節點,以去除告警。
你也可以簡單的關閉整個叢集的節點,然後再啟動每一個節點,當然,你要確定你啟動的第一個節點在你所信任的分區之中。
Automatically handling partitions
RabbitMQ also offers three ways to deal with network partitions automatically: pause-minority mode, pause-if-all-down mode and autoheal mode. (The default behaviour is referred to as ignore mode).
In pause-minority mode RabbitMQ will automatically pause cluster nodes which determine themselves to be in a minority (i.e. fewer or equal than half the total number of nodes) after seeing other nodes go down. It therefore chooses partition tolerance over availability from the CAP theorem. This ensures that in the event of a network partition, at most the nodes in a single partition will continue to run. The minority nodes will pause as soon as a partition starts, and will start again when the partition ends.
In pause-if-all-down mode, RabbitMQ will automatically pause cluster nodes which cannot reach any of the listed nodes. In other words, all the listed nodes must be down for RabbitMQ to pause a cluster node. This is close to the pause-minority mode, however, it allows an administrator to decide which nodes to prefer, instead of relying on the context. For instance, if the cluster is made of two nodes in rack A and two nodes in rack B, and the link between racks is lost, pause-minority mode will pause all nodes. In pause-if-all-down mode, if the administrator listed the two nodes in rack A, only nodes in rack B will pause. Note that it is possible the listed nodes get split across both sides of a partition: in this situation, no node will pause. That is why there is an additional ignore/autoheal argument to indicate how to recover from the partition.
In autoheal mode RabbitMQ will automatically decide on a winning partition if a partition is deemed to have occurred, and will restart all nodes that are not in the winning partition. Unlike pause_minority mode it therefore takes effect when a partition ends, rather than when one starts.
The winning partition is the one which has the most clients connected (or if this produces a draw, the one with the most nodes; and if that still produces a draw then one of the partitions is chosen in an unspecified way).
You can enable either mode by setting the configuration parameter cluster_partition_handling for therabbit application in your configuration file to:
● pause_minority
● {pause_if_all_down, [nodes], ignore | autoheal}
● autoheal
自動處理分區
RabbitMQ提供了三種方法自動的解決網絡分區:pause-minority mode, pause-if-all-down mode以及autoheal mode。(預設的是ignore模式)
在pause-minority mode下,顧名思義,當發生網絡分區時,叢集中的節點在觀察到某些節點“丢失”時,會自動檢測其自身是否處于少數派(小于或者等于叢集中一半的節點數),RabbitMQ會自動關閉這些節點的運作。根據CAP原理來說,這裡保障了P,即分區耐受性(partition tolerance)。這樣確定了在發生網絡分區的情況下,大多數節點(當然這些節點在同一個分區中)可以繼續運作。“少數派”中的節點在分區發生時會關閉,當分區結束時又會啟動。
在pause-if-all-down mode下,RabbitMQ在叢集中的節點不能和list中的任何節點互動時才會關閉叢集的節點({pause_if_all_down, [nodes], ignore | autoheal},list即[nodes]中的節點)。也就是說,隻有在list中所有的節點失敗時才會關閉叢集的節點。這個模式和pause-minority mode有點相似,但是,這個模式允許管理者的任命而挑選信任的節點,而不是根據上下文關系。舉個案例,一個叢集,有四個節點,2個節點在A機架上,另2個節點在B機架上,此時A機架和B機架的連接配接丢失,那麼根據pause-minority mode所有的節點都将被關閉。
在autoheal mode下,當認為發生網絡分區時,RabbitMQ會自動決定一個獲勝(winning)的分區,然後重新開機不在這個分區中的節點。
一個獲勝的分區(a winning partition)是指用戶端連接配接最多的一個分區。(如果産生一個平局,即有兩個(或多個)分區的用戶端連接配接數一樣多,那麼節點數最多的一個分區就是a winning partition. 如果此時節點數也一樣多,将會以一個未知的方式挑選winning partition.)
你可以通過在RabbitMQ配置檔案中設定cluster_partition_handling參數使下面任何一種模式生效:
pause_minority
{pause_if_all_down, [nodes], ignore | autoheal}
autoheal
Which mode should I pick?
It’s important to understand that allowing RabbitMQ to deal with network partitions automatically does not make them less of a problem. Network partitions will always cause problems for RabbitMQ clusters; you just get some degree of choice over what kind of problems you get. As stated in the introduction, if you want to connect RabbitMQ clusters over generally unreliable links, you should use federation or the shovel.
With that said, you might wish to pick a recovery mode as follows:
● ignore - Your network really is reliable. All your nodes are in a rack, connected with a switch, and that switch is also the route to the outside world. You don’t want to run any risk of any of your cluster shutting down if any other part of it fails (or you have a two node cluster).
● pause_minority - Your network is maybe less reliable. You have clustered across 3 AZs in EC2, and you assume that only one AZ will fail at once. In that scenario you want the remaining two AZs to continue working and the nodes from the failed AZ to rejoin automatically and without fuss when the AZ comes back.
● autoheal - Your network may not be reliable. You are more concerned with continuity of service than with data integrity. You may have a two node cluster.
我該挑選那種模式?
有一點必須要清楚,允許RabbitMQ能夠自動的處理網絡分區并不一定會有正面的成效,也有能會帶來更多的問題。網絡分區會導緻RabbitMQ叢集産生衆多的問題,你需要對你所遇到的問題作出一定的選擇。就像本文開篇所說的,如果你置RabbitMQ叢集于一個不可靠的網絡環境下,你需要使用federation或者shovel插件。
你可能選擇如下的恢複模式:
ignore: 你的網絡很可靠,所有的節點都在一個機架上,連接配接在同一個交換機上,這個交換機也連接配接在WAN上,你不需要冒險而關閉部分節點。(或者适合隻有兩個節點的叢集。)
pause_minority: 你的網絡相對沒有那麼的可靠。比如你在EC2上建立了三個節點的叢集,假設其中一個節點宕了,在這種政策下,剩餘的兩個節點還可以繼續工作,失敗的節點可以在恢複之後重新加入叢集
autoheal: 你的網絡非常不可靠,你更關心服務的連續性而不是資料的完整性。适合有兩個節點的叢集。
More about pause-minority mode
The Erlang VM on the paused nodes will continue running but the nodes will not listen on any ports or do any other work. They will check once per second to see if the rest of the cluster has reappeared, and start up again if it has.
Note that nodes will not enter the paused state at startup, even if they are in a minority then. It is expected that any such minority at startup is due to the rest of the cluster not having been started yet.
Also note that RabbitMQ will pause nodes which are not in a strict majority of the cluster - i.e. containing more than half of all nodes. It is therefore not a good idea to enable pause-minority mode on a cluster of two nodes since in the event of any network partition or node failure, both nodes will pause. However, pause_minoritymode is likely to be safer than ignore mode for clusters of more than two nodes, especially if the most likely form of network partition is that a single minority of nodes drops off the network.
Finally, note that pause_minority mode will do nothing to defend against partitions caused by cluster nodes being suspended. This is because the suspended node will never see the rest of the cluster vanish, so will have no trigger to disconnect itself from the cluster.
有關pause-minority模式的更多資訊
關閉的RabbitMQ節點所在主機上的Erlang虛拟機還是在正常運作,但是此節點并不會監聽任何端口也不會執行其他任務。這些節點每秒會檢測一次剩下的叢集節點是否會再次出現,如果出現,就啟動自己繼續運作。
注意上面所說的“關閉的RabbitMQ節點”并不會在啟動時就進入關閉狀态,即使它們在“少數派(minority)”。這些“少數派”可能在“剩餘的叢集節點”沒有啟動好之前就啟動了。
同樣需要注意的是RabbitMQ也會關閉不是嚴格意義上的“大多數(majority)”——數量超過叢集的一半。是以在一個叢集隻有兩個節點的時候并不适合采用pause-minority模式,因為由于其中任何一個節點失敗而發生網絡分區時,兩個節點都會被關閉。然而如果叢集中的節點個數遠大于兩個時,pause_minority模式比ignore模式更加的可靠,特别是網絡分區通常是由于單個節點掉出網絡。
最後,需要注意的是pause_minority模式将不會防止由于叢集節點被挂起而導緻的分區。這是因為挂起的節點将永遠不會看到叢集的其餘部分的消失,是以将沒有觸發器将其從叢集中斷開。