To be able to reuse Redis data, or to prevent system failures, we need to write the data in Redis to disk space, known as persistence. Redis provides two different persistence methods for storing data on disk, one called snapshot RDB and the other called appending file only AOF

RDB

What is the

Writes a Snapshot of an in-memory data set to disk (Snapshot) at a specified time interval. The Snapshot file is read directly into memory when it recovers.

Redis forks a separate subprocess for persistence, writing data to a temporary file that will be used to replace the last persistent file after the persistence process is complete. During the whole process, the main process does not perform any I/O operations, which ensures high performance. If large-scale data recovery is required and data integrity is not sensitive, the RDB mode is more efficient than the AOF mode. The downside of RDB is that data can be lost after the last persistence.

Fork

Fork copies the same process as the current one. All data values of the new process (variables, environment variables, program counters, etc.) are the same as those of the original process, but are a new process and a child of the original process. Rdb stores the dump. Rdb file.

Configure the location

The configuration of Redis is mainly placed in Redis. Conf. You can modify the configuration file to implement many features of Redis, such as replication, persistence, clustering, etc.

How do I trigger an RDB snapshot

  • Default snapshot configuration in the configuration file
  • The commandsaveOr is itbgsave
Save: Save only, other ignore, all block. BGSAVE: Redis takes snapshots asynchronously in the background and responds to client requests. You can run the lastsave command to obtain the time when the last snapshot was successfully executed.Copy the code
  • performflushallCommands are also generateddump.rdbfile

How to restore

Move the backup file dump. RDB to the redis installation directory and start the service

advantage

Suitable for large-scale data recovery with low requirements on data integrity and consistency

disadvantage

Backups are made at regular intervals, so if Redis unexpectedly goes down, all changes since the last snapshot are lost.

How to stop

Redis -cli config set save “”

AOF(Append Only File)

What is the

Every write operation is recorded in the form of a log. All write instructions performed by Redis are recorded (read operations are not recorded). Only files can be appended but files cannot be overwritten. When redis is restarted, write instructions are executed from front to back according to the contents of the log file to complete data recovery. AOF holds the appendone.aof file

AOF startup/repair/recovery

  • Normal recovery:
    • Boot: Settingsyes; Modify the defaultappendonly noInstead,yes
    • There will be dataaofMake a copy of the file and save it to the corresponding directory (config get dir)
    • Recovery: RestartredisAnd then reload
  • Abnormal recovery:
    • Boot: Settingsyes; Modify the defaultappendonly noInstead,yes
    • The backup was written badAOFfile
    • Fixed:Redis-check-aof --fixTo repair
    • Recovery: RestartredisAnd then reload

Rewrite

  • What is:
When the size of AOF files exceeds the set threshold, Redis will start the content compression of AOF files and only retain the minimum instruction set that can recover data. You can use the command bgrewriteaofCopy the code
  • Rewriting principle:
If the AOF file grows too large, a new process is forked to rewrite the file (also write temporary files first before rename), traversing the memory of the new process, one for each recordsetStatements. The operation of overwriting an AOF file, rather than reading the old AOF file, commands the entire contents of the database in memory to rewrite a new AOF file, similar to a snapshot.Copy the code
  • Trigger mechanism:
Redis records the AOF size of the last rewrite, triggered by default when the AOF file size is double the size since rewrite and the file size is greater than 64M.Copy the code

advantage

  • Sync each change:appendfsync alwaysSynchronous persistence, in which every data change is immediately recorded to disk, provides poor performance but better data integrity
  • Synchronization per second:appendfsync everysecAsynchronous operations are recorded every second. If the machine goes down within one second, data is lost
  • Out of sync:appendfsync noNever synchronization

disadvantage

  • For the same data setaofThe file is much bigger thanrdbFile recovery is slower thanrdb
  • aofOperating efficiency is slower thanrdb, the synchronization efficiency per second is better, the asynchronous efficiency andrdbThe same

Conclusion (Which one)

RDB persistence allows you to take snapshots of your data at specified intervals.

The AOF persistence mode records each write operation to the server. When the server is restarted, these commands will be executed again to restore the original data. The AOF command saves each write operation to the end of the file using the Redis protocol. Redis can also rewrite AOF files in the background so that AOF files are not too large.

Cache only: If you only want your data to live while the server is running, you can also do without any persistence.

Enable both persistence methods

In this case, when Redis restarts, AOF files will be loaded first to recover the original data, because in general AOF files hold more complete data sets than RDB files. RDB data is not real-time, and server restart will only look for AOF files when both are used. Should we just use AOF? The authors advise against it, as RDB is better for backing up databases (AOF is not easy to back up constantly), restarts quickly, and there are no potential AOF bugs left as a back-up measure.

Performance Suggestions

Since RDB files are only used for backup purposes, it is recommended to persist RDB files only on the Slave and backup them only once every 15 minutes, leaving only the save 9001 rule.

If you Enalbe AOF, the benefit is that you lose less than two seconds of data in the worst case. The startup script is simpler and you just load your own AOF files. The cost is constant IO, and the end result of AOFrewrite is almost inevitable blocking by writing new data to new files. The frequency of AOFrewrite should be minimized as long as hard drives are permitted; the default base size of AOFrewrite, 64M, is too small and can be set to more than 5G. The default size exceeds 100% of the original size and overrides can be changed to an appropriate value.

If AOF is not enabled, only master-slave Replication can be used to implement high availability. It saves a lot of IO and reduces the system volatility that comes with rewriting. The price is that if the Master/Slave is dropped at the same time, the data will be lost for more than ten minutes. The startup script also compares the RDB files in the two Master/Slave files and loads the newer one, which is the architecture chosen by Sina Weibo.