A brief introduction to Redis cluster

0X 00 Foreword

What is Redis? Redis is an excellent open source Key-value database. Redis stores data in memory, so the read and write speed is very fast, the read speed is 110000 times/s, and the write speed is 81000 times/s. And support Rich data types, including String, List, Hash, Set and Sorted Set.

Although the data is stored in the memory, it can still be flashed to the hard disk from time to time to complete the persistence.

0X 10 What is a cluster

As 集群everyone may have heard it, it sounds very tall. In fact, otherwise, if you understand it, you will find that this is a natural thinking of human beings.

So what exactly is a cluster?

The so-called cluster is to provide the same service by adding the number of servers, so that the servers can reach a stable and efficient state

Is it a very simple concept? At this time, some people have questions. Why is Redis performance so high? Why do clusters have to be distributed with several servers?

In fact, the use of clusters is first for efficiency and second for safety

Because of a server, it is difficult to guarantee that it will not be a problem. If there is a downtime and no persistence, then the data stored in Redis will be directly lost, which is very scary

0X 20 cluster implementation

To understand the cluster, we must have the following concepts:

  • In a Redis cluster, each redis (or server) is called a节点
  • In Redis cluster, there are two types of nodes: master node (master) and slave node (slave)
  • In the Redis cluster, it is implemented based on Redis master-slave replication

Redis master-slave replication

In this model, we will select a Redis as the Mastermaster node. What needs to be remembered is that 有且仅有一个Master. SlaveThere will be multiple slave nodes

As long as the network connection is normal, the Master will always synchronize its own data updates to Slaves and keep主从同步
Insert picture description here

Features

1. The master node Master can read and write

2. Slave node Slave is read-only

However, in practice, it is more inclined to design the Master to be write-only , and to distribute all the read data to the slave nodes, so that the problem of read and write conflicts can be avoided, and greater efficiency can be obtained.

0X 30 Sentinel mode

We just explained the specific content of the cluster, and I think everyone knows it.

But we will find that the above model has this serious problem, so there is only one Master. If the Master goes down, will the entire cluster fail to work? So we need to use another mechanism to solve this problem-哨兵

What is a sentinel?

The sentinel is used to watch, what watch is it to stare at? Keep an eye on the tips of the entire cluster. It has three tasks:

  • Monitoring : The sentry will constantly check whether the master server and slave server are operating normally
  • Reminder : When there is a problem with a certain Redis server that is not monitored, the sentry will send a notification to the administrator or other applications through the API
  • Automatic failover : When a primary server is not working properly, Sentinel will automatically start a failover operation, it will be 选举, the one from the server to a new primary server , and let the failure of the primary server instead of the other copy from the server New master server; when the client tries to connect to the failed master server, the cluster will also return the address of the new master server to the client, so that the cluster can use the new master server to replace the failed server

When the original Master is back online, it will become a new Salve and join the cluster

We also need to pay attention to that the so-called sentry is not only one, but used 哨兵网络, because when there is only one sentry, if the sentry hangs up, then automatic failover cannot be realized. So this is also a way to avoid this kind of problem

0X 40 Afterword

Now we probably understand the concept of clusters and sentries. Looking back, it is actually the same as we said at the beginning. It is a very natural idea, because in order to avoid a single server error and cause the entire system to be unavailable, so the data Distributed on multiple servers. This in itself is a disaster tolerance technology.

In order to improve efficiency, only the Master is used as the write server, because statistics show that the database read and write ratio is about 4:1 and more than part of the time is read, so the read operation is distributed to multiple slave servers. This greatly improves efficiency

The emergence of the sentinel mode is to avoid system paralysis caused by the collapse of this Master-centric system center.

These are all common sense ideas and designs, and we can see that other databases have similar patterns. For example, there is a master-slave model in MySql. In fact, the ideas and designs are similar.

Understanding the improper cluster is to understand such a model. In fact, we are more understanding of this way of thinking, so as to migrate to other fields.

Alright, that's it for today, I hope it helps everyone~

————————————————————————————————————————————

Reference: Introduction to redis cluster

Guess you like

Origin blog.csdn.net/rjszz1314/article/details/104924316