]> git.saurik.com Git - redis.git/blob - CLUSTER
Cluster branch merged to unstable.
[redis.git] / CLUSTER
1 TODO
2
3 - disconnect FAIL clients after some pong idle time.
4
5 ---------------------------------
6
7 * Majority rule: the cluster con continue when there are all the hash slots covered AND when there are the majority of masters.
8 * Shutdown on request rule: when a node sees many connections closed or even a timeout longer than usual on almost all the other nodes, it will usually wait for the normal timeout before to change the state, unless it receives a query from a client: in such a case it will put itself into error status.
9
10 --------------------------------
11
12 * When asked for a key that is not in a node's business it will reply:
13
14 -ASK 1.2.3.4:6379 (in case we want the client to ask just one time)
15 -MOVED <slotid> 1.2.3.4:6379 (in case the hash slot is permanently moved)
16
17 So with -ASK a client should just retry the query against this new node, a single time.
18
19 With -MOVED the client should update its hash slots table to reflect the fact that now the specified node is the one to contact for the specified hash slot.
20
21 * Nodes communicate using a binary protocol.
22
23 * Node failure detection.
24
25 1) Every node contains information about all the other nodes:
26 - If this node is believed to work ok or not
27 - The hash slots for which this node is responsible
28 - If the node is a master or a slave
29 - If it is a slave, the slave of which node
30 - if it is a master, the list of slave nodes
31 - The slaves are ordered for "<ip>:<port>" string from lower to higher
32 ordered lexicographically. When a master is down, the cluster will
33 try to elect the first slave in the list.
34
35 2) Every node also contains the unix time where every other node was
36 reported to work properly (that is, it replied to a ping or any other
37 protocol request correctly). For every node we also store the timestamp
38 at which we sent the latest ping, so we can easily compute the current
39 lag.
40
41 3) From time to time a node pings a random node, selected among the nodes
42 with the least recent "alive" time stamp. Three random nodes are selected
43 and the one with lower alive time stamp is pinged.
44
45 4) The ping packet contains also information about a few random nodes
46 alive time stamp. So that the receiver of the ping will update the
47 alive table if the received alive timestamp is more recent the
48 one present in the node local table.
49
50 In the ping packet every node "gossip" information is somethig like
51 this:
52
53 <ip>:<port>:<status>:<pingsent_timestamp>:<pongreceived_timestamp>
54
55 status is OK, POSSIBLE_FAILURE, FAILURE.
56
57 5) The node replies to ping with a pong packet, that also contains a random
58 selections of nodes timestamps.
59
60 A given node thinks another node may be in a failure state once there is a
61 ping timeout bigger than 30 seconds (configurable).
62
63 When a possible failure is detected the node performs the following action:
64
65 1) Is the average between all the other nodes big? For instance bigger
66 than 30 seconds / 2 = 15 seconds? Probably *we* are disconnected.
67 In such a case we don't trust our lag data, and reset all the
68 timestamps of sent ping to zero. This way when we'll reconnect there
69 is no risk that we'll claim many nodes are down, taking inappropriate
70 actions.
71
72 2) Messages from nodes marked as failed are *always* ignored by the other
73 nodes. A new node needs to be "introduced" by a good online node.
74
75 3) If we are well connected (that is, condition "1" is not true) and a
76 node timeout is > 30 seconds, we mark the node as POSSIBLE_FAILURE
77 (a flat in the cluster node structure). Every time we sent a ping
78 to another node we inform this other nodes that we detected this
79 condition, as already stated.
80
81 4) Once a node receives a POSSIBLE_FAILURE status for a node that is
82 already marked as POSSIBLE_FAILURE locally, it sends a message
83 to all the other nodes of type NODE_FAILURE_DETECTED, communicating the
84 ip/port of the specified node.
85
86 All the nodes need to update the status of this node setting it into
87 FAILURE.
88
89 5) If the computer in FAILURE state is a master node, what is needed is
90 to perform a Slave Election.
91
92 SLAVE ELECTION
93
94 1) The slave election is performed by the first slave (with slaves ordered
95 lexicographically). Actually it is the first functioning slave, so if
96 the first slave is marked as failing the next slave will perform the
97 election and so forth. Such a slave is called the "Successor".
98
99 2) The Successor starts checking that all the nodes in the cluster already
100 marked the master in FAILURE state. If at least one node does not agree
101 no action is performed.
102
103 3) If all the nodes agree that the master is failing, the Successor does
104 the following:
105
106 a) It will send a SUCCESSION message to all the other nodes, that will
107 upgrade the hash slot tables accordingly. It will make sure that all
108 the nodes are updated and if some node did not received the message
109 it will keep trying.
110 b) Once all nodes not marked as FAILURE accepted the SUCCESSION message
111 it will update his own table and will start acting as a master
112 accepting write queries.
113 c) Every node receiving the succession message, if not already informed
114 of the change will broadcast the same message to other three random
115 nodes. No action is performed if the specified host was already marked
116 as the master node.
117 d) A node that was a slave of the original master that failed will
118 switch master to the new one once the SUCCESSION message is received.
119
120 RANDOM
121
122 1) When selecting a slave, the system will try to pick one with an IP different than the master and other slaves, if possible.
123
124 2) The PING packet also contains information about the local configuration checksum. This is the SHA1 of the current configuration, without the bits that normally change form one node to another (like latest ping reply, failure status of nodes, and so forth). From time to time the local config SHA1 is checked against the list of the other nodes, and if there is a mismatch between our configuration and the most common one that lasts for more than N seconds, the most common configuration is asked and retrieved from another node. The event is logged.
125
126 3) Every time a node updates its internal cluster configuration, it dumps such a config in the cluster.conf file. On startup the configuration is reloaded.
127 Nodes can share the cluster configuration when needed (for instance if SHA1 does not match) using this exact same format.
128
129 CLIENTS
130
131 - Clients may be configured to use slaves to perform reads, when read-after-write consistency is not required.