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.
Less code, more hair
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 tooyoung.
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.
#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 amnot afraid. After all, if you do the same work thathas doneevery 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.
Upgrade all redis services in our department toSentinelmode. [Related recommendations:Redis video tutorial]
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
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 theadvantages and disadvantagesof a single machine.
Advantages:
Disadvantages:
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 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.
# 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# 例如 # slaveof 192.168.1.214 6379
Start all services ofmaster-slavenodes, and check the log to see the service connection betweenmaster-slavenodes.
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 ultimateuser experience,downtimeis 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 beelectedto ensure service. high availability.
Advantages of the master-slave mode:
Once the master node goes down, theslave nodecan be used as thebackupof the master node. Come up anytime.
Expand thereading capabilityof theprimary nodeto 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:
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 theoriginalmaster node to access. This...
Therefore, when Redis version 2.8 was introduced, the concept of sentinel came into being.
Based onreplication, Sentinel implementsautomatedfailure recovery.
As shown in the figure, the sentinel node consists of two parts, the sentinel node and the data node:Master node survival detection,Master-slave running status detection,Master-slave switching.
The minimum configuration of Sentinel for Redis isone master and one slave.
Let’s talk about the principle of sentinel mode monitoring Each Sentinel sends messages to itsallmain servers,# at a frequency of once per second ##Send a PING command from the serverand other Sentinelinstances.
If 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 amaster server
is marked assubjectively offline, thenallSentinel nodes that are monitoring this master server must beOnce per secondfrequency to confirm whether the main server has indeed entered thesubjective offlinestate.If a master server is marked as subjectively offline, and there aresufficient number
of Sentinels (at least the number specified in the configuration file) within the specifiedtime rangeIf you agree with this judgment, then the main server is marked asObjective offline.Under normal circumstances, each Sentinel will send INFO commands to all master servers and slave servers it knows once every 10 seconds.
When amaster server
is marked by Sentinel asobjectively 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 themaster node
status. If the master node is in theSDOWN`state, the voteautomatically selects thenew master node. Point the remainingslave nodesto thenew primary nodefordata replication.When there are not enough Sentinels to agree that the main server goes offline, theobjective offline status
of the main server will be removed. When themain serverreturns a valid reply to Sentinel's PING command, the main server'sSubjective offline statuswill be removed.Advantages and Disadvantages of Sentinel Mode
Sentinel mode is based on master-slave mode, with all the advantages of master-slave, Sentinel All modes are available.
我部署的redis服务就如上图所示,三个哨兵节点,三个主从复制节点。
使用java的jedis去访问我的redis服务,下面来一段简单的演示代码(并非工程里面的代码):
public static void testSentinel() throws Exception { //mastername从配置中获取或者环境变量,这里为了演示 String masterName = "master"; Setsentinels = 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的地方改一下,之前使用的是一个两个单机节点。
干完,收工。
虽然领导的任务完成了,但并不意味着学习redis的路结束了。爱学习的龙叔,继续研究了下redis的集群模式。
主从不能解决故障自动恢复问题,哨兵已经可以解决故障自动恢复了,那到底为啥还要集群模式呢?
主从和哨兵都还有另外一些问题没有解决,单个节点的存储能力是有上限,访问能力是有上限的。
Redis Cluster 集群模式具有高可用、可扩展性、分布式、容错等特性。
通过数据分片的方式来进行数据共享问题,同时提供数据复制和故障转移功能。
之前的两种模式数据都是在一个节点上的,单个节点存储是存在上限的。集群模式就是把数据进行分片存储,当一个分片数据达到上限的时候,就分成多个分片。
集群的键空间被分割为16384个slots(即hash槽),通过hash的方式将数据分到不同的分片上的。
HASH_SLOT = CRC16(key) & 16384 复制代码
CRC16是一种循环校验算法,这里不是我们研究的重点,有兴趣可以看看。
这里用了位运算得到取模结果,位运算的速度高于取模运算。
有一个很重要的问题,为什么是分割为16384个槽?这个问题可能会被面试官随口一问
读请求分配给slave节点,写请求分配给master,数据同步从master到slave节点。
读写分离提高并发能力,增加高性能。
master节点可以做扩充,数据迁移redis内部自动完成。
当你新增一个master节点,需要做数据迁移,redis服务不需要下线。
举个栗子:上面的有三个master节点,意味着redis的槽被分为三个段,假设三段分别是0~7000,7001~12000、12001~16383。
现在因为业务需要新增了一个master节点,四个节点共同占有16384个槽。
槽需要重新分配,数据也需要重新迁移,但是服务不需要下线。
redis集群的重新分片由redis内部的管理软件redis-trib负责执行。redis提供了进行重新分片的所有命令,redis-trib通过向节点发送命令来进行重新分片。
假如途中红色的节点故障了,此时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!