Redis persistent RDB and AOF

This article is reproduced from: http://www.ymq.io/2018/03/24/redis/

Redis is an advanced key-value database. Data can be persisted, and the supported data types are very rich. There are strings, linked lists, sets and sorted sets. It supports computing the union, intersection and complement of sets on the server side, and also supports various sorting functions. So Redis can also be regarded as a data structure server.

In order to ensure efficiency, Redis caches data in memory . Redis will periodically write updated data to disk or write modification operations to additional record files to ensure data persistence .

Redis is an in-memory database that supports persistence, which can synchronize data in memory to disk to ensure persistence.

Redis persistence strategy : 2 types

  • RDB : The snapshot form is to directly save the data in the memory to a dump file, save it regularly, and save the strategy.
  • AOF : Save all commands to modify the Redis server in a file, a collection of commands.

Redis defaults to the persistence method of snapshot RDB

When Redis restarts, it prefers AOF files to restore datasets, because AOF files usually hold more complete datasets than RDB files. You can even turn off persistence so that data is only stored while the server is running.

1. RDB endurance

By default Redis will persist data to disk in the form of snapshot "RDB", a binary file, dump.rdb

The working principle is briefly introduced :

When Redis needs to do persistence, Redis will fork a child process, and the child process will write data to a temporary RDB file on disk. When the child process finishes writing the temporary file, it replaces the original RDB, which has the advantage of copy-on-write.

By default, Redis is the persistence method of snapshot RDB, and the data in memory is written to the binary file in the form of snapshot. The default file name is dump.rdb. Of course, we can also manually execute save or bgsave (asynchronously) to make snapshots.

Redis.conf configuration  : The default is the following configuration

save 900 1 
save 300 10
save 60 10000
  • Within 900 seconds, if more than 1 key is modified, a snapshot save will be initiated;
  • Within 300 seconds, if more than 10 keys are modified, a snapshot save will be initiated;
  • Within 1 minute, if 10,000 keys are modified, a snapshot save will be initiated;

1.1 Advantages of RDB:

This kind of file is great for backups: for example, you can back up an RDB file every hour for the last 24 hours, and an RDB file every day of the month. This way, you can always restore your dataset to a different version, even if you run into problems. RDB is very suitable for disaster recovery (disaster recovery) .

1.2 Disadvantages of RDB:

If you need to try to avoid losing data in the event of a server failure , then RDB is not for you . Although Redis allows you to set various save points to control how often the RDB file is saved, it is not an easy operation because the RDB file needs to save the state of the entire dataset. So you may save RDB files at least once every 5 minutes. In this case, in the  event of an outage, you could lose several minutes of data .

2. AOF persistence

Use AOF for persistence, each write command is appended to appendonly.aof through the write function, configuration method: the way to start AOF persistence

Redis.conf configuration

appendfsync yes   
appendfsync always     #每次有数据修改发生时都会写入AOF文件。
appendfsync everysec   #每秒钟同步一次,该策略为AOF的缺省策略。

AOF can be persistent throughout the entire process. It only needs to be turned on in the configuration file (the default is no). After appendonly yes is turned on, Redis will add it to the AOF file every time Redis executes a command to modify data. When Redis restarts , the AOF file will be read for "replay" to restore to the last moment before Redis was shut down.

2.1 Advantages of AOF

Using AOF persistence makes Redis much more durable: you can set different fsync strategies , such as no fsync, fsync every second, or fsync every time a write command is executed. The default policy of AOF is fsync once per second . Under this configuration, Redis can still maintain good performance, and even if there is a failure, only one second of data will be lost ( fsync will be executed in the background thread, so The main thread can continue to work hard on the command request).

2.2 Disadvantages of AOF

For the same dataset, the size of AOF files is usually larger than that of RDB files. Depending on the fsync strategy used, AOF may be slower than RDBIn general, the performance of fsync per second is still very high , and turning off fsync can make AOF as fast as RDB, even under heavy load. However, when dealing with huge write loads, RDB can provide a more guaranteed maximum latency (latency).

Third, the difference between the two

RDB persistence refers to writing a snapshot of the data set in memory to disk within a specified time interval. The actual operation process is to fork a sub-process, first write the data set to a temporary file, and then replace the previous file after successful writing. , stored with binary compression.

AOF persistence records every write and delete operation processed by the server in the form of a log. The query operation is not recorded, but is recorded in the form of text. You can open the file to see the detailed operation record.

3.1 RDB and AOF, which one should I use?

  • If you care a lot about your data, but can still tolerate data loss for minutes, then you can just use RDB persistence.
  • AOF appends every command that Redis executes to disk, and processing huge writes will degrade Redis performance, I don't know if you can accept it.

Database backup and disaster recovery : Regularly generating RDB snapshots (snapshot) is very convenient for database backup, and the speed of RDB recovery of data sets is faster than that of AOF recovery.

Redis supports opening RDB and AOF at the same time. After the system restarts, Redis will give priority to using AOF to restore data, so that the lost data will be the least.

In order to facilitate everyone to communicate, I have opened a WeChat public account and a QQ group, QQ group: 291519319, let’s communicate with those who like technology

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325211659&siteId=291194637