Redis persistence mechanism

In Redis installation and common commands have been explained in detail Redis this Philippine relational database, Redis compared with other non-relational database one of the advantages, is Redis support data persistence mechanism. Redis is an in-memory database. All data in the Redis database is stored in memory in the form of cache. When the Redis server is restarted, or the computer is restarted, the data will be lost. To solve this problem, we can persist data in Redis memory to hard disk.

There are two Redis persistence mechanisms, namely RDB and AOF.

  • RDB: Takes a snapshot of the database if the number of changes in the database has reached the specified interval.
  • AOF: records every write operation to the server. When the server restarts, these commands are read to restore the original data of the database.

RDB

By default, no configuration is required. Redis uses this persistence mechanism by default. At a certain interval, it detects key changes and persists data. In the Redis directory, there is a redis.windows.conf file that contains the following configuration:

save 900 1		#15minutes1A key value has been changed to save300 10		#5minutes10A key value has been changed to save60 10000	#1minutes10000Three key values have been changedCopy the code

When the Redis database meets the requirements specified in the configuration, the Redis server automatically persists the database and generates a file named dump.rdb in the Redis directory. When the server is shut down, the next time the server is restarted, the dump. RDB is automatically read by the server and the original data of the database is restored.

The advantages of RDB

  • RDB is a very compact file that holds Redis data sets at a point in time.
  • Such files are great for backup and disaster recovery.
  • RDB maximizes the performance of Redis: the only thing the parent has to do to save the RDB file is fork out a child, which then handles all subsequent saves without the parent performing any disk I/O operations. RDB can recover large data sets faster than AOF.

The disadvantage of RDB

  • The time granularity of RDB operation is relatively large. If the server fails and the data of Redis is very large, a large amount of data may be lost even in 1 second.

AOF

The operation of each command can be recorded in the way of logging. After each command operation, data can be persisted (consuming performance). In the redis.windows.conf file, there is a appendOnly attribute that is set to no by default. If AOF persistence is required, change appendOnly to yes. You should also modify the appendfSync property to set the AOF persistence policy.

Appendonly no # disable AOF #appendonly yes # enable AOFCopy the code
Appendfsync always # persist every operation appendfsync everysec # persist every second # appendfsync no # do not persistCopy the code

If AOF persists successfully, an appendone.aof file is generated in the Redis directory. This file is similar to dump. RDB.

The advantages of AOF

  • The default AOF policy is fsync once per second. In this configuration, Redis still performs well and loses at most a second of data in the event of an outage (fsync is performed in background threads, so the main thread can continue to struggle to process command requests).

The disadvantage of AOF

  • AOF files are usually larger than RDB files for the same data set.

Enable persistence

Open the CMD command prompt window and go to the Redis directory.

cd E:\Program Files\Redis-x64-3.2100.
Copy the code

Start the Redis server and load the configuration file.

redis-server.exe redis.windows.conf
Copy the code