170103, Redis official cluster scheme Redis Cluster

Source: Internet
Author: User
Tags failover redis cluster install redis redis server

We talked about Redis sharding multi-server clustering technology, Redis sharding is a client-side sharding technology, for the server, each Redis servers are independent of each other, which is very lightweight for the service side to deploy Redis flexibly, The Redis sharding is a lightweight clustering technology with good flexibility and scalability.

This article introduces another multi-Redis server clustering technology, namely Redis Cluster. Redis cluster is a server sharding technology that is officially available in version 3.0.

In Redis cluster, the sharding uses the concept of slot (slot), which is divided into 16,384 slots, which is somewhat similar to the pre-sharding idea of the previous one. For each key-value pair that enters Redis, it is hashed according to Key and assigned to one of the 16,384 slots. The hash algorithm used is also relatively simple, that is, after the CRC16 16384 modulo.

Each node (node) in the Redis cluster is responsible for allocating part of the 16,384 slots, that is, each slot is responsible for one node processing. When node nodes are added or reduced dynamically, 16,384 slots need to be reassigned, and the key values in the slots are migrated. Of course, this process, in the current implementation, is still in the semi-automatic state, requires manual intervention.

Redis cluster, to ensure that the 16,384 slots corresponding to the node is working properly, if a node fails, then its responsible slots will be invalidated, the entire cluster won't work.

In order to increase the accessibility of the cluster, the official recommended scheme is to configure node as a master-slave structure, which is the primary master node, which hangs n slave from the node. At this point, if the primary node fails, Redis cluster selects an ascending primary node from the slave node based on the election algorithm, and the entire cluster continues to provide services externally. This is very similar to the Redis sharding scenario mentioned in the previous article, where the server node is composed of a master-slave structure via the Sentinel Monitor, but the Redis cluster itself provides the ability to fail-over fault-tolerant.

The new node recognition capability, fault detection and failover capability of Redis cluster is communicated through each node in the cluster, which is referred to as the cluster bus (cluster bus). They use a special port number, which is the external service port number plus 10000. For example, if the port number of a node is 6379, then it communicates with other nodes with a port number of 16379. The communication between nodes uses a special binary protocol.

For the client, the entire cluster is considered to be a whole, and the client can connect to any node to operate, as with a single Redis instance, and when the client operation key is not assigned to the node, Redis returns to the correct node, It's kind of like a 302 redirect jump on a browser page.

Redis cluster is a Redis 3.0 after the official launch, late, at present can prove that in large-scale production environment, the success of the case is not many, it takes time to test.

Cluster Build R

Below, let's build a Redis Cluster for the actual operation. We will build 3 node, each node frame form a master one from, so there are 6 Redis instances, port number from 7000-7005.

Redis instances under cluster, like normal Redis instances, are only run in cluster mode.

The actual steps are as follows:

1. Take 3.0.5 as an example, set up a cluster directory, and then build 6 subdirectories to represent the 6 instance environment

mkdir Cluster

mkdir 7000 7001 7002 7003 7004 7005

2. Copy the redis.conf template to the above 6 subdirectories, and make the following changes, taking 7000 as an example:

Modify the following information

Daemonize Yes


Port 7000

LogFile "/var/log/redis-7000.log"

Comment out the following information, no RDB persistence required

#save 900 1

#save 300 10

#save 60 10000

Modify the following information

AppendOnly Yes

Appendfilename "Appendonly-7000.aof"

Remove the following comment to let Redis run in cluster mode

cluster-enabled Yes to start cluster mode

Cluster-config-file nodes-7000.conf Cluster information file name, maintained by Redis itself

Cluster-node-timeout 15000 15 seconds can not contact the other node, that the other side of the fault may

3. Execute Redis-server redis.conf in each directory to start the Redis instance

At this point, each of these instances is a cluster state that is running, and does not form an overall cluster, and we need the ruby-based development tools provided by Redis for manual setup.

4. Install the Ruby environment

Yum install ruby Ruby-devel RubyGems

5. Installing the Ruby dependency interface for Redis

Gem Install Redis

6. Creating a cluster with scripting tools

./redis-trib.rb Create--replicas 1

The script automatically executes the node assignment scheme, with the first 3 Redis instances as the primary node, and the last three as the slave node,

Follow the prompts to "yes", the implementation of the scheme, the 6 nodes into a cluster, 3 master 3 from.

Perform the Redis-cli-p 7000 info Replication command, observe the 7000 node, discover that its replication configuration information is configured as a master node, and have a slave node 7003

Then execute redis-cli-p 7003 info replication found that the node has been set up from the node. These master and slave settings are automatically completed when the cluster is created.

At this point, 3 of the Master 3 from the Redis cluster has been set up. Next we do a failover test, the main node 7001 shutdown off, to see what happens?

We see that from node 7004 will rise the primary node continues to provide the Cluster service. What about restarting the 7001 node?

We found that the 7001 node has become a slave node and will not be replaced by the 7,004 primary node. What if I shutdown both the primary node 7001 and the node 7004?

At this time, the entire cluster is refused to provide services. Since the original 7001 allocated slots now have no node takeover, manual intervention is required to reallocate the slots.

Delete cluster node r

Below we operate, if you modify the cluster node schema:

Deletes a slave node. Note that if you delete the master node, its responsible slots must be empty.

./redis-trib.rb Del-node EE2FC0EA6E630F54E3B811CAEDF8896B26A99CBA

Transfer 7001-node slots to 7000

./redis-trib.rb Reshard

Follow the prompts to do so.

Add a node. Note that the newly added node has not yet allocated a slot, assigning it a percentage of slots with Reshard

./redis-trib.rb Add-node

Adds a slave node to the specified primary node.

./redis-trib.rb add-node--slave--master-id f4d17d56a9dda1a102da7cd799192beff7cba69e

Note If this Redis instance participates in a cluster, you need to reset it by cluster reset cleanup first.

Jedis Client Access R

The service-side Redis cluster build process is described above. Let's see how the client uses it.

The Java language client-side driver Jedis is supported for Redis cluster. We specifically do the following:

1. Configuring the Jedis jar package in Pom.xml




<version>2.8.0</version> <!--recently upgraded


2. Configure Jediscluster in the spring configuration file

<bean id= "Poolconfig" class= "Redis.clients.jedis.JedisPoolConfig" >

<property name= "Maxtotal" value= "4096"/>

<property name= "Maxidle" value= "/>"

<property name= "Maxwaitmillis" value= "/>"

<property name= "Testonborrow" value= "true"/>

<property name= "Testonreturn" value= "true"/>


<bean id = "Jediscluster" class = "Redis.clients.jedis.JedisCluster" >

<constructor-arg index= "0" >


<bean class= "Redis.clients.jedis.HostAndPort" >

<constructor-arg index= "0" value= ""/>

<constructor-arg index= "1" value= "7002"/>




<constructor-arg index= "1" value= "" "type=" int "/>

<constructor-arg index= "2" value= "2" type= "int"/>

<constructor-arg index= "3" ref= "Poolconfig"/>


3. Writing test Code


public void Basicoptestforcluster () {

Long begin = System.currenttimemillis ();

for (int i=0;i<10000; i++) {

Jedis.set ("person." + i + ". Name", "Frank");

Jedis.set ("person." + i + ". City", "Beijing");

String name = Jedis.get ("person." + i + ". Name");

String City = jedis.get (' person. ' + i + '. City ');

Assertequals ("Frank", name);

Assertequals ("Beijing", city);

Jedis.del ("person." + i + ". Name");

Boolean result = jedis.exists ("person." + i + ". Name");

Assertequals (False,result);

result = Jedis.exists ("person." + i + ". City");

Assertequals (True,result);


Long end = System.currenttimemillis ();

SYSTEM.OUT.PRINTLN ("Total Time:" + (End-begin)/1000);


170103, Redis official cluster scheme 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.