Redis architecture design practice

Posted by sps on Tue, 12 Oct 2021 21:09:06 +0200

catalogue

Master slave replication

Background introduction

Basic architecture

Quick start practice

Principle analysis

        Full synchronization

        Incremental synchronization

Interview questions

        If you were asked to design redis to support 10W + concurrency, what would you do?

        What is the replication mechanism of Redis?

Sentinel mode

introduce

effect

Basic structure

quick get start

Working principle analysis

Redis cluster high availability

introduce

Basic structure

  practice

Interview questions

        hash slot of Redis cluster?

        Will write operations be lost in the cluster?

        How are redis clusters replicated

        What is the maximum number of nodes in the redis cluster?

        How do redis clusters select databases?

Master slave replication

Background introduction

        The reading and writing capacity of each redis is limited (the number of times it can read is about 110000 times / s, and the number of times it can write is 81000 times / s). Therefore, when there is a large amount of data, we can use multiple redis to improve the concurrent data processing ability of redis. Starting multiple redis requires mutual collaboration, so some architecture design is required.

Basic architecture

        

As shown in the figure:

        The master-slave architecture consists of a master server and several slave servers

        The primary server can read and write, and the sub server can only read.

Quick start practice

        1. Make two copies of redis.

cp -r redis01/ redis02

cp -r redis01/ redis03

         2. If the redis service has been started, stop the already started redis service (docker rm -f redis container name) and start a new redis container.

        redis01

docker run -p 6379:6379 --name redis6379 \
-v /usr/local/docker/redis01/data:/data \
-v /usr/local/docker/redis01/conf/redis.conf:/etc/redis/redis.conf \
-d redis redis-server /etc/redis/redis.conf \
--appendonly yes

        redis02

docker run -p 6380:6379 --name redis6380 \
-v /usr/local/docker/redis02/data:/data \
-v /usr/local/docker/redis02/conf/redis.conf:/etc/redis/redis.conf \
-d redis redis-server /etc/redis/redis.conf \
--appendonly yes

        redis03

docker run -p 6381:6379 --name redis6381 \
-v /usr/local/docker/redis03/data:/data \
-v /usr/local/docker/redis03/conf/redis.conf:/etc/redis/redis.conf \
-d redis redis-server /etc/redis/redis.conf \
--appendonly yes

         3. Detect redis service role

        Log in to the three redis container services respectively and view the roles through the info command. By default, the three redis started are all master s.

Input:

127.0.0.1:6379> info replication

Display:

\# Replication
role:master
connected_slaves:0
master_repl_offset:3860
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:3859

You can see that it is the master.

4. View the IP address of redis6379.

Input:

docker inspect redis6379

Display:

......
"Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "c33071765cb48acb1efed6611615c767b04b98e6e298caa0dc845420e6112b73",
                    "EndpointID": "4c77e3f458ea64b7fc45062c5b2b3481fa32005153b7afc211117d0f7603e154",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:02",
                    "DriverOpts": null
                }
            }

You can see that the IP address of redis6379 is 172.17.0.2

        5. Set the Master/slave architecture

        Log in to redis6380/redis6381 respectively and execute the following statements

    slaveof 172.17.0.2 6379 

  Note: if the master has a password, you need to add the statement "masterauth your password" in the redis.conf configuration file of the slave, restart redis, and then execute the slaveof command

        6. Log in to redis6379 again and check info

\# Replication
role:master
connected_slaves:2
slave0:ip=172.17.0.3,port=6379,state=online,offset=2004,lag=1
slave1:ip=172.17.0.4,port=6379,state=online,offset=2004,lag=1
master_failover_state:no-failover
master_replid:5baf174fd40e97663998abf5d8e89a51f7458488
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:2004
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:2004

        7. Log in to redis6379 to test. The master can read and write

[root@centos7964 ~]# docker exec -it redis6379 redis-cli
127.0.0.1:6379> set role master6379
OK
127.0.0.1:6379> get role
"master6379"
127.0.0.1:6379>

        8. Log in to redis6380 to test. The slave can only read but not write

[root@centos7964 ~]# docker exec -it redis6380 redis-cli
127.0.0.1:6379> get role
"master6379"
127.0.0.1:6379> set role slave6380
(error) READONLY You can't write against a read only replica.
127.0.0.1:6379>

Principle analysis

        The master-slave structure can be a master-slave structure. Replication can be divided into full synchronization and incremental synchronization according to whether it is full or not.

        Full synchronization

        Generally, it occurs in the initial stage of Slave and copies all the data in redis.

        Send RDB files to the slave.

        Incremental synchronization

        Copy the newly added data to the slave.

Interview questions

        If you were asked to design redis to support 10W + concurrency, what would you do?

        Generally speaking, single redis cannot achieve 10W + concurrency, so it should be realized through the master-slave architecture and read-write separation mechanism.

        What is the replication mechanism of Redis?

        1) The data is copied to the slave node asynchronously

        2) A master can configure multiple slave nodes

        3) Save can connect to other slave s

        4) When the slave is running, it will not affect the operation of the master

        5) It is mainly used for horizontal expansion and read-write separation

Sentinel mode

introduce

         Sentinel is a mechanism to achieve high availability under the master-slave architecture mode of Redis.
The Sentinel system composed of one or more Sentinel instance s can monitor any number of master servers and all slave servers under these master servers, and automatically upgrade a slave server under the offline master server to a new master server when the monitored master server enters the offline state, Then, the new master server continues to process the command request instead of the offline master server.
        In short, the Sentry will patrol and find that the master is dead. It will pull a slave to replace the host to perform read and write operations.

effect

        Cluster monitoring: it is responsible for monitoring whether the master and save processes work normally

        Message notification: if a redis instance fails, a message will be sent to the administrator

        Failover: if the master hangs up, it will be automatically transferred to the salve

        Configuration center: if a failover occurs, the client will be notified of the new master address

Basic structure

        

quick get start

        1. Enter the three redis containers for configuration respectively, and create sentinel.conf file in the specified directory of the container / etc/redis. The contents of the file are as follows:

sentinel monitor redis6379 172.17.0.2 6379 1

         The above command indicates the master to be monitored, redis6379 is the service name, 172.17.0.2 and 6379 are the ip and port of the master, and 1 indicates how many sentinel s think a master is invalid

         If bash: vi: command not found appears here, you can execute the following two commands in sequence to install vim

apt-get update 
apt-get install vim

        If the network is not good, you can execute the following instructions under the mount directory corresponding to the host, or directly in the container.

cat <<EOF > /etc/redis/sentinel.conf 
sentinel monitor redis6379 172.17.0.2 6379 1
EOF

        2. Execute the following command in the client to start the sentinel service

redis-sentinel sentinel.conf

        3. Close the master

        4. Open the slave for detection

        slave has become master

\# Replication
role:master
connected_slaves:1
slave0:ip=172.17.0.3,port=6379,state=online,offset=222807,lag=0
master_failover_state:no-failover
master_replid:3d63e8474dd7bcb282ff38027d4a78c413cede53
master_replid2:5baf174fd40e97663998abf5d8e89a51f7458488
master_repl_offset:222807
second_repl_offset:110197
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:29
repl_backlog_histlen:222779
127.0.0.1:6379>

Working principle analysis

        1.sentinel sends a ping command to the known master and save every second

        2. If a master does not reply to the ping command within a period of time, generally 30 seconds, it will be marked as offline.

        3. If the master is marked as offline, monitor the sentinel of the master once a second to confirm that the master is offline.

        4. If more than a certain number of sentinel s judge that the master is offline, the master will be offline.

        5. Get master permission for a save

Redis cluster high availability

introduce

         The reliability guarantee of redis single machine mode is not very good, and it is prone to single point of failure. At the same time, its performance is also limited by the processing capacity of CPU. Redis must be highly available in actual development, so the single machine mode is not our destination. We need to upgrade the current redis architecture mode.
Sentinel mode achieves high availability, but only one master is providing services in essence (in the case of read-write separation, the master is also providing services in essence). When the machine memory of the master node is insufficient to support the system data, the cluster needs to be considered.
Redis cluster architecture realizes the horizontal expansion of redis, that is, start n redis nodes, distribute and store the whole data in the n redis nodes, and each node stores 1/N of the total data. Redis cluster provides a certain degree of availability through partition. Even if some nodes in the cluster fail or cannot communicate, the cluster can continue to process command requests.

Basic structure

Generally, at least six nodes, three master s and three slave nodes are set

  practice

        

Step 1: prepare the network environment
The creation of virtual network card is mainly used for redis cluster to communicate with the outside world. Generally, the bridge mode is commonly used.

docker network create redis-net

To view the network card information of docker, you can use the following instructions

docker network ls

To view docker network details, you can use the command

docker network inspect redis-net

Step 2: prepare redis configuration template

mkdir -p /usr/local/docker/redis-cluster
cd /usr/local/docker/redis-cluster
vim redis-cluster.tmpl

Enter the following in redis-cluster.tmpl

port ${PORT}
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-announce-ip 192.168.227.131
cluster-announce-port ${PORT}
cluster-announce-bus-port 1${PORT}
appendonly yes

Each node is explained as follows:

    Port: node port, that is, the port that provides external communication
    Cluster enabled: whether to enable the cluster
    Cluster config file: cluster configuration file
    Cluster node timeout: connection timeout
    Cluster announcement ip: host ip
    Cluster announcement port: cluster node mapping port
    Cluster announcement bus port: cluster bus port
    appendonly: persistent mode

Step 3: create a node profile

Execute the following command in redis cluser

for port in $(seq 8010 8015); \
do \
  mkdir -p ./${port}/conf  \
  && PORT=${port} envsubst < ./redis-cluster.tmpl > ./${port}/conf/redis.conf \
  && mkdir -p ./${port}/data; \
done

    The instruction envsubst < source file > target file is used to update the contents of the source file into the target file

View the contents of the configuration file through the cat instruction

cat /usr/local/docker/redis-cluster/801{0..5}/conf/redis.conf

Step 4: create a redis node container in the cluster

for port in $(seq 8010 8015); \
do \
   docker run -it -d -p ${port}:${port} -p 1${port}:1${port} \
  --privileged=true -v /usr/local/docker/redis-cluster/${port}/conf/redis.conf:/usr/local/etc/redis/redis.conf \
  --privileged=true -v /usr/local/docker/redis-cluster/${port}/data:/data \
  --restart always --name redis-${port} --net redis-net \
  --sysctl net.core.somaxconn=1024 redis redis-server /usr/local/etc/redis/redis.conf; \
done

Where, -- privileged=true means that the container user to be started has true root permission, -- sysctl net.core.somaxconn=1024, which is a linux kernel parameter used to set the size of the request queue. The default value is 128. Subsequent instructions to start redis need to be placed in the request queue first, and then started in turn
After the creation is successful, view the node content through the docker ps instruction.

Step 5: create redis cluster configuration

redis-cli --cluster create 192.168.227.131:8010 192.168.227.131:8011 192.168.227.131:8012 192.168.227.131:8013 192.168.227.131:8014 192.168.227.131:8015 --cluster-replicas 1

The above instructions should be executed in one line as far as possible, and the last 1 represents the master-slave ratio. When the selection prompt message appears, enter yes.

Step 6: connect the redis cluster and add data to redis

redis-cli -c -h 192.168.227.131 -p 8010

other:
During the build process, you may need to stop or directly delete the docker container after a problem occurs. You can use the following reference commands

Batch stop docker container:

docker ps -a | grep -i "redis-801*" | awk '{print $1}' | xargs docker stop

Batch delete docker container:         

docker ps -a | grep -i "redis-801*" | awk '{print $1}' | xargs docker rm -f

Batch delete files

rm -rf 801{0..5}/conf/redis.conf

The above is a simple step to build redis cluster based on docker. It may not be so simple in practical application. This article is only for reference.

Interview questions

        hash slot of Redis cluster?

        redis does not use consistent hash, but introduces the concept of hash slot. redis has 16384 hash slots. Each key takes the module of 16384 after CRC16 verification to decide which slot to put in. Each node of the cluster is responsible for part of the hash slot.

        Will write operations be lost in the cluster?

        The strong consistency of data cannot be guaranteed, and write operations may be lost under specific conditions.

        How are redis clusters replicated

        Asynchronous replication

        What is the maximum number of nodes in the redis cluster?

        16384

        How do redis clusters select databases?

        The database cannot be selected at present. It is 0 database by default.

       

Topics: Database Redis