Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of redis Master-Slave replication, Sentinel and Cluster

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the example analysis of redis master-slave replication, sentinel and cluster, which has a certain reference value. Interested friends can refer to it. I hope you can learn a lot after reading this article.

First, master-slave copy 1. The use of master-slave synchronization

Through the persistence function,    ensures that data will not be lost even when the server is rebooted, because persistence will save the data in memory to the hard disk, and reboot will load the data from the hard disk, but because the data is stored on a server, if the server has hard disk failure and other problems, it will also lead to data loss. To avoid a single point of failure, it is common practice to replicate multiple copies of the database for deployment on different servers, so that even if one server fails, other servers can continue to provide services. To this end, redis provides a replication replication function, which can automatically synchronize the updated data to other databases when the data in one database is updated.

   in the concept of replication, databases are divided into two categories, one is the master database master, and the other is the slave database slave. The master database can read and write, and when the write operation causes data changes, the data is automatically synchronized to the slave database, while the slave database is generally read-only and receives the synchronized data from the master database. A master database can have multiple slave databases, while a slave database can have only one master database.

two。 Master-slave synchronization principle 2.1 principle is explained in detail

If you start a Slave machine process, it sends a sync_command command to the Master machine requesting a synchronous connection.

Whether you connect for the first time or reconnect, the Master machine starts a background process to save the data snapshot (RDB) to the data file (.rdb file), and Master records all commands that modify the data and caches it in the data file.

After the background process completes the cache operation, the Master machine will send the data file to the Slave machine, and the Slave machine will save the data file to the hard disk, then load it into memory, and then the Master machine will send all the operations to modify the data to the Slave machine. If there is an outage caused by a Slave failure, it will automatically reconnect when it returns to normal.

After receiving the connection from the Slave machine, the Master machine sends its complete data file to the Slave machine. If the Master receives synchronization requests from multiple Slave at the same time, Master will start a process in the background to save the data file, and then send it to all Slave machines to ensure that all Slave machines are normal.

RDB does full synchronization, AOF does incremental synchronization

2.2 theoretical simplification

Slave-> master sends sync command request synchronization master main process-> call fork () function to derive RDB child process for persistence-> generate RDB file to push RDB file to slaves (complete full synchronization) # incremental synchronization: use AOF persistence (mechanism: save cached data to buffer) Therefore, both master and slave nodes need to enable AOF incremental synchronization through the AOF function to append (append) the data in the cache to the buffer for master buffering-> slave buffered synchronization in the continuous running process. It is also the process of incremental continuous synchronization 2.3.Finally simplified version slave-> master sends syncmaster using RDB to generate .rdb file (full synchronization) to slavesmaster using AOF to synchronize buffer data to slaves buffer data (increment) II, Sentinel mode 1. The role of sentinels

The emergence of the Sentinel mainly solves the problem that human intervention is needed when master-slave replication fails.

Main functions of Sentinel mode:

Cluster monitoring: responsible for monitoring whether redismaster and slave processes are working properly message notification: if a redis instance fails, the sentry is responsible for sending a message to the administrator as an alarm notification of failover: if the master node is down, it will be automatically transferred to the configuration center on the slave node: if the failover occurs, notify the client client of the new master address

   uses a system composed of one or more Sentinel sentinel instances to monitor the redis node. When the master node fails, it can upgrade one of the slave nodes to the master node and fail over to ensure the availability of the system.

two。 Detailed explanation of Sentinel principle 2.1

First of all, the information of the master node is configured in the configuration file of the Sentinel sentinel.

The sentinel node establishes two connection command connections and subscription connections with the configured master node.

PS:redis publish subscription (pub/sub) is a mode of message communication: the sender (pub) sends the message and the subscriber (sub) receives the message.

The sentry sends the INFO command every 10 seconds through the command connection, and through the INFO command, the master node returns its own run_id and its own slave node information.

The Sentinel will also establish two connection command connections and subscription connections to these slave nodes.

The sentry sends INFO commands to the slave node through the command connection to get some information about him:

Run id (redis server id) role (function) offset other replication offsets from the server

Send a message to the server's sentinel:hello channel through a command connection, including your own IP, port, run id, configuration (which will be used later in voting), and so on.

The server's sentinel:hello channel is monitored through a subscription connection, and all sentry messages sent to that channel can be accepted.

By parsing the monitored messages and analyzing and extracting them, we can know that other sentinel service nodes are also listening to these master and slave nodes, and update the structure to record these sentinel nodes.

Establish command connections to other observed sentinel nodes (there is no subscription connection at this time).

2.2 principle simplification

3 sentinels, 3 redis.

Establish a command connection between the three sentinels and periodically check the status of "teammates"

The Sentinel sends two connections to the master node (which has been specified in the configuration file), namely the command connection and the subscription connection (in order to obtain the data of the master node periodically)

The sentry periodically sends info commands to master, and the master (if alive) returns the information of the redis-cli info replication master node + the location of the slave node.

The sentry sends the info command to the slaves node through the information returned by master, and the slaves returns the data, so that the sentry cluster can obtain all the cluster information of the redis.

The Sentinel will send commands to the server to establish its own hello channel, and the Sentinel will establish subscriptions to this hello channel for message sharing between Sentinels.

2.3 ideas

Three sentinels listen to each other and use ping to check for survival.

Three sentinels send commands to the data node master to connect and subscribe to the connection (info command) to obtain data node information (including master and slave nodes). Three sentinels send info to other slave nodes to obtain slave node details

Message sharing between the three sentinels through the hello channel

3. Fault Migration in Sentinel Mode

① subjective offline

The Sentinel node sends PING commands to the instance that has established a command connection at a frequency of once per second. If there is no valid response within down-after-milliseconds milliseconds, including a response other than PONG/LOADING/MASTERDOWN, the Sentinel will mark the status of the instance in this structure as SRI_S_DOWN subjective offline.

② objective offline

When a Sentinel node finds that the master node is subjectively offline, it will ask other Sentinel nodes whether the node has been subjectively offline. If the configuration parameter quorum is exceeded and the nodes are considered to be subjectively offline, the sentinel node will mark the master node in the structure it maintains as the SRIO DOWN objective offline query command SENTINEL is-master-down-by-addr.

③ master Election

If the master node is considered to be offline objectively, an election will be held between the sentinel nodes with the command SENTINEL is-master-down-by-addr, but runid will bring its own runid into it this time, hoping that the recipient will set itself as the master node. If more than half of the nodes return to mark the node as leader, the leader will migrate the failure.

④ failover

# if the new master node is selected from the slave node and the communication priority of the new master node is the same, select the highest offset # set the node to the new master node SLAVEOF no one, and ensure that the node returns to master # when the subsequent INGO command sets the other slave nodes to copy from the new master node SLAVEOF command # turn the old master node into the slave node of the new master node PS: advantages and disadvantages # advantages: high availability, Sentinel mode is based on master-slave mode, all the advantages of master-slave mode, Sentinel mode has Master and slave can be switched automatically, the system is more robust, and the availability is higher # disadvantages: it is difficult for redis to support online capacity expansion, which will become very complex when the cluster capacity reaches the upper limit. 3. Cluster 1. The meaning of redis cluster

The master node is responsible for the maintenance of read and write requests and cluster information, and the slave node only replicates the master node data and status information.

The sentinel mode of    redis can basically achieve high availability and read-write separation, but in this mode, every redis server stores the same data, which is a waste of memory resources, so Cluster cluster mode is added to redis3.0 to achieve redis distributed storage, that is, each redis node stores different content. According to the official recommendation, cluster deployment should have at least 3 master nodes, preferably with a mode of 3 masters, 3 slaves and 6 nodes.

   Cluster cluster is a distributed network service cluster composed of multiple redis servers. There are multiple master master nodes in the cluster, each of which is readable and writable. The nodes communicate with each other and are connected. The redis cluster has no central node.

2. Characteristics of redis cluster

In the redis-Cluster cluster, you can add slave nodes to each master node, master nodes and slave nodes directly follow the characteristics of the master-slave model. When users need to handle more read requests, adding slave nodes can expand the read performance of the system.

Failover of redis-cluster: the host node of the redis cluster has built-in node failure detection and automatic failover features similar to redis sentinel. When a master node in the cluster goes offline, other online master nodes in the cluster will notice this and fail over the master node that has gone offline.

The method of cluster failover is basically the same as that of redis sentinel, except that in the cluster, the failover is carried out by other online master nodes in the cluster, so the cluster does not have to use redis sentinel separately.

IV. Distributed lock

Https://www.zhihu.com/question/300767410/answer/1749442787

   if in a distributed system, we read a data from the database, and then modify and save, this situation is easy to encounter concurrency problems. Because read and update save is not an atomic operation, it can lead to incorrect data when concurrent. In fact, this kind of scene is not uncommon, such as e-commerce flash sale activity, inventory updates will be encountered. If it is a stand-alone application, the direct use of local locks can be avoided. If it is a distributed application, the local lock is not useful, so it is necessary to introduce distributed lock to solve the problem. Thus it can be seen that the purpose of distributed locking is actually very simple, which is to ensure that only one server executes when multiple servers execute a certain piece of code.

To put it simply:

   's current business applications are usually micro-service architecture, which also means that an application will deploy multiple processes. If multiple processes need to modify the same row of records in the database, in order to avoid data errors caused by out-of-order operations, distributed locks need to be introduced to solve the problem.

In order to ensure the availability of distributed locks, at least ensure that the implementation of the lock meets the following points:

Repulsion. At any time, ensure that only one client holds the lock.

There can be no deadlocks. If a client crashes while it is holding a lock, make sure that subsequent clients can lock it.

Make sure that both locking and unlocking are the same client.

In general, distributed locks can be implemented in the following ways:

Use MySQL, based on a unique index.

Using ZooKeeper, based on temporary ordered nodes.

Use Redis, based on the setnx command.

For redis, pay attention to three points: lock the key. If the request for renewal of the expired key is not completed, request the unlocking of the key after completion. Prevent a key read in a concurrent environment from being modified by multiple requests, resulting in invalid operations and waste of resources.

V. redis summary

Redis can be used as the pre-cache database of mysql. The way to connect redis with mysql needs to configure thread pool and define the location of back-end mysql (location of IP + port + sock files)

Redis basic function: fast storage (read) for memory / cache

How to do it:

Storing data in memory / cache by default has a wealth of data types: string list hash set & & order set and other important data persistence functions, persistence mode: AOF RDB

Single-threaded mode-> one of the reasons for its high speed: Epoll + I ram O reuse (slots hash slot in cluster can act as an index for data reading and fetching)

Algorithm in redis:

LRU: elimination strategy 1) random elimination of data in cache 2) random elimination of data with expiration time set in cache 3) data with expiration time set in cache will be deleted lazily (only if the accessed data expires) 4) memory will be full during continuous data storage The recent elimination token bucket + leaky bucket algorithm: current-limited Raft: election mechanism is used to elect a new master node in the data with expiration time set.

The Mechanism of redis caching Hot data

Hot data: the data with high hit times specify to improve the hit number of the data in the cache, the most direct can browse the script, access these data 6, system optimization 1. Singleton server, server itself optimization

Hardware resource selection (five major resources of the system)

Disk solid state disk SCSI (hardware disk array)

Server memory bar selection (local server and cloud server)

CPU core number selection

Network card (local server and cloud server), you need to consider the network traffic QPS under load pressure.

Server selection (Kirin, Xiaolong, Chao Yingxin, Huawei, Hua3, Dell (type: blade, tower, cabinet)

The above costs need to be calculated, and the percentage of performance consumed by the services on the server at run time needs to be taken into account (some resources need to be reserved for the system)

The choice of service environment

Operating system chooses Linux distribution: centos ubuntu redhat server debian alphon mac SUSE (PS: virtualized KVM XEN FUFE)

Based on operating system, dependent on environment. Choose to minimize the installation or specify the installation of the operating system version + specify the kernel version. Whether the software is dependent (for example, tomcat requires JDK, compilation requires gcc gcc-c++ pcre. )

Five major loads of software resource optimization + kernel optimization (TCP protocol correlation, queue dependence, route forwarding, redirection, port, number of file openings, system software and hardware restrictions, etc.)

two。 Optimization of singleton server application service itself

Take redis as an example

First of all, from the recovery files that start reading, you need to enable the AOF function based on AOF (RDB default)

Selection and determination of save M N trigger period in RDB, which will affect the use of disk resources

Select the appropriate syncwrite synchronous write to disk policy everysecond in AOF

When using it, you need to consider the amount of memory used (OOM)

Memory elimination strategy: lazy elimination + periodic deletion, prohibited elimination + periodic deletion. Select the appropriate phase-out strategy (defined in the configuration file) according to the situation.

Persistence direction

The persistence feature not only ensures data integrity, but also continuously exerts storage pressure on the disk (the pressure comes from the data files generated by AOF and RDB, and the log files of AOF and RDB).

Regular archiving of data / log files

Partition of log files (saved in the center of the log)

Shared storage NFS GFS fastDFS

Redis main process

Two redis main processes can be used together to achieve backup redundancy and improve the ability to resist high concurrency.

Thank you for reading this article carefully. I hope the article "sample Analysis of redis Master-slave replication, Sentinel and Cluster" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report