In my last post, I wrote the basics of Redis, which is a little appetizer, but I’m going to expand on it. It’s really big! Ha ha ha…

Think about these dry goods, originally you should spend some silver other person ability to can tell you, but here can be white piao completely, be the kind of real white piao ha!

Oh, who called uncle so love you, ahem

We know Redis is based on memory single process single thread model working mode, single machine can easily reach 10W +QPS, because it is based on memory operation, so extremely fast, standard second male, hahaha…

A master-slave replication

But fast is not enough, because with the development of the business, a machine is not enough to support the development of the business, after all, no boss will want their products to be only single, to scale, standardization, a large number of.

We’re the ones who move bricks in big factories, of course, the ones who build tens of thousands of machines.

Therefore, this time we need to use Redis Cluster, which is the Redis Cluster mode, through the primary and secondary synchronization, read and write separation, a large number of machines to organize, so as to provide services for more users.

We can deploy one primary server and multiple secondary servers. The primary server only processes write requests, and the secondary server synchronizes the data of the primary server with the replication function and only processes read requests, so as to improve the service capability of Redis.

However, a large number of machines, just like the employees in the company, more difficult to manage, so, it needs a good coordination mechanism, to let them work together, so as to play a greater energy.

This collaboration mechanism is data synchronization, and the specific working process is as follows:

When you start a Slave, it sends a psync command to the Master, and the Master starts a thread that generates an RDB snapshot and caches the new write request in memory. When the RDB file is created, the Master sends the RDB file to the Slave. After the slave gets it, it first writes it to disk, and then loads it to memory. Then the master sends the commands cached in memory to the slave, thus completing the data synchronization.

But things happen all the time. What if the power goes out and the Internet is disconnected in the process of synchronizing data?

Don’t worry, Redis is very family-oriented, he will automatically reconnect and make up for any missing data. What a nice guy!

persistence

We know that Redis is an in-memory database. As a regular computer user, we know that the data in memory is lost when the machine is restarted, so persistence is important.

Just like a lot of girls say they don’t like cheating on women’s feelings of men, but cheating on women’s feelings of men bring excitement and freshness is still addictive, say no, but the body is very honest, ha ha ha…

But Redis is a standard good man, he will take your pot for you…

There are two main methods of Redis persistence, one is RDB, the other is AOF.

The RDB stores data before a certain point in time.

AOF saves every command executed by Redis;

Both methods persist data to disk, but like two men who cheat on a woman’s affections, there are comparisons.

RDB

advantages

If there is a failure, only the data from the last RDB execution time to the time when the failure occurred will be lost. This advantage is especially suitable for cold backup. Generally, operation and maintenance will set a scheduled task to synchronize data to other machines. For example, data in Hangzhou will be backed up to Shanghai, and data in Xi ‘an will be backed up to Shijiazhuang. If the machine in that place dies, and you want to recover data from any point in time, all you have to do is make a copy.

It also recovers quickly with minimal impact on performance.

disadvantages

If the amount of data is large and the QPS is high, then the time required to execute an RDB will increase accordingly.

AOF

advantages

It saves every command executed by the server in a file, so that in theory only one command can be lost in the event of a failure. That means its data is more secure.

disadvantages

If Redis has a large number of modification operations, the final state of a data in the RDB may require a large number of commands to reach, resulting in large AOF files and slow loading.

And the AOF file loading needs to create a pseudo client, and then send the command to the Redis server, server and then complete the corresponding command again, think about it is very annoying!

How to choose at this time?

How do you choose between the thrill of cheating on a womanly man and finding an honest man to live with? Then you have to choose a man like me who is both a slut and a family man. Hahaha…

In the same way, Redis is also such a mature man, it is recommended to use RDB and AOF mixed persistence scheme, in this way, when the AOF rewrite the child process will save the current point in time snapshot of the data in RDB file format, and then the parent process accumulated commands saved in AOF, so that it can be both fast complete.

The sentry

A sentry, as the name suggests, is a person who keeps watch, watches the enemy, and keeps everyone safe.

The Sentinel mechanism of Redis is to ensure the high availability of Redis. When the Redis Master fails, a Redis Slave can be automatically selected to switch to the Master and continue to provide external services.

It is equivalent to the unfortunate death of the big husband. We should not be leaderless. At this time, we should choose a more prestigious brother to be the eldest brother.

Here’s the big-name Sentinel deployment:

The figure shows a Redis Master with two slaves under the Master. The three sentries are connected to the Master and Slave at the same time, and the sentries are connected to each other.

By communicating with the Master and Slave, sentry knows the health status of each Redis service

In practice, at least three sentries will be deployed, and the number of sentries should be an odd number. This is because first of all, Redis Sentinel is to ensure the high availability of the system and avoid the dilemma of individual combat, so there are at least three, and the odd number is to ensure that two leaders will not be selected at the same time when the first leader is selected.

conclusion

OK, this article is really the essence, it is almost one of the necessary bricks to enter the big factory. At the end, I can’t bear to post it, ha ha ha…

However za is slag and mature uncle, of course is to let everyone white prostitute of!

If you think uncle write good, ask attention, ask for like, ask to share, after all uncle boil liver code word is also very hard, of course, if you are a girl, also welcome direct small window to find me oh!

See you next time!

I am Shi Yuan jun, a wandering uncle in the Internet river’s lake.