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

How to realize Cluster with redis

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

Share

Shulou(Shulou.com)06/01 Report--

This article is about how redis implements clustering. The editor thought it was very practical, so I shared it with you as a reference. Let's follow the editor and have a look.

1. Master-slave replication

Principle of master-slave replication:

Connect to the master server from the slave server and send the SYNC command

After receiving the SYNC naming, the master server starts executing the BGSAVE command to generate the RDB file and uses a buffer to record all write commands executed thereafter

After the master server BGSAVE executes, it sends snapshot files to all slave servers and continues to record the write commands executed during the sending period.

After receiving the snapshot file from the server, discard all old data and load the received snapshot

After the master server snapshot is sent, it begins to send write commands in the buffer to the slave server.

Finish loading the snapshot from the server, receive command requests, and execute write commands from the autonomous server buffer; (initialization from the server is complete)

Each time the master server executes a write command, it sends the same write command to the slave server, receives and executes the received write command (the operation after initialization from the slave server)

Advantages and disadvantages of master-slave replication:

Advantages:

Master-slave replication is supported, and the host automatically synchronizes the data to the slave, allowing read-write separation.

In order to offload the read operation pressure of Master, the Slave server can provide read-only service for the client, and the write service must still be done by Master.

Slave can also accept connection and synchronization requests from other Slaves, which can effectively offload the synchronization pressure of Master.

Master Server provides services to Slaves in a non-blocking manner. So during Master-Slave synchronization, the client can still submit queries or modify requests.

Slave Server also completes data synchronization in a non-blocking manner. During synchronization, if a client submits a query request, Redis returns the data before synchronization

Disadvantages:

Redis does not have automatic fault tolerance and recovery features, and the downtime of the host slave will cause the read and write requests of the frontend to fail. You need to wait for the machine to restart or manually switch the IP of the frontend to recover.

The host is down, some of the data can not be synchronized to the slave in time before the downtime, and the problem of data inconsistency will be introduced after switching IP, which reduces the availability of the system.

It is difficult for Redis to support online expansion, and it will become very complicated when the cluster capacity reaches the upper limit.

two。 Sentinel mode

When the master server is out of service, you can upgrade a slave server to continue to provide services, but this process needs to be done manually. For this reason, the Sentinel tool is provided in Redis 2.8 to achieve automated system monitoring and fault recovery functions.

The role of the Sentinel is to monitor the operation of the Redis system. Its functions include the following two.

(1) monitor whether the master server and slave server are running properly.

(2) when the master server fails, the slave server will be automatically converted to the master server.

The way Sentinels work:

Each Sentinel process sends a PING command to the Master master server, Slave slave server, and other Sentinel processes throughout the cluster at a frequency of once per second.

If the time from the last valid reply to a PING command to an instance (instance) exceeds the value specified by the down-after-milliseconds option, the instance will be marked as subjective SDOWN by the Sentinel (Sentinel) process.

If a Master master server is marked as subjective offline (SDOWN), all Sentinel processes monitoring the Master master server should confirm that the Master master server has indeed entered the subjective offline state at a frequency of once per second.

When a sufficient number of Sentinel (Sentinel) processes (greater than or equal to the value specified in the profile) confirm that the Master master server has entered the subjective offline state (SDOWN) within the specified time range, the Master master server will be marked as objective offline (ODOWN).

In general, each Sentinel process sends INFO commands to all Master master servers and Slave slave servers in the cluster every 10 seconds.

When the Master master server is marked as objective offline (ODOWN) by the Sentinel process, the frequency of INFO commands sent by the Sentinel process to all Slave slaves of the offline Master master server will be changed from once per second to once per second.

If there are not enough Sentinel processes to allow the Master master server to go offline, the objective offline state of the Master master server will be removed. If the Master master server re-sends the PING command to the Sentinel process to return a valid reply, the subjective offline state of the Master master server will be removed.

Advantages and disadvantages of Sentinel Model

Advantages:

Sentinel mode is based on master-slave mode, all the advantages of master-slave mode, Sentinel mode has.

The master and slave can be switched automatically, so the system is more robust and more available.

Disadvantages:

It is difficult for Redis to support online expansion, and it will become very complicated when the cluster capacity reaches the upper limit.

3.Redis-Cluster cluster

Redis's sentinel mode 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, so cluster mode is added to redis3.0 to achieve redis distributed storage, that is, different content is stored on each redis node.

Redis-Cluster adopts a centerless structure, which has the following characteristics:

All redis nodes are interconnected with each other (PING-PONG mechanism), and binary protocols are used internally to optimize transmission speed and bandwidth.

The fail of a node takes effect only when it is detected by more than half of the nodes in the cluster.

The client is directly connected to the redis node and does not need an intermediate proxy layer. The client does not need to connect to all the nodes in the cluster, but to any of the available nodes in the cluster.

Mode of work:

On every node of the redis, there are two things, one is the slot (slot), its value range is: 0-16383. Another is cluster, which can be understood as a plug-in for cluster management. When our access key arrives, redis will get a result according to crc16's algorithm, and then calculate the remainder of the result to 16384, so that each key will correspond to a hash slot numbered between 0 and 16383, through this value, to find the node corresponding to the corresponding slot, and then automatically jump to the corresponding node for access operation.

To ensure high availability, the redis-cluster cluster introduces the master-slave mode, in which a master node corresponds to one or more slave nodes. When the master node goes down, the slave node is enabled. When other master nodes ping a master node A, if more than half of the master nodes time out to communicate with A, then the master node An is considered to be down. If both master node An and its slave node A1 are down, the cluster can no longer provide services.

Thank you for reading! So much for sharing on how to implement the cluster in redis. I hope the above content can be of some help to you so that you can learn more knowledge. If you think the article is good, you can share it and let more people see it.

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