Home>Article>Database> Detailed graphic explanation of redis high availability solution

Detailed graphic explanation of redis high availability solution

尚
forward
2019-12-17 16:42:01 3305browse

Detailed graphic explanation of redis high availability solution

Understand and build a redis cluster from scratch

Some developers only use redis in their applications, such as caching data results. . And now there are many good redis client tools (redisson), which can basically complete a considerable number of functions without paying attention to the redis command. Therefore, there may not be enough attention on the following issues:

How to recover from disaster? That is, if there is a problem with a certain redis node, how to ensure the high availability of the service

How to expand horizontally? When the amount of data is particularly large, how to solve the performance problem of a single redis

At least how many machines does the cluster need? Or what technologies and tools are used to build clusters of several redis nodes

?

How to recover from disaster?

Redis provides a master-slave hot standby mechanism. The data of the master server is synchronized to the slave server. The status of the master server is monitored in real time through sentinels and is responsible for electing the master server. When an abnormality in the main server is found, the main server is re-elected according to a certain algorithm and the problem server is removed from the available list, and the client is finally notified. The master-slave is a one-to-many tree structure, as shown below:

Detailed graphic explanation of redis high availability solution

Sentinel

Sentinel is the Chinese name of sentinel. A high-availability architecture tool produced by redis is itself an independent process and can monitor more than one redis cluster at the same time.

Sentinel Cluster

Based on high availability considerations, Sentinel itself also needs to support clustering. If there is only one Sentinel, there will be a single point of problem.

Sentinel Decision

The sentinel has a quantity configuration. When how many sentinels think that a certain master service is unavailable at the same time, a master-slave switch will be performed. For example, there are a total of 5 sentinels. , when the three sentinels believe that the service is unavailable, they decide to make a master-slave switch. This can avoid some mistaken switching and reduce switching costs, such as instantaneous network abnormalities.

How to expand horizontally?

Whether it is redis or some other database products, when the data capacity of a single node reaches a certain upper limit, the ability to provide services to the outside world will become weaker and weaker. Redis provides redis-trib.rb in higher versions to implement cluster functions, and you can also use the third-party tool twemproxy.

Decentralization, every node is equal

The redis cluster was not designed with centralization in mind, which can avoid problems such as a single point in the central node. Each node can grasp the status of the entire cluster, and any node connected to it can access all keys, just like single-node redis.

Cluster schematic diagram

I drew it according to my own understanding. If there is any misunderstanding, please point it out.

Detailed graphic explanation of redis high availability solution

The relationship between key and redis node

Introduced hasy soldt, which is understood as hash slot in Chinese. There are 16384 in total. The key we operate uses the modulo algorithm to confirm which slot the key falls on.

HASH_SLOT = CRC16(key) mod 16384

There is a certain relationship between the hash slot and the node, so we can assign the key to a specific redis node.

The detailed relationship can be studied further. For example, node A is responsible for hash slots numbered 0-5000, node B is responsible for 5001-1000

Build step by step

Start building a cluster with three masters and three slaves. The system is ubuntu and the cluster tool redis-trib.rb provided by redis is used.

Install the latest redis

Create the redis_cluster directory and create 6 directories from 7000 to 7005

Copy redis.conf in the redis directory to the 6 directories created above middle

分别修改redis.conf文件,对6个文件做类似的修改。

port 7000 //端口7000 bind 127.0.0.1 //默认ip为127.0.0.1 需要改为其他节点机器可访问的ip daemonize yes //后台运行 pidfile /var/run/redis_7000.pid //pidfile文件对应7000 cluster-enabled yes //开启集群 cluster-config-file nodes_7000.conf //集群的配置 cluster-node-timeout 15000 //请求超时 默认15秒,可自行设置

bind需要注意的就是需要配置为其它机器可以访问的ip,否则无论是创建集群还是客户端连接都会有问题。

启动6个redis

redis-server redis_cluster/7000/redis.conf redis-server redis_cluster/7001/redis.conf redis-server redis_cluster/7002/redis.conf redis-server redis_cluster/7003/redis.conf redis-server redis_cluster/7004/redis.conf redis-server redis_cluster/7005/redis.conf

创建集群
redis的src目录下有个redis-trib.rb,将它复制到/usr/local/bin中,然后执行如下脚本:

redis-trib.rb create --replicas 1 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005

--replicas后面的1代表从服务器的个数,上面可以理解为前面3个为主服务器,后面三个分别做为从服务器,即三对主从。

执行过程中会遇到提示需要安装ruby,安装完成之后又会提示安装 gem redis。

安装gem redis,折腾了好久,最终发现是因为在国内访问不了某些网站导致通过apt-get install安装不成功,最后通过下载源码的方式安装成功。

Detailed graphic explanation of redis high availability solution再次执行创建集群的脚本,出现如下提示:

Detailed graphic explanation of redis high availability solution

输入yes,继续

Detailed graphic explanation of redis high availability solution

最少需要多少个主服务器?

可能是基于某些约定,集群约定只有当可用节点数大于半数以上时才具备对外提供服务的能力。首先数量一定是奇数,其实必须大于1,所以最少的主服务器数量为3。

测试集群
连接客户端,由于我的所有节点都是在本地,所以不需要输入ip,但需要加-c的参数。redis-cli -c -p 7000

连接成功后,增加一个key

set mykey 123

有一行提示语,指向到端口7002,这说明虽然我们连接的是7000的实例,但通过hash算法最终会将key分配到7002的实例上。

Detailed graphic explanation of redis high availability solution

再连接7005端口查询下key,测试下是否任意一个实例都可以查询到key

get mykey

显示指向到端口7002

Detailed graphic explanation of redis high availability solution

更多redis知识请关注redis数据库教程栏目。

The above is the detailed content of Detailed graphic explanation of redis high availability solution. For more information, please follow other related articles on the PHP Chinese website!

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