This section explains the persistence principles and configuration of Redis

Mondo Technology Updated on 2024-01-31

Redis is an in-memory database, so if the server fails or shuts down unexpectedly, it loses all of its stored data. To solve this problem, Redis provides two persistence methods: RDB (Redis database) and Aof (append-only file).

1.rdbrdb persistence is achieved by dumping the Redis dataset into a binary file on disk, which is done by forking a child process. The advantage of RDB persistence is that it is simple because it only needs to back up one file. All things being equal, RDB persistence requires much less space than AOF persistence because it only requires one file. In addition, using RDB persistence for backup and recovery is also faster than using AOF persistence.

The RDB configuration directive is as follows:

s**e 900 1 Perform a save operation if at least 1 key is modified in 900 secondss**e 300 10 Perform a save operation if at least 10 keys are modified in 300 secondss**e 60 10000 Perform a save operation if at least 10000 keys are modified in 60 seconds.

2.AOFAOF persistence is achieved by recording every write operation received by the server. The write operation is appended to the end of the file in Redis protocol format and re-executed when the server is started to restore the original data set. Since the AOF file is a text file, it can be easily repaired by compressing, editing, viewing, or manually.

The downside of AOF persistence is that it takes up more disk space. In addition, it can be slower than RDB persistence in some cases because each write operation requires an additional disk IO.

The AOF configuration directives are as follows:

appendonly yes enables AOF persistence, which defaults to no appendfilename"appendonly.aof"aof filename, which defaults"appendonly.aof"appendfsync everysec performs fsync every second

3.Recommended.

It is generally recommended to use a hybrid persistence of RDB and AOF so that all modifications are saved via AOF between savepoints and the recovery data is saved with RDB saved with the mind paused. An example of this configuration is as follows:

s**e 900 1s**e 300 10s**e 60 10000appendonly yesappendfilename "appendonly.aof"appendfsync everysec

In this configuration, RDB is executed at regular intervals (900 seconds, 300 seconds, and 60 seconds) and all modifications are persisted by AOF. aof files perform fsync every second.

Related Pages