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 achieve High availability of Redis

2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces how to achieve the high availability of Redis, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

How to achieve high availability of Redis?

To achieve high availability, one machine is certainly not enough, while redis has two options to ensure high availability.

Master-slave architecture

Master-slave mode is the simplest way to achieve high availability, and the core is master-slave synchronization. The principle of master-slave synchronization is as follows:

Slave sends sync commands to master

After receiving the sync, master executes bgsave to generate the full RDB file.

Master records slave write commands to the cache

After the bgsave execution is finished, send the RDB file to slave,slave for execution

Master sends write commands in the cache to slave,slave for execution.

The command I wrote here is sync, but psync has been used instead of sync since the redis2.8 version, because the sync command consumes system resources and psync is more efficient.

Sentinel

Based on the shortcomings of the master-slave solution is still obvious, assuming that the master down, then can not write data, then slave will not be useful, the entire architecture will not be available, unless you switch manually, the main reason is that there is no automatic failure mechanism. The function of sentinel is much more comprehensive than the simple master-slave architecture, it has automatic failover, cluster monitoring, message notification and other functions.

The Sentinel can monitor multiple master and slave servers at the same time, and automatically promote a slave to master when the monitored master is offline, and then the new master continues to receive commands. The whole process is as follows:

Initialize sentinel and replace ordinary redis code with sentinel-specific code

Initialize the masters dictionary and server information, which mainly stores ip:port and records the address and ID of the instance

Create two connections to master, command connection and subscription connection, and subscribe to sentinel:hello channel

Send an info command to master every 10 seconds to get current information about master and all the slave below it

When a new slave is found in master, sentinel and the new slave also establish two connections and send an info command every 10 seconds to update the master information

Sentinel sends ping commands to all servers every 1 second. If a server continuously returns invalid responses within the configured response time, it will be marked as offline.

To elect the lead sentinel, the lead sentinel needs the consent of more than half of the sentinel.

The leader sentinel selects one of all the slave of the offline master and converts it to master

Change all slave to copy data from the new master

When the original master is set as the slave server of the new master, when the original master resumes the connection, it becomes the slave server of the new master.

Sentinel sends ping commands to all instances (including master and slave servers and other sentinel) every second, and determines whether it has been offline based on the reply, which is called subjective offline. When it is judged to be subjectively offline, it will ask other monitored sentinel. If more than half of the votes think it is offline, it will be marked as objective offline and a failover will be triggered.

Thank you for reading this article carefully. I hope the article "how to achieve the High availability of Redis" shared by the editor will be helpful to everyone. At the same time, I also hope that 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

Development

Wechat

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

12
Report