Reprinted from: http://www.cnblogs.com/kevingrace/p/7846324.html
Generally speaking, the purpose of redis master-slave and mysql master-slave is similar, but the redis master-slave configuration is very simple. It mainly specifies the master node ip and port in the slave node configuration file, such as: slaveof 192.168.10.10 6379, and then starts the master-slave, master-slave It's set up. If the master node of redis fails, it will not switch automatically. You need to use redis's Sentinel (sentinel mode) or keepalive to achieve master failover.
Today we will introduce the redis cluster cluster mode:
redis cluster is a centerless distributed redis storage architecture that can share data between multiple nodes and solve the problems of redis high availability and scalability. The redis cluster provides the following two Benefits:
1) Automatically split data into multiple nodes
2) When a node in the cluster fails, redis can continue to process client requests.
A Redis cluster contains 16384 hash slots, and each data in the database belongs to one of these 16384 hash slots. The cluster uses the formula CRC16(key) % 16384 to calculate which slot the key belongs to. Each node in the cluster is responsible for processing a portion of hash slots.
Master-slave replication in the cluster
Each node in the cluster has 1 to N replicas, one of which is the master node and the rest are slave nodes. If the master node goes offline, the cluster will replace one of the master nodes. The slave node is set as the new master node and continues working. In this way, the cluster will not be unable to work properly because a master node goes offline.
==========The Load Balance function has been supported since Redis3.x============
Redis Cluster cluster function has been launched for some time. In the stand-alone version of Redis, there is no communication between each Master, so pre-sharding is usually done in a Jedis client or a proxy such as Codis. According to CAP theory, the stand-alone version of Redis guarantees CP (Consistency & Partition-Tolerancy) at the expense of A (Availability), which means that Redis can ensure that all users see the same data (consistency, because Redis does not automatically redundant Data) and network communication problems, the temporarily isolated subsystem can continue to run (partition tolerance, because there is no direct relationship between Masters and no communication is required), but there is no guarantee that when some nodes fail, all requests can Responded (availability, if a Master node is down, the sharded data on it will be inaccessible).
With the Cluster function, Redis has changed from a simple NoSQL memory database to a distributed NoSQL database, and the CAP model has also changed from CP to AP. In other words, through automatic sharding and redundant data, Redis has real distributed capabilities. If a node fails, because the data is backed up on other nodes, other nodes can continue to provide it. service, ensuring Availability. However, precisely because of this, Redis cannot guarantee the strong consistency it once had. This is also required by CAP theory, and only two of the three can be chosen.
Redis Cluster is a cluster implementation of Redis with a built-in automatic data sharding mechanism. All keys are mapped to 16384 Slots within the cluster. Each Redis Instance in the cluster is responsible for reading and writing part of the Slots. The cluster client can send commands by connecting to any Redis Instance in the cluster. When the Redis Instance receives a request for a Slot that it is not responsible for, it will return the Redis Instance address responsible for requesting the Slot where the Key is located to the client. After the client receives it Automatically resend the original request to this address, transparent to the outside world. Which Slot a Key belongs to is determined by crc16(key) % 16384. In Redis Cluster, load balancing and HA are already fully supported.
Load Balance: Data can be migrated between Redis Instances in the cluster in Slot units, but it is not automatic and needs to be triggered by external commands.
Cluster member management: Cluster nodes (Redis Instance) and nodes regularly exchange and update node information in the cluster. From the perspective of the sending node, this information includes: which nodes are in the cluster, what are the IP and PORT, and the node information. What is the name, what is the status of the node (such as OK, PFAIL, FAIL, detailed later), including the node role (master or slave), etc.
Regarding availability, the cluster consists of N groups of master-slave Redis Instances.
The master can have no slaves, but the absence of slaves means that the Slot read and write services that the master is responsible for are unavailable after the master goes down.
A master can have multiple slaves. When the master goes down, a certain slave will be promoted to the master. Specifically, which slave is promoted to the master. The protocol is similar to Raft. See here. How to detect master outage? Redis Cluster uses the quorum+heartbeat mechanism. From the perspective of a node, the node will periodically send Pings to all other nodes. If no reply is received from the other party within the cluster-node-timeout (configurable, second level), the peer node will be unilaterally considered to be down, and the node will be considered down. The node is marked with PFAIL status. Through the exchange of information between nodes, it is collected that a quorum of nodes all think that this node is PFAIL, so the node is marked as FAIL and sent to all other nodes. After receiving it, all other nodes immediately consider the node to be down. It can be seen from here that after the master goes down, the read and write services of the Slot that the master is responsible for are unavailable at least within the cluster-node-timeout period.
The characteristics of Redis Cluster are as follows:
- Node automatic discovery
- slave->master election, cluster fault tolerance
- Hot resharding: online sharding
- Cluster management:clusterxxx
- Cluster management based on configuration (nodes-port.conf)
- ASK steering/MOVED steering mechanism
- Deployment does not require specifying a master
- Can support clusters of more than 1,000 nodes
======Redis-Cluster adopts a centerless structure. Each node saves data and the entire cluster status. Each node is connected to all other nodes. ====== The
redis-cluster architecture diagram is as follows:
Its structural features:
- All redis nodes are interconnected with each other (PING-PONG mechanism), and a binary protocol is used internally to optimize transmission speed and bandwidth.
- The fail of a node takes effect only when more than half of the nodes in the cluster detect failures.
- The client is directly connected to the redis node, without the need for an intermediate proxy layer. The client does not need to connect to all nodes in the cluster, just connect to any available node in the cluster.
- redis-cluster maps all physical nodes to [0-16383] slot (not necessarily evenly distributed), and cluster is responsible for maintaining node<->slot<->value.
- The Redis cluster is pre-divided into 16384 buckets. When a key-value needs to be placed in the Redis cluster, the bucket in which a key is placed is determined based on the value of CRC16(key) mod 16384.
The redis cluster is designed to reduce the pressure on a single node or a simple master-slave redis. There is no synchronization relationship between the master and master nodes, and there is a synchronization relationship between the data between the master and slave nodes . How many master nodes there are, 16384 hash slots will be evenly distributed to these master nodes . When writing data to redis, the number of hash slots will be calculated based on the hash algorithm and decided to put it. Which master node to go to, and then the slave node of this master node will automatically synchronize. Just connect to any master node on the client, and there will be internal jumps between master nodes! When the corresponding data is fetched, each node will automatically jump to the main node where the fetched data is located!
1) Redis cluster node allocation
Assume that there are three master nodes: A, B, C. They can be three ports on one machine, or they can be three different servers.
Then, if 16384 slots are allocated using the hash slot method , the slot ranges assumed by the three nodes are:
Node A covers 0-5460;
Node B covers 5461-10922;
Node C covers 10923-16383 .Get
data:
If a value is stored, follow the algorithm of the redis cluster hash slot: CRC16('key')%16384 = 6782. Then the storage of this key will be allocated to B. Similarly, when I connect to
any node (A, B, C) and want to get the key 'key', I will also use this algorithm, and then jump internally to node B to get the data. Add a new master
node:
Add a new node D, the method of redis cluster is to take a part of the slot from the front of each node and put it on D. I will experiment in the next practice. It will roughly become like this:
Node A covers 1365-5460
Node B covers 6827-10922
Node C covers 12288-16383
Node D covers 0-1364,5461-6826,10923-12287
Deleting a node is similar. You can delete the node after the move is completed.
2) Redis Cluster master-slave mode
In order to ensure the high availability of data, redis cluster has added the master-slave mode. One master node corresponds to one or more slave nodes. The master node provides data access, and the slave nodes pull data from the master node.
Backup, when the master node hangs up, one of the slave nodes will be selected to act as the master node to ensure that the cluster will not hang up.
In the above example, the cluster has three master nodes A, B, and C. If these three nodes do not join the slave nodes, and if B dies, we will not be able to access the entire cluster. Slots A and C are also inaccessible.
Therefore, when the cluster is established, slave nodes must be added to each master node. For example, like this, the cluster contains master nodes A, B, C, and slave nodes A1, B1, and C1. Then even if B hangs up the system,
Can continue to work correctly. Node B1 replaces node B, so the Redis cluster will select node B1 as the new master node, and the cluster will continue to provide services correctly. When B is restarted, it will become the slave node of B1.
However, it should be noted that if nodes B and B1 hang up at the same time, the Redis cluster will not be able to continue to provide services correctly.
=========== Without further ado, let’s record how to build a redis cluster ==========
Since the smallest redis cluster requires 3 master nodes (that is, the Redis Cluster cluster requires at least 3 master nodes, that is to say, at least 6 nodes are needed to build a Redis cluster). One machine can run multiple redis instances (usually two machines are used, and each starts 3 redis instances, that is, three master nodes. three slave nodes). In many cases, a single server is used to open 6 ports. The operations are similar, but the configuration is relatively simple. Multiple servers are closer to the production environment. [When the cluster is initially created, remember the master-slave relationship of each node (or specify the master-slave relationship when creating); if one of the machines restarts, after restarting, it needs to be added to the redis cluster again. ; This requires changing the previous slave node of each node on this machine to the master node (the client executes slaveof no one), and then adding each node of this machine to the cluster according to the new master node, and then changing it to For the slave node]
redis cluster node information in this case:
redis01
172.16.51.175:7000
172.16.51.175:7001
172.16.51.175:7002
redis02
172.16.51.176:7003
172.16.51.176:7004 172.1
6.51.176
:7005 redis03
172.16.51.178:7006
172.16 .51.178:7007
172.16.51.178:7008
Let’s first talk about the deployment process of the redis01 node (the deployment process of the other two nodes is the same)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 |
|
==========================集群模式配置======================
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
==========================redis cluster常见的几个问题======================
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
|