Author: Zhang Fengzhe

Link: https://www.jianshu.com/p/dbc62ed27f03

To build the simplest Redis cluster, you need at least six nodes: three masters and three slaves. Why do we need three masters? If you know anything about Hadoop/Storm/Zookeeper, you know that general distribution requires a base number of nodes so that elections are easy (majority rule).

Redis cluster model

Here, I’ll take the “lazy” approach and build a six-node Redis cluster on a Linux virtual machine. (My computer was completely overwhelmed because I had six Linux virtual machines running)

In fact, the idea is simple: I’m going to open six instances of Redis on a node, each with its own port. So that’s like six machines. Then build a Redis cluster with these six instances.

Step 1: Create a directory for each of the six instances. Why do you want to do this?

Step 2: Since you are going to start six instances of Redis, you naturally need to prepare their own configuration files.

Make 6 copies of redis.conf

Specific configuration of six Redis instances

Specifically, note that each instance should have a different port because it is on a single machine (192.168.99.121); At the same time, each instance will obviously have its own place to store data; Enable AOF mode; Enable cluster configuration. Enable background mode;

Step 3: Actually, the Redis cluster operation is done via Ruby scripts as you will see later, so we need to install Ruby related RPM packages, as well as Redis and Ruby interface packages.

yum install ruby

yum install rubygems

gem install redis

Step 4: Get the Redis cluster working!

Start six instances of Redis

Next, we will create the cluster using Ruby scripts.

Redis-trib.rb is a script that operates on a Redis cluster

[root@mydream121 bin]#./redis-trib.rb create --replicas 1 192.168.99.121:8001 192.168.99.121:8002 192.168.99.121:8003 192.168.99.121 192.168.99.121:8004 192.168.99.121:8005:8006Copy the code

create redis cluster

redis cluster info

Let’s take a look at replicas 1. What does it mean? 1 actually represents a ratio of the number of primary nodes to the number of secondary nodes. When you create a cluster, which nodes are the primary nodes? Which nodes follow from the nodes? The answer is that there will be three primary nodes and then three secondary nodes in the order of IP:PORT in the command. This point can be verified by the two pictures above.

Second, notice the concept of slot in the figure. Slot A slot for storing data in a Redis cluster. There is a slot range for each Master, but not for the Slave. In the Redis cluster, the Master can still read and write, while the Slave is read-only. Data is written to slots, which is different from the previous 1.x Master/Slave data stores (Master/Slave data stores are identical), because the three Master data stores in the Redis cluster are not the same. This point will be verified in subsequent experiments.

Step 5: Verify that the Redis cluster setup is successful

cluster info/cluster nodes

Set up a Redis cluster

Recommended reading:

Carefully organize | 2017 in the second half of the article directories

Possible solutions for strong cache and database consistency

User process buffer and kernel buffer

Introduction to Dynamic Programming through Gold Mine Story (PART 1)

Focus on server background technology stack knowledge summary and sharing

Welcome to pay attention to exchanges and common progress

Coder youdao coding

Code agriculture Youdao, to provide you with easy to understand technical articles, so that technology becomes simpler!