Home  >  Article  >  Database  >  Quickly understand the stand-alone, master-slave, sentinel and cluster modes in Redis

Quickly understand the stand-alone, master-slave, sentinel and cluster modes in Redis

青灯夜游
青灯夜游forward
2021-05-06 11:08:322564browse

This article will take you through the four modes in Redis: stand-alone, master-slave, sentinel, and cluster. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

Quickly understand the stand-alone, master-slave, sentinel and cluster modes in Redis

Less code, more hair

In the first week of employment, I was cheated

I just joined a new company recently. I originally thought that when I first come to a new company, I would usually get familiar with my colleagues in the company, look at the engineering documents in the group, and find a few demos to practice on my own.

Cough cough cough, I never expected it, everything is what I thought, I am still too young.

On the afternoon of the day I joined, the team leader threw me a few documents and asked me to look at the caching system problems of these projects, and asked me to upgrade redis to sentinel mode.

When I received the mission, I was confused inside.

Quickly understand the stand-alone, master-slave, sentinel and cluster modes in Redis

#First, I don’t know what types of services use redis.

Second, I don’t know how to use redis.

Third, if redis hangs, will it affect users?

Fourth, I have never used redis at all.

Although I have never done it before, I am not afraid. After all, if you do the same work that has done every day, then there is something wrong and it will be optimized quickly.

It seems that social recruitment and school recruitment are still different. School recruitment will have some induction training or newbie classes.

Through these forms of education, first, understand the company's culture and values, and second, learn the work process and feel the company's technical atmosphere.

Task

Upgrade all redis services in our department to Sentinel mode. [Related recommendations: Redis video tutorial]

Multiple modes of redis

They all mentioned upgrading to sentinel mode, but the one used before was not Sentry mode, there are definitely other modes.

Single-machine mode, master-slave mode, sentinel mode, cluster mode

Single-machine mode

This is the simplest and can be understood at a glance.

Just install a redis, start it, and call the business. I won’t go into details about the specific installation steps and startup steps, just search them online.

Single machines are also used in many scenarios, such as in situations where high availability is not necessarily guaranteed.

Ahem, cough, cough, actually our service uses redis stand-alone mode, so let me change it to sentinel mode.

Let’s talk about the advantages and disadvantages of a single machine.

Advantages:

  • Easy to deploy, 0 cost.
  • Low cost, no spare nodes, no other expenses required.
  • High performance, no need to synchronize data on a single machine, and data is naturally consistent.

Disadvantages:

  • The reliability guarantee is not very good, and there is a risk of downtime on a single node.
  • The high performance of a single machine is limited by the processing power of the CPU, and redis is single-threaded.

The choice of stand-alone mode needs to be based on your own business scenario. If high performance and reliability are required, stand-alone mode is not suitable.

Master-slave replication

Master-slave replication refers to copying data from one Redis server to other Redis servers.

The former is called the master node (master), and the latter is called the slave node (slave); data replication is one-way, and can only be from the master node to the slave node.

Quickly understand the stand-alone, master-slave, sentinel and cluster modes in Redis

# Master-slave mode configuration is very simple. You only need to configure the ip and port number of the master node on the slave node.

slaveof <masterip> <masterport>
# 例如
# slaveof 192.168.1.214 6379</masterport></masterip>

Start all services of master-slave nodes, and check the log to see the service connection between master-slave nodes.

It is easy to think of a problem from the above. Since master-slave replication means that the data of master and slave are the same, there is a data redundancy problem.

In program design, redundancy is allowed for high availability and high performance. I hope everyone will take this into consideration when designing the system and avoid saving this resource for the company.

For products that pursue the ultimate user experience, downtime is absolutely not allowed.

The master-slave model is considered in many system designs. A master is hung on multiple slave nodes. When the master service goes down, a new master node will be elected to ensure service. high availability.

Advantages of the master-slave mode:

  • Once the master node goes down, the slave node can be used as the backup of the master node. Come up anytime.

  • Expand the reading capability of the primary node to share the reading pressure of the primary node.

  • Cornerstone of high availability: In addition to the above functions, master-slave replication is also the basis for the implementation of sentinel mode and cluster mode. Therefore, master-slave replication is the cornerstone of Redis high availability.

also has corresponding shortcomings, such as the data redundancy problem I just mentioned:

  • Once the master node goes down, the slave Node is promoted to master node. At the same time, the master node address of the application side needs to be modified, and all slave nodes need to be commanded. Copy New master node, the whole process requires manual intervention.
  • The write capability of the master node is limited by a single machine.
  • The storage capacity of the master node is limited by the single machine.

Sentinel Mode

As mentioned just now, in the master-slave mode, when the master node goes down, the slave node can come up as the master node and continue to provide services. of.

But there is a problem. The IP of the master node has changed. At this time, the application service still uses the address of the originalmaster node to access. This...

Therefore, when Redis version 2.8 was introduced, the concept of sentinel came into being.

Based on replication, Sentinel implements automated failure recovery.

Quickly understand the stand-alone, master-slave, sentinel and cluster modes in Redis

As shown in the figure, the sentinel node consists of two parts, the sentinel node and the data node:

    Sentinel node: The sentinel system consists of one or more It is composed of sentinel nodes. Sentinel nodes are special redis nodes that do not store data.
  • Data node: The master node and the slave node are both data nodes.
All data accessed to the redis cluster is through the sentinel cluster, and the sentinel monitors the entire redis cluster.

Once it is discovered that there is a problem with the redis cluster, such as the master node just mentioned hangs up, the slave node will come up. However, if the master node address changes, the application service will not be aware of it at this time, and there is no need to change the access address, because the sentinel interacts with the application service.

Sentinel solves the problem of failover very well and takes it to a higher level in terms of high availability. Of course, Sentinel also has other functions.

For example,

Master node survival detection, Master-slave running status detection, Master-slave switching.

The minimum configuration of Sentinel for Redis is

one master and one slave.

Let’s talk about the principle of sentinel mode monitoring

Each Sentinel sends messages to its

allmain servers,# at a frequency of once per second ##Send a PING command from the server and other Sentinelinstances.

Quickly understand the stand-alone, master-slave, sentinel and cluster modes in RedisIf the time since the last valid reply to the PING command exceeds the value specified by down-after-milliseconds, then this instance will be marked # by Sentinel. ##Subjective offline

.

If a master server

is marked as

subjectively offline, then all Sentinel nodes that are monitoring this master server must be Once per second frequency to confirm whether the main server has indeed entered the subjective offline state. If a master server is marked as subjectively offline, and there are sufficient number

of Sentinels (at least the number specified in the configuration file) within the specified

time range If you agree with this judgment, then the main server is marked as Objective offline. Under normal circumstances, each Sentinel will send INFO commands to all master servers and slave servers it knows once every 10 seconds.

When a

master server

is marked by Sentinel as

objectively offline, the frequency of Sentinel sending INFO commands to all slave servers of the offline master server will change from 10 Once per second was changed to once per second. Sentinel negotiates with other Sentinels about the master node

status. If the master node is in the

SDOWN` state, the vote automatically selects the new master node. Point the remaining slave nodes to the new primary node for data replication. When there are not enough Sentinels to agree that the main server goes offline, the objective offline status

of the main server will be removed. When the

main server returns a valid reply to Sentinel's PING command, the main server's Subjective offline status will be removed. Advantages and Disadvantages of Sentinel Mode

Advantages:

Sentinel mode is based on master-slave mode, with all the advantages of master-slave, Sentinel All modes are available.

    Master and slave can be automatically switched, making the system more robust and highly available.
  • Sentinel will continuously check whether the master server and slave server are running normally. When a problem occurs on a monitored Redis server, Sentinel sends notifications to the administrator or other applications through API scripts.
  • shortcoming:
    • Redis较难支持在线扩容,对于集群,容量达到上限时在线扩容会变得很复杂。

    我的任务

    我部署的redis服务就如上图所示,三个哨兵节点,三个主从复制节点。

    使用java的jedis去访问我的redis服务,下面来一段简单的演示代码(并非工程里面的代码):

    public static void testSentinel() throws Exception {
         //mastername从配置中获取或者环境变量,这里为了演示
             String masterName = "master";
             Set<String> sentinels = new HashSet<>();
         // sentinel的IP一般会从配置文件获取或者环境变量,这里为了演示
             sentinels.add("192.168.200,213:26379");
             sentinels.add("192.168.200.214:26380");
             sentinels.add("192.168.200.215:26381");
     
         //初始化过程做了很多工作
             JedisSentinelPool pool = new JedisSentinelPool(masterName, sentinels); 
         //获取到redis的client
             Jedis jedis = pool.getResource();
         //写值到redis
             jedis.set("key1", "value1");
         //读取数据
         jedis.get("key1");
    }

    具体部署的配置文件这里太长了,需要的朋友可以公众号后台回复【redis配置】获取。

    听起来是入职第二天就部署了任务感觉很难的样子。

    其实现在看来是个so easy的任务,申请一个redis集群,自己配置下。在把工程里面使用到redis的地方改一下,之前使用的是一个两个单机节点。

    干完,收工。

    Quickly understand the stand-alone, master-slave, sentinel and cluster modes in Redis

    虽然领导的任务完成了,但并不意味着学习redis的路结束了。爱学习的龙叔,继续研究了下redis的集群模式。

    集群模式

    主从不能解决故障自动恢复问题,哨兵已经可以解决故障自动恢复了,那到底为啥还要集群模式呢?

    主从和哨兵都还有另外一些问题没有解决,单个节点的存储能力是有上限,访问能力是有上限的。

    Redis Cluster 集群模式具有 高可用可扩展性分布式容错 等特性。

    Cluster 集群模式的原理

    通过数据分片的方式来进行数据共享问题,同时提供数据复制和故障转移功能。

    之前的两种模式数据都是在一个节点上的,单个节点存储是存在上限的。集群模式就是把数据进行分片存储,当一个分片数据达到上限的时候,就分成多个分片。

    数据分片怎么分?

    集群的键空间被分割为16384个slots(即hash槽),通过hash的方式将数据分到不同的分片上的。

HASH_SLOT = CRC16(key) & 16384 复制代码

CRC16是一种循环校验算法,这里不是我们研究的重点,有兴趣可以看看。

这里用了位运算得到取模结果,位运算的速度高于取模运算。

Quickly understand the stand-alone, master-slave, sentinel and cluster modes in Redis

有一个很重要的问题,为什么是分割为16384个槽?这个问题可能会被面试官随口一问

数据分片之后怎么查,怎么写?
Quickly understand the stand-alone, master-slave, sentinel and cluster modes in Redis

读请求分配给slave节点,写请求分配给master,数据同步从master到slave节点。

读写分离提高并发能力,增加高性能。

如何做到水平扩展?

Quickly understand the stand-alone, master-slave, sentinel and cluster modes in Redis

master节点可以做扩充,数据迁移redis内部自动完成。

当你新增一个master节点,需要做数据迁移,redis服务不需要下线。

举个栗子:上面的有三个master节点,意味着redis的槽被分为三个段,假设三段分别是0~7000,7001~12000、12001~16383。

现在因为业务需要新增了一个master节点,四个节点共同占有16384个槽。

槽需要重新分配,数据也需要重新迁移,但是服务不需要下线。

redis集群的重新分片由redis内部的管理软件redis-trib负责执行。redis提供了进行重新分片的所有命令,redis-trib通过向节点发送命令来进行重新分片。

如何做故障转移?

Quickly understand the stand-alone, master-slave, sentinel and cluster modes in Redis

假如途中红色的节点故障了,此时master3下面的从节点会通过 选举 产生一个主节点。替换原来的故障节点。

此过程和哨兵模式的故障转移是一样的。

总结

每种模式都有各自的优缺点,在实际使用场景中要根据业务特点去选择合适的模式。

redis是一个非常常用的中间件,作为一个使用者来说,学习成本一点不高。

如果作为一个很好的中间件去研究的话,还是有很多值得学习和借鉴的地方。比如redis的各种数据结构(动态字符串、跳跃表、集合、字典等)、高效的内存分配(jemalloc)、高效的IO模型等等。

Each point can be studied in depth and incorporated into the later design of a high-concurrency and high-availability system.

For more programming related knowledge, please visit: Programming Video! !

The above is the detailed content of Quickly understand the stand-alone, master-slave, sentinel and cluster modes in Redis. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:juejin.cn. If there is any infringement, please contact admin@php.cn delete