1. Overview of Redis Cluster


Redis, as a very popular in-memory data structure, can be used for data storage, cache and message proxy. This article will explain how to build a Redis cluster based on Docker. The design of Redis cluster consists of two parts: master-slave replication and hash Slot.

1.1. Master/slave Replication

Master-slave replication is common in databases and is used for read-write separation, as is Redis. Slaver can also have its own slaver. Because of this master-slave relationship, they specify their subordinate relationship during the configuration phase, rather than the parallel relationship that Zookeeper automatically optimizes.

In read/write separation, the Master only writes and synchronizes data to Slaver. Slaver performs read tasks. Therefore, the expansion of Slaver improves read efficiency but not write efficiency.

Slaver loads the information obtained by the Master into the disk before loading it into memory, from which the client reads the information. When a new Slaver is added to the cluster, it will actively ask the Master to visit the port. When the Master discovers the new Slaver, it will send the full data to the new Slaver. The larger the data is, the greater the performance consumption is.

  • Advantages: Read and write separation, improved concurrent read capability by adding Slaver.
  • Cons: Master write capacity is a bottleneck, and maintenance Slaver overhead will always be a bottleneck.

1.2. The hash Slot

The hash Slot name may be confusing, but it is actually a “horizontal partition” in the database. If you have read about table partitioning in a database, you will notice that the descriptions of HASH slots are similar to those of “HASH partitions” in database table partitioning.

The object is hashed to a specified Node by CRC16 before being saved to Redis. For example, Object4 is finally hashed to Node1.

Each Node is allocated an average Slot segment, which ranges from 0 to 16384. Slots must be unique or missing. Otherwise, objects cannot be stored repeatedly or cannot be stored.

Nodes also listen to each other. When a Node exits or joins, data is transferred by Slot. For example, if Node1 goes offline, Slot 0 to Slot 5640 will be allocated equally to Node2 and Node3. The slots maintained by Node2 and Node3 will not be reassigned. Therefore, Slot 5641 to Slot 16384 will not be affected during the migration.

  • Advantages: The Redis write operation is distributed to multiple nodes, which improves the write concurrency and makes capacity expansion simple.
  • Disadvantages: Each Node is responsible for monitoring each other, high concurrent data write, high concurrent data read, heavy work tasks.

1.3. Merge into one

It can be found that the advantages and disadvantages of master-slave and hashing design complement each other. Combining the two together, it is the ultimate form of Redis cluster. First Hash is divided into logical nodes, and then inside each logical node is master-slave, as shown in the figure:


2. Install the cluster

Now that we have the Docker environment by default, we will install the Redis cluster based on Docker. Prior to Redis 5.0, there were many tutorials on creating clusters using redis-trib.rb, but after Redis 5.0, you can only create clusters using redis-cli. In this paper, the cluster is created through redis- CLI. Because the node election method of Redis cluster requires more than half of the master to pass, so it is suggested to create an odd number of nodes. In this example, three Master nodes are created and one Slave node is allocated to each Master node.

2.1. Host environment

Conf file, rename it to redis-cluster.tmpl, and configure the following parameters. The purpose of this file is to generate redis.conf for each redis instance:



vi redis-cluster.tmpl

Then execute the following script to create their own directories for the three masters and three slaves

The current directory structure is

2.2. Create a Redis node

Assuming that we only consider the pure Docker environment without service orchestration such as Docker-compose and K8S, communication between each Redis container must be guaranteed by creating docker network. (The use of microservice choreography will be discussed later)



Now we can run the master and slave instances of Docker Redis



View the created Redis container



2.3. Build a cluster

Run redis-cli to create a cluster. Find a redis container and run redis-cli –cluster create –cluster-replicas 1 IP :port



Remember to make sure the node redis data is empty when building the cluster, otherwise the following error occurs.



2.4. Cluster verification

After the cluster was set up, we connected the cluster nodes through redis- CLI command for verification. Redis -cli -c -h ${IP} -p ${port}



You can see the basic cluster information through the “cluster info” command. All slots (16384) have been allocated. Then run the “cluster Nodes “command to view the slots allocated for each master node. At this point, the Redis cluster is basically installed successfully.

3. Later operation and maintenance

3.1. Basic commands

The cluster



node

Groove (slot)




3.2. Common Problems

(1) Redis-cluster maps all physical nodes into [0 to 16,383] slots. The cluster maintains node<->slot<->value.

(2) If the master fails on any node in the cluster but slaver still exists, the slave becomes master automatically, and the system works normally.

(3) If the master fails and slaver does not exist on any node in the cluster, the cluster enters the Fail state.

(4) If the master of more than half of the nodes in the cluster fails, the cluster will enter the Fail state regardless of slaver status.

(5) All the master nodes in the cluster participate in the election to determine whether the node is invalid. If more than half of the master nodes and the current detected master node communication detection times out (cluster-node-timerout), the current master node is considered to have hung up.

I started my own business team, MadPecker, mainly doing BUG management, test management and application distribution

Website :www.madpecker.com, friends in need welcome to try, experience!

This article is prepared by MadPecker team technical staff, please indicate the source of reprint