Home  >  Article  >  Database  >  What is the implementation principle and process of Redis persistence mechanism?

What is the implementation principle and process of Redis persistence mechanism?

WBOY
WBOYforward
2023-06-02 21:43:50858browse

What is the implementation principle of Redis persistence mechanism?

Persistence: Redis is an in-memory database, and data is stored in memory. In order to avoid permanent loss of data due to process exit, the data in Redis needs to be regularly stored in some form (data or command ) is saved from the memory to the hard disk; when Redis restarts next time, the persistent file is used to achieve data recovery. In addition, for disaster backup, persistent files can be copied to a remote location

What is the persistence process?

Since redis data can be saved on disk, what is the process like?

The following five processes are required:

(1) The client sends a write operation to the server (the data is in the client's memory).

(2) The database server receives the data of the write request (the data is in the memory of the server).

(3) The server calls the write system call to write the data to the disk (the data is in the buffer of the system memory).

(4) The operating system transfers the data in the buffer to the disk controller (the data is in the disk cache).

(5) The disk controller writes the data to the physical media of the disk (the data actually falls on the disk).

These 5 processes are a normal saving process under ideal conditions, but in most cases, our machines, etc. will have various failures. Here are two situations:

If the Redis database fails, as long as the third step above is completed, it can be persisted and saved. The remaining two steps will be completed by the operating system for us; if the operating system fails, all the above 5 steps must be completed. Only then.

In order to cope with the above five steps, redis provides two different persistence methods: RDB (Redis DataBase) and AOF (Append Only File)

Due to the need for fork operation, RDB's Snapshots and AOF rewriting cause blocking to Redis, which is a very resource-consuming operation. Therefore, in order not to affect the response of the Redis main process, we need to reduce blocking as much as possible.

1. Reduce the frequency of fork, for example, you can manually trigger RDB to generate snapshots and AOF rewrite;

2. Control the maximum memory usage of Redis to prevent fork from taking too long;

3. Use more powerful hardware;

4. Properly configure the Linux memory allocation strategy to avoid fork failure due to insufficient physical memory

Online practical experience :

1. If the data in Redis is not particularly sensitive or the data can be rewritten in other ways, persistence can be turned off. If the data is lost, it can be recovered through other means;

2. Develop your own strategy to regularly check the Redis situation, and then manually trigger backup and rewrite data;

3. You can join the master-slave machine, use one slave machine for backup processing, and other machines will respond normally Client commands;

4.RDB persistence and AOF persistence can exist at the same time and be used together.

The above is the detailed content of What is the implementation principle and process of Redis persistence mechanism?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete
Previous article:How to use Redis streamsNext article:How to use Redis streams