I. Sentinel mode

Prior to Redis3.0, Redis used the Sentinel architecture, which uses the Sentinel tool to monitor the status of master nodes. If the master node is abnormal, the slave node is used as the master.

Disadvantages of sentinel mode:

(1) When the master fails, sentinel will elect a master. During the election, there is no way to access Redis, and the access will be interrupted. If the master is suspended during the promotion of e-commerce websites, a lot of order data will be lost in a few seconds.

(2) In sentinel mode, only master node can be used for writing, while slave node can only be used for reading. Although Redis single node supports up to 10W QPS, the pressure of writing data is all on the master when e-commerce is promoting.

(3) The single node memory of Redis cannot be set too large. If the data is too large, the master/slave synchronization will be slow; It takes a long time to start a node. (The slave node has all the data of the master node)

Second, Redis cluster

1. Introduction to Redis cluster

Redis cluster is a distributed service cluster composed of multiple master and slave nodes with replication, high availability and sharding features. The Redis cluster can perform node removal and failover without sentinel sentinel. Each node needs to be set to cluster mode, which has no central node and can scale horizontally and linearly to tens of thousands of nodes according to official documentation (no more than 1000 nodes are officially recommended). Redis clusters provide better performance and high availability than previous versions of Sentinel mode, and cluster configuration is very simple.

2. Advantages of Redis cluster:

(1) Redis cluster has multiple masters, which can reduce the impact of access transient problem;

If a master in the cluster is suspended and data needs to be written to the master, the operation needs to wait. However, writing data to other master nodes is unaffected.

(2) Redis cluster has multiple masters, which can provide higher concurrency;

(3) Redis cluster can be fragmented storage, so that more data can be stored;

3. Set up Redis cluster

The cluster construction of Redis requires at least 3 master nodes. Here we set up 3 master nodes, each with a slave node attached below, and there are 6 Redis nodes in total. (3 machines, one master and one slave for each machine)

First machine: 192.168.1.1 8001 port 8002 port

Second machine: 192.168.1.2 8001 port 8002 port

Third machine: 192.168.1.3 8001 port 8002 port

Step 1: Create a folder

mkdir -p /usr1/redis/redis-cluster/8001 /usr1/redis/redis-cluster/8002
Copy the code

Step 2: Copy the redis. Conf files from the redis installation directory to the 8001 directory

Cp/usr1 / redis - 5.0.3 / redis. Conf/usr1 / redis/redis cluster / 8001Copy the code

Step 3: Modify the following contents of the redis.conf file

Port 8001 daemonize yes pidfile "/var/run/redis\_8001.pid" Dir /usr1/redis/redis-cluster/8001/ # Start cluster mode cluster-enabled yes Cluster \-config-file nodes-8001.conf # Timeout duration of the node offline cluster\-node-timeout 5000 #bind 127.0.0.1 # disable protected\-mode no # start the AOF file appendonly yes # Add the following configuration to set the password: Requirepass redis\-pw # Set the password for accessing nodes in the cluster to masterauth redis\-pwCopy the code

Step 4: Copy the configuration file to 8002 and change 8001 to 8002:

cp /usr1/redis/redis-cluster/8001/redis.conf /usr1/redis/redis-cluster/8002 cd /usr1/redis/redis-cluster/8002/ vim Redis. conf # Batch modify string :%s/8001/8002/gCopy the code

Step 5: Copy the files from the local machine (192.168.1.1) to the other two machines

SCP/usr1 / redis/redis - cluster / 8001 / redis. Conf [email protected]: / usr1 / redis/redis cluster / 8001 / SCP / usr1 / redis/redis - cluster / 8002 / redis. Conf [email protected]: / usr1 / redis/redis cluster / 8002 / SCP / usr1 / redis/redis - cluster / 8001 / redis. Conf [email protected]: / usr1 / redis/redis cluster / 8001 / SCP / usr1 / redis/redis - cluster / 8002 / redis. Conf [email protected]: / usr1 / redis/redis cluster / 8002 /Copy the code

Step 6: Start each of the six Redis instances and check if they are started successfully

/ usr1 / redis/redis - 5.0.3 / SRC/redis server/usr1 / redis/redis - cluster / 8001 / redis. Conf / usr1 / redis/redis - 5.0.3 / SRC/redis server/usr1 / redis/redis - cluster / 8002 / redis. Conf ps - ef | grep redisCopy the code

Step 7: Create the entire Redis cluster using redis-cli (Ruby script redis-trib.rb before Redis5.0)

Run the above commands to complete the setup

/ usR1 /redis/redis-5.0.3/ SRC /redis-cli -a redis-pw --cluster create --cluster-replicas 1 192.168.1.1:8001 192.168.1.1:8002 192.168.1.2:8001 192.168.1.2:8002 192.168.1.3:8001 192.168.1.3:8002Copy the code

Description:

-a: indicates the password.

–cluster-replicas 1: indicates that a slave is connected to a master. –cluster-replicas 2: indicates that a master has two slaves attached to it.

Extension:

View the help command: SRC /redis‐cli –cluster help

Host1 :port1... HostN :portN call: you can run the redis command add\-node: to add a node to the cluster. The first parameter is the IP address of the new node :port. The second parameter is the IP address of any existing node in the cluster :port del\-node: Reshard: refragments a node. Check: checks the cluster statusCopy the code

Step 8, verify the cluster

(1) Connect to any client

/ usR1 /redis/redis-5.0.3/ SRC /redis-cli -a redis-pw -c -h 192.168.1.1 -p 8001Copy the code

‐ A indicates the server password. ‐ C denotes cluster mode; -h Specifies the IP address. -p indicates the port number

(2) View cluster info

(3) View the node list: The master of cluster Nodes slave can also be viewed from above.

You can see from above which slave hangs under which master;

The node information is stored in the/usR1 /redis/redis-cluster/8001/nodes-8001.conf file.

(4) Data operation verification;

(5) To shut down clusters one by one, run the following command:

/usr/local/redis‐5.0.3/ SRC /redis‐cli ‐a redis-pw ‐c ‐h 192.168.1.1 ‐p 8001 shutdown /usr/local/redis‐5.0.3/ SRC /redis‐cli ‐a Redis - PW ‐c ‐h 192.168.1.1 ‐p 8002 shutdown......Copy the code

Note: When creating a cluster, you need to turn off the fire protection on all nodes to ensure that the Redis service port can communicate with the gossip port.

Systemctl stop Firewalld # Temporarily disable the firewall

Systemctl disable firewalld

4. Redis cluster principle analysis

The Redis Cluster divides all data into 16384 slots, some of which each node is responsible for. Slot information is stored in each node. Only master nodes are allocated slots. Slave nodes are not allocated slots.

When the client of the Redis Cluster connects to the Cluster, it also gets a slot configuration information of the Cluster and caches it locally to the client. In this way, when the client wants to find a key, it can directly locate the target node. In addition, slot information may be inconsistent between the client and server. Therefore, a correction mechanism is required to verify and adjust slot information.

Slot location algorithm

By default, the Cluster hashes the key using the CRC16 algorithm to obtain an integer value, and then modulates 16384 to obtain the specific slot.

HASH_SLOT = CRC16(key) % 16384

Jump relocation

When a client sends a command to a node, the node first calculates the key of the command to obtain slot information and determines whether the calculated slot is managed by the node. If the slot is not managed by the current node, it sends a special jump instruction to the client with the address of the node to which the target operation is to be performed, telling the client to connect to the node to obtain data. After receiving the command, the client not only redirects to the correct node, but also synchronously updates and corrects the local slot mapping table cache. All subsequent keys use the new slot mapping table.

Redis communication mechanism between cluster nodes

Cluster metadata can be maintained in centralized mode and gossip mode. Communication between Redis cluster nodes adopts gossip protocol to communicate

(1) Centralized:

Advantages: Metadata update and read, the timeliness is very good, once the metadata changes will be immediately updated to the centralized storage, other nodes can immediately perceive when read;

Disadvantages: All metadata update pressure is concentrated in one place, which may cause metadata storage pressure. Zookeeper uses this method

(2) Gossip:

The gossip protocol contains multiple messages, including ping, pong, meet, fail, and so on.

Advantages: Metadata update is scattered, not centralized in one place, update requests will be intermittent, hit all nodes to update, there is a certain delay, reduce the pressure;

Disadvantages: Delays in metadata updates may cause some delays in cluster operations.

Each node has a dedicated port for communication between nodes, that is, the port number of the service provided by the node +10000. For example, port 17001 is used for communication between nodes. Each node sends ping messages to several other nodes at regular intervals, and the other nodes return pong messages after receiving the ping messages.

The network jitter

Network jitter is a very common phenomenon in which parts of a connection suddenly become inaccessible and then quickly recover. To solve this problem, Redis Cluster provides cluster-node-timeout, which indicates that a node is faulty and needs to be switched over only when the disconnection time of the node exceeds timeout. Without this option, network jitter can cause frequent master/slave switching (data replication).

Redis cluster election principle

When the slave master fails, it attempts to perform a FailOver to become the new master. Since the master has multiple slaves, these slaves must compete to become the master node. The process is as follows:

(1) Both Slave1 and Slave2 find that the master state of the connection changes to Fail.

(2) They increment their recorded cluster currentEpoch (election cycle) by one and broadcast the FailOver_auth_request message using the gossip protocol;

(3) Other nodes receive the message from Slave1 and Salve2 (only master responds), judge the validity of the request, and send FailOver_auth_ack to Slave1 or SlavE2 (only one ACK for each epoch); In an election cycle, a master responds only to the first slave that sends it a message;

(4) Slave1 collects the failover_auth_ACKS returned. It becomes a new master after receiving more than half of the master acks. (This is why a cluster needs at least three master nodes. If there are only two master nodes and one of them fails, there will not be an election.)

(5) The new master node broadcasts the Pong message to inform other cluster nodes, so there is no need to vote any more.

The slave node does not attempt to initiate an election as soon as the master node enters the FAIL state, but there is a certain delay. A certain delay ensures that we wait for the FAIL state to spread across the cluster. If the slave attempts to vote immediately, other masters may not be aware of the FAIL state and may refuse to vote.

DELAY = 500ms + Random (0-500ms) + SLAVE_RANK * 1000ms

SLAVE_RANK: indicates the rank of the total data copied by the slave from the master. A smaller Rank indicates that the replicated data is newer. This way, the slave with the latest data would call the election first (in theory).

Why does a Redis cluster need at least three master nodes and an odd number of nodes is recommended? To elect a new master, more than half of the master nodes in the cluster must agree. If there are only two master nodes, if one of them fails, the condition for electing a new master cannot be met. An odd number of master nodes can save one node on the basis of meeting the election condition. For example, compared with a cluster with three master nodes and four master nodes, if everyone fails a master node, they can elect a new master node. If both master nodes fail, it is impossible to elect new master nodes. Therefore, the odd number of master nodes is more to save machine resources.

Conf configuration cluster-require-full-coverage: No indicates that the cluster is still available when the primary library responsible for a slot is offline and no corresponding secondary library is used for fault recovery. If yes, the cluster is unavailable.

5, Java operation Redis cluster

Mode 1 (Jedis) :

(1) Introducing maven coordinates of Jedis

< the dependency \ > < groupId \ > redis. Clients < / groupId \ > < artifactId \ > jedis < / artifactId \ > < version \ > 2.9.0 < / version \ > </dependency\>Copy the code

(2) Code written in Java, as follows:

public class JedisClusterTest { public static void main(String\[\] args) throws IOException { JedisPoolConfig config \= new JedisPoolConfig(); config.setMaxTotal(20); config.setMaxIdle(10); config.setMinIdle(5); Set<HostAndPort> jedisClusterNode = new HashSet<HostAndPort>(); JedisClusterNode. Add (new HostAndPort (8001) "192.168.1.1,"); JedisClusterNode. Add (new HostAndPort (8002) "192.168.1.1,"); JedisClusterNode. Add (new HostAndPort (8001) "192.168.1.2 instead,"); JedisClusterNode. Add (new HostAndPort (8002) "192.168.1.2 instead,"); JedisClusterNode. Add (new HostAndPort (" 192.168.1.3 ", 8001)); JedisClusterNode. Add (new HostAndPort (" 192.168.1.3 ", 8002)); JedisCluster jedisCluster \= null; Try {//connectionTimeout: indicates the waiting time for connecting to a URL //soTimeout: JedisCluster = new jedisCluster (jedisClusterNode, 6000, 5000, 10, "redis-pw", config); System.out.println(jedisCluster.set("name", "zhangsan")); System.out.println(jedisCluster.get("name")); } catch (Exception e) { e.printStackTrace(); } finally { if (jedisCluster ! = null) { jedisCluster.close(); }}}}Copy the code

Method 2: SpringBoot integrates Redis

(1) Introducing Maven coordinates

< the dependency \ > < groupId \ > org. Springframework. Boot < / groupId \ > < artifactId \ > spring ‐ boot ‐ starter ‐ data ‐ redis < / artifactId \ > < / dependency \ > < the dependency \ > < groupId \ > org.apache.com mons < / groupId \ > < artifactId \ > Commons ‐ pool2 < / artifactId \ > </dependency\>Copy the code

(2) Configuration file

server: port: 8080 spring: redis: database: 0 timeout: 3000 password: redis\-pw cluster: nodes: 192.168.0.61:8001192168 0.62:8002192168 0.63:8003192168, 0.61, 8004192168, 0.62, 8005192168, 0.6 006 lettuce: : Pool: Max ‐idle: 50 min‐idle: 10 Max ‐active: 100 Max ‐wait: 1000Copy the code

(3) Java code, as follows:

@RestController public class TestController { private static final Logger logger = LoggerFactory.getLogger(TestController.class); @Autowired private StringRedisTemplate stringRedisTemplate; @RequestMapping("/test\_cluster") public void testCluster() throws InterruptedException { stringRedisTemplate.opsForValue().set("user:name", "wangwu"); System.out.println(stringRedisTemplate.opsForValue().get("user:name")); }}Copy the code

Author: the wind stop the rain from links: www.cnblogs.com/yufeng218/p… Source: cnblogs