The construction and practice of Redis cluster

Source: Internet
Author: User
Tags redis cluster install redis

Redis ClusterFirst, redis-cluster design

Redis clusters are built in a variety of ways, such as using zookeeper, but after Redis 3.0, the Redis-cluster cluster is supported, Redis-cluster uses a non-central structure, each node holds the data and the entire cluster state, Each node is connected to all other nodes. The composition of its redis-cluster frame is as follows:

Its structural characteristics:

1. All Redis nodes are interconnected (ping-pong mechanism), using binary protocols to optimize transmission speed and bandwidth internally.
2. The fail of a node is effective only if it is detected by more than half of the nodes in the cluster.
3, the client and the Redis node direct connection, do not need intermediate proxy layer. The client does not need to connect to all nodes in the cluster to connect to any of the available nodes in the cluster.
4, Redis-cluster all the physical nodes mapped to [0-16383]slot (not necessarily the average distribution), cluster responsible for maintaining node<->slot<->value.

5, Redis cluster good 16,384 buckets, when you need to place a key-value in the Redis cluster, according to the value of CRC16 (key) mod 16384, decide which bucket to put a key.


   1. Redis cluster node Assignment


Now we are three main nodes: A, B, C three nodes, which can be three ports on a single machine or three different servers. Then, with the hash slot (hash slot) to allocate 16,384 slots, their three nodes will assume the slot interval is:


Node A covers 0-5460;
Node B covers 5461-10922;
Node C covers 10923-16383.

Get Data:

If a value is deposited, follow the algorithm of the Redis cluster hash slot: CRC16 (' key ')%16384 = 6782. Then the storage for this key will be allocated to B. Similarly, when I connect (a,b,c) any node want to get ' key ' this key, it will also be such an algorithm, and then jump inside the B node to get the data

Add a new master node:

Add a node D,redis cluster This approach is to take a portion of the slots from the front of each node to D, which I'll experiment with in the next practice. This is roughly what it looks like:

Node A covers 1365-5460
Node B covers 6827-10922
Node C covers 12288-16383
Node D covers 0-1364,5461-6826,10923-12287


Similarly, deleting a node is similar, the node can be deleted after the move is complete.


 2. Redis cluster master-slave mode

Redis cluster in order to ensure the high availability of data, a master-slave mode, a primary node corresponding to one or more slave nodes, the master node to provide data access, from the node is to pull the data from the main node backup, when the main node hangs, there will be a slave node selected to act as the master node, This ensures that the cluster will not be hung out.

In the above example, the cluster has the ABC three master node, if the 3 nodes are not joined from the node, if B is dead, we will not be able to access the entire cluster. Slots A and C are also inaccessible.

So when we set up the cluster, we must add the slave nodes for each master node, such as this, the cluster contains the primary Node A, B, C, and from the node A1, B1, C1, so even if B hangs the system can continue to work correctly.

The B1 node replaces the b node, so the Redis cluster will select the B1 node as the new primary node, and the cluster will continue to provide the service correctly. When B is re-opened, it becomes B1 from the node.

However, it is important to note that if Node B and B1 are hung at the same time, the Redis cluster cannot continue to provide the service correctly.


Second, the construction of Redis cluster

The cluster should have at least an odd number of nodes, so there are at least three nodes, each node has at least one backup node, so the following uses 6 nodes (master node, backup node determined by the Redis-cluster cluster).

Use redis-3.2.0 installation below,

1. Install Redis node designated port

Unzip the Redis compact package, compile and install

<span style= "FONT-SIZE:14PX;" >[[email protected] redis-3.2.0]# tar xzf redis-3.2.0.tar.gz[[email protected] redis-3.2.0]# CD Redis-3.2.0[[email Protected] redis-3.2.0]# Make[[email protected] redis01]# make install prefix=/usr/andy/redis-cluster</span>

under Redis-cluster, modify the Bin folder to Redis01, and copy the redis.conf configuration file

Configure the configuration file for Redis01 redis.conf

Daemonize Yes #后台启动

Port 7001 #修改端口号, from 7001 to 7006

cluster-enabled Yes #开启cluster, remove comments

Copy six copies, modify the corresponding port number


  2. The Ruby script required to install the Redis-trib

Copy the redis-trib.rb file from the Redis decompression file src to the Redis-cluster directory

<span style= "FONT-SIZE:14PX;" >[[email protected] redis-cluster]# cp/usr/andy/redis/redis-3.2.0/src/redis-trib.rb ./</span>

to install the Ruby environment:

<span style= "FONT-SIZE:14PX;" >[[email protected] redis-cluster]# yum install ruby[[email protected] redis-cluster]# Yum install rubygems</span& Gt

install redis-trib.rb run dependent Ruby package Redis-3.2.2.gem, download

<span style= "FONT-SIZE:14PX;" >[[email protected] redis-cluster]# gem install Redis-3.2.2.gem </span>

3. Start all Redis nodes

Can write a command script start-all.sh

<span style= "FONT-SIZE:14PX;" >CD redis01./redis-server REDIS.CONFCD. CD Redis02./redis-server REDIS.CONFCD. CD Redis03./redis-server REDIS.CONFCD. CD Redis04./redis-server REDIS.CONFCD. CD Redis05./redis-server REDIS.CONFCD. CD Redis06./redis-server REDIS.CONFCD. </span>

Set permissions to start

<span style= "FONT-SIZE:14PX;" >[[email protected] redis-cluster]# chmod 777 start-all.sh [[email protected] redis-cluster]#./start-all.sh </ Span>


Viewing the Redis process startup status

[Email protected] redis-cluster]# Ps-ef | grep redisroot       4547      1  0 23:12?        00:00:00./redis-server 127.0.0.1:7001 [cluster]root       4551      1  0 23:12?        00:00:00./redis-server 127.0.0.1:7002 [cluster]root       4555      1  0 23:12?        00:00:00./redis-server 127.0.0.1:7003 [cluster]root       4559      1  0 23:12?        00:00:00./redis-server 127.0.0.1:7004 [cluster]root       4563      1  0 23:12?        00:00:00./redis-server 127.0.0.1:7005 [cluster]root       4567      1  0 23:12?        00:00:00./redis-server 127.0.0.1:7006 [cluster]root       4840   4421  0 23:26 pts/1    00:00:00 grep-- Color=auto Redis

You can see that the Redis 6 nodes have started successfully

4. Create a cluster using REDIS-TRIB.RB

./redis-trib.rb Create--replicas 1 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 127.0.0.1:7006

Use the Create command. The--replicas 1 parameter represents the creation of a slave node for each master node. The other parameters are the collection of addresses for the instance.

[[email protected] redis-cluster]#./REDIS-TRIB.RB Create--replicas 1 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 127.0.0.1:7006>>> Creating cluster>>> Performing Hash Slots allocation on 6 nodes ... Using 3 masters:127.0.0.1:7001127.0.0.1:7002127.0.0.1:7003adding replica 127.0.0.1:7004 to 127.0.0.1:7001adding Replica 127.0.0.1:7005 to 127.0.0.1:7002adding replica 127.0.0.1:7006 to 127.0.0.1:7003m: Dfd510594da614469a93a0a70767ec9145aefb1a 127.0.0.1:7001 slots:0-5460 (5461 slots) Masterm: E02EAC35110BBF44C61FF90175E04D55CCA097FF 127.0.0.1:7002 slots:5461-10922 (5462 slots) Masterm: 4385809e6f4952ecb122dbfedbee29109d6bb234 127.0.0.1:7003 slots:10923-16383 (5461 slots) MasterS: ec02c9ef3acee069e8849f143a492db18d4bb06c 127.0.0.1:7004 replicates dfd510594da614469a93a0a70767ec9145aefb1as: 83e5a8bb94fb5aaa892cd2f6216604e03e4a6c75 127.0.0.1:7005 replicates e02eac35110bbf44c61ff90175e04d55cca097ffs: 10c097c429ca24f8720986c6b66f0688bFb901ee 127.0.0.1:7006 replicates 4385809e6f4952ecb122dbfedbee29109d6bb234can I set the above configuration? (Type ' yes ' to accept): yes>>> Nodes configuration updated>>> Assign a different config epoch to each no De>>> sending CLUSTER MEET messages to join the clusterwaiting for the CLUSTER to join......>>> Performi ng Cluster Check (using node 127.0.0.1:7001) m:dfd510594da614469a93a0a70767ec9145aefb1a 127.0.0.1:7001 slots:0-5460 (54 Slots) masterm:e02eac35110bbf44c61ff90175e04d55cca097ff 127.0.0.1:7002 slots:5461-10922 (5462 slots) masterm:43858 09e6f4952ecb122dbfedbee29109d6bb234 127.0.0.1:7003 slots:10923-16383 (5461 slots) Masterm: ec02c9ef3acee069e8849f143a492db18d4bb06c 127.0.0.1:7004 Slots: (0 slots) Master replicates dfd510594da614469a93a0a707 67ec9145aefb1am:83e5a8bb94fb5aaa892cd2f6216604e03e4a6c75 127.0.0.1:7005 Slots: (0 slots) Master replicates e02eac3511 0bbf44c61ff90175e04d55cca097ffm:10c097c429ca24f8720986c6b66f0688bfB901ee 127.0.0.1:7006 Slots: (0 slots) master replicates 4385809e6f4952ecb122dbfedbee29109d6bb234[ok] all nodes agree About slots configuration.>>> Check for open slots...>>> Check slots coverage ... [OK] All 16384 slots covered.

The above shows that the creation was successful, there are 3 primary nodes, 3 slave nodes, and each node is a successful connection state.


the 3 primary nodes [M] and the allocated Hashika slots are as follows:

M:dfd510594da614469a93a0a70767ec9145aefb1a 127.0.0.1:7001
slots:0-5460 (5461 slots) Master
M:E02EAC35110BBF44C61FF90175E04D55CCA097FF 127.0.0.1:7002
slots:5461-10922 (5462 slots) Master
m:4385809e6f4952ecb122dbfedbee29109d6bb234 127.0.0.1:7003
slots:10923-16383 (5461 slots) Master

the 3 slave nodes [S] and the secondary master nodes are as follows:

S:EC02C9EF3ACEE069E8849F143A492DB18D4BB06C 127.0.0.1:7004
Replicates Dfd510594da614469a93a0a70767ec9145aefb1a
S:83e5a8bb94fb5aaa892cd2f6216604e03e4a6c75 127.0.0.1:7005
Replicates e02eac35110bbf44c61ff90175e04d55cca097ff
S:10c097c429ca24f8720986c6b66f0688bfb901ee 127.0.0.1:7006
Replicates 4385809e6f4952ecb122dbfedbee29109d6bb234


the above cluster installation succeeded if the installation failed to report the following error
>>> Creating Cluster
[ERR] Sorry, can ' t connect to node ....

Need to install the latest Ruby source code, download

[Email protected] redis-cluster]# TAR-ZXVF ruby-2.3.1.tar.gz [[email protected] redis-cluster]# CD  [email Protected] redis-cluster]#./configure--prefix=/usr/local/ruby-2.3.1  [[email protected] redis-cluster]# make && make install     [[email protected] redis-cluster] #gem install Redis  


third, the Redis cluster test










The construction and practice of Redis cluster

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.