Home  >  Article  >  Database  >  How redis does data persistence

How redis does data persistence

(*-*)浩
(*-*)浩Original
2019-11-27 09:31:311996browse

Redis provides two methods of data persistence, namely RDB (Redis DataBase) and AOF (Apend Only File).

How redis does data persistence

RDB method

RDB method is a snapshot persistence method that stores data at a certain moment. Persisted to disk. (Recommended learning: Redis video tutorial)

During the process of data persistence, redis will first write the data to a temporary file, and when the persistence process is completed, This temporary file will be used to replace the last persisted file. It is this feature that allows us to perform backups at any time, because the snapshot file is always fully available.

For RDB mode, redis will create (fork) a child process separately for persistence, and the main process will not perform any IO operations, thus ensuring the extremely high performance of redis.

If large-scale data recovery is required and the integrity of data recovery is not very sensitive, the RDB method is more efficient than the AOF method.

AOF method

The AOF method records the executed write instructions, and then executes the instructions in order from front to back during data recovery. Again.

The AOF command uses the redis protocol to append and save each write operation to the end of the file. Redis can also rewrite AOF files in the background so that the size of the AOF files does not become too large.

The default AOF persistence strategy is fsync once per second (fsync refers to recording the write instructions in the cache to the disk), because in this case, redis can still maintain good processing performance , even if redis fails, only the data of the last 1 second will be lost.

If when appending logs, the disk space is full, the inode is full, or the power is outage, resulting in incomplete log writing, it does not matter. Redis provides the redis-check-aof tool, which can be used Perform log repair.

Because of the append method, if no processing is done, the AOF file will become larger and larger. For this reason, redis provides an AOF file rewrite (rewrite) mechanism, that is, when the size of the AOF file When the set threshold is exceeded, redis will start the content compression of the AOF file, retaining only the minimum instruction set that can recover the data.

An example may be more vivid. If we call the INCR instruction 100 times, 100 instructions will be stored in the AOF file, but this is obviously very inefficient. These 100 instructions can be merged. Into a SET instruction, this is the principle of the rewriting mechanism.

When rewriting AOF, the process of writing temporary files first and then replacing them is still used, so problems such as power outages and full disks will not affect the availability of AOF files.

For more Redis-related technical articles, please visit the Redis Getting Started Tutorial column to learn!

The above is the detailed content of How redis does data persistence. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn