This is the 24th day of my participation in Gwen Challenge

Redis persists in three ways mentioned in the previous article:

  • AOF log: Records all operation commands and appends them to a file as text

  • RDB snapshot: The RDB snapshot is used to write memory data at a certain time point to the disk in binary mode

  • Mixed persistence: Mixed persistence has been added to Redis4.0 to integrate the benefits of RDB and AOF

Why persistence?

The redis cache is stored in memory. If the server is shut down or the Redis server daemon exits, the data in memory will be lost.

Application scenario: When Redis is used as a database to store important service data, data persistence is required to prevent adverse impact. If you use it only as a cache, you don’t need to worry about persistence.

AOF

AOF: Append Only File

AOF log implementation

In the AOF persistence mode, the client records each write operation command to the server and saves these write operations to a file with the suffix AOF using Redis protocol. When the Redis server restarts, the commands of the AOF file are loaded and run to recover data.

Note content:

MySQL databases use “log before write”. That is, the modified data is recorded in a log file before data is written. For example, mysql redo log. This is the opposite of Redis. Redis executes commands, writes data to memory, and writes logs to a file

Why does Redis execute commands first and then write data to the log?

  • Redis does not need to syntax check commands before logging
  • Redis logs only successful commands
  • Execute the file before recording to avoid blocking the current write operation

The bad consequences of doing this are:

  • Data may be lost
  • Other operations may be blocked

Enable AOF persistence

Redis does not enable AOF persistence by default. We modify the Redis

#Enable AOF mechanism
appendonly yes

#AOF the file nameAppendfilename "appendonly. Aof"
#Write policy
appendfsync always

#Aof files are not overwritten by default
no-appendfsync-on-rewrite no

## save directory
dir ~/redis/
Copy the code

There are three ways to write policies

appendfsync always
#appendfsync everysec
#appendfsync no
Copy the code
  • Always: Each write operation on the client is saved to an AOF file. This policy is secure. However, each write request has I/O operations, so it is slow
  • Everysec: The default write policy of appendfsync, which writes AOF files once per second
  • No: The operating system is responsible for writing AOF files. The Redis server is not responsible for writing AOF files. Therefore, this mode is not secure and is not recommended

Advantages and disadvantages of AOF:

AOF advantages:

  • AOF only appends log files and has little impact on server performance. AOF is faster than RDB and consumes less memory

AOF faults:

  • The log file generated by AOF is too large. Procedure
  • Data recovery is slower than RDB