If you think the article is useful or well written, please click “like” on the left.

This article has been included in my personal blog: Geekvic. Top, welcome to chat!

What is persistence?

The main application of persistence is to store objects in memory to a database, or to disk files, XML data files, etc.

You can also think of persistence this way:

Application layer: If you close your application and then restart it, the previous data still exists.

System layer: If you shut down your system (computer) and then restart it, the previous data still exists.

Why persist?

Redis data is cached in memory. When you restart the system or shut down the system, the cached data will be discarded. Therefore, in order to keep the data for a long time, the data in Redis will be stored in persistent storage.

What kinds of persistence are there?

Redis provides two ways to persist:

RDB

Data is dumped from the memory every N minutes or N times to form RDB files, compressed, and stored in a backup directory, mainly as snapshots.

AOF

Recording each command operation on Redis is similar to recording an operation log. When the server restarts, these commands will be executed again to restore the original data.

Persistent configuration

RDB persistent configuration

# Time strategy
save 900 1 If there is at least one change in 900 seconds, dump
save 300 10 If there are at least 10 changes in 300 seconds, dump
save 60 10000 If there are at least 10000 changes in 60 seconds, dump

stop-writes-on-bgsave-error yes Whether the main program stops writing if persistence fails

rdbcompression yes Whether compression is supported

rdbchecksum yes Check whether RDB files are stored and loaded

dbfilename dump.rdb Set the RDB file name

dir /usr/local/data # RDB file save path
Copy the code

If you want to disable RDB configuration, on the last line of save write: save “”

AOF persistent configuration

appendonly yes # Whether to enable aOF

appendfilename "appendonly.aof" # file name

appendfsync everysec # Sync mode

no-appendfsync-on-rewrite no # aOF is out of sync during rewrite

# override trigger configuration
auto-aof-rewrite-percentage 100 # aOF file size is overwritten when the size growth rate is 100% compared to the last time it was overwritten
auto-aof-rewrite-min-size 64mb #aof file is rewritten at least 64M

aof-load-truncated yes If there is an error when loading aOF

aof-use-rdb-preamble yes # File rewrite policy
Copy the code

Appendfsync has three modes:

Always: Each command is synchronized to aOF, which is safe and slow.

No: The buffer size is determined by the OPERATING system and is written to the AOF in a unified manner. The synchronization frequency is low and the speed is fast.

Everysec: Compromise, write once per second, lose one second at most.

Aof-load-truncated yes If the configuration is enabled, if the aOF tail is incorrect during loading, a log is written to the client, but the operation continues. If the value is set to no, the client stops when errors are detected and must be repaired before reloading.

How persistence works

How RDB works

RDB persistence triggers are divided into two types: manual triggering and Redis timed triggering.

Manual trigger

  • Save: blocks the current Redis server until persistence is complete and should be disabled online.
  • Bgsave: This trigger forks a child process, and since the process is responsible for persistence, blocking only happens when the child process is forked

Automatic trigger

  • Automatically triggered according to the Save M N configuration rule
  • When the secondary node performs full replication, the primary node sends the RDB file to the secondary node to complete the replication, and the primary node triggers the BGSave
  • Debug reload;
  • If aOF is not enabled when shutdown is executed, it is also triggered

How AOF works

There are two main steps:

  1. Real-time writing of the command loses 1s if the appendfsync everysec is configured
  2. Rewrite the AOF file

Aof overrides are used to reduce the size of AOF files and can be triggered manually or automatically.

  • Manual trigger: bgrewriteof
  • Automatic trigger: Triggered according to the configuration rules, of course, the overall time of automatic trigger is also related to the Redis scheduled task frequency

Persisting recovery data

If there are both RDB files and AOF files on a server, which one should be loaded?

If you want to restore the data, just restart Redis. Check if the AOF file exists at startup, and try to load the RDB if it doesn’t. The reason for loading AOF first is that AOF stores more complete data, and we know that AOF basically loses at most 1s of data. As shown in the figure below.

Performance Optimization Suggestions

  1. If the Redis data is not important or can be rewritten in some other way, persistence can be turned off, and if lost data can be replaced in other ways
  2. Custom policies periodically check Redis and can then manually trigger backups and rewrites of data
  3. If multiple instances are deployed on a single machine, you need to prevent multiple instances from running persistent and rewrite operations at the same time to prevent memory, CPU, and I/O resource competition and make persistence become serial
  4. You can configure a master/slave replication. One slave server is used for backup, and other machines normally respond to client commands
  5. RDB persistence and AOF persistence can be used together.

Left hand typing, right hand shooting, a basketball loving coder ~~~

Sync personal blog: Geekvic. top/post/758625…

Disclaimations: All articles on this blog are licensed under by-NC-SA unless otherwise stated. Reprint please indicate the source!