Introduction to Data persistence in Redis
It is currently used in two cases
1. Use as a database;
2. Use as a cache server.
The high performance of Redis is largely due to the fact that it stores data in memory, but the data is lost when the Redis restarts or when the machine is accidentally restarted. In order to keep the data in Redis from being lost, we need to store the data in memory in some way so that the data can be restored after the Redis server restarts, which is called persistence .
Redis supports two methods of data persistence, one is RDB, and the other is AOF mode.
The Rdb method writes the in-memory data to the hard disk according to the specified rules;
AOF the command itself is logged each time the command is executed.
Both methods can be used alone, usually in combination with each other. The following are two ways to introduce each.
The following configuration is done by modifying the redis.conf file under the Redis root . RDB mode
The RDB is done through snapshots (snapshot), and when rules are met, Redis generates a copy of the memory data and stores it on the hard disk, a process known as a "snapshot."
Redis performs snapshots in the following scenarios
1. Automatic snapshots based on configuration rules
2. User executes save or bgsave command
3. Execute Flushall command
4. When copying is performed.
The following 4 in the above methods are described separately
1.1 Automatic snapshots based on configuration rules
Users can customize snapshot conditions, which users can customize in the configuration file in the following format:
Save time change the number of keys
Save is the keyword; the time is the amount of time, the unit is the second, the number of modifier keys is that the number of changes in the specified time is greater than the number of changes that we specify, automatically execute the snapshot.
Such as:
Save 100 1
Save 60 10
Save 30 1000
Commands allow multiple, relational, or associative relationships to exist between them. The above command means, save once every 100 seconds, or, within 60 seconds, 10 or more key values change, or 30 seconds, 1000 key values change. When any of the above 3 conditions are met, Redis will automatically take a snapshot.
1.2 user executes save or bgsave command
When we need to restart the Redis service, migrate the Redis service, and back up the Redis service, we need to perform a manual snapshot, which can be done using the Save or Bgsave command.
Save: Synchronous execution, this will affect the user request, when the Redis data is relatively large, it is not recommended to use this command;
Bgsave: The background executes asynchronously without affecting the user's request for Redis. You can use the Lastsave command to view the last snapshot time.
1.3 Execute Flushall command
When you execute the flushall command, all data in Redis is cleared. A snapshot operation is triggered when any one of the custom snapshot conditions is, and no custom snapshot is triggered if no snapshot rules are customized.
1.4 When copying is performed.
When the Redis service is configured for master-slave replication, snapshots are executed even if no custom snapshot conditions are configured and no manual action is performed (execution save or bgsave).
Redis defaults to storing the snapshot files in the Dump.rdb file in the current directory, and you can configure the file path and file name between Dir and dbfilename two parameters, respectively.
Such as:
Dir C:\GYC
Dbfilename Dump.rdb
Note: By using RDB to persist Redis, any changes to the data after the last snapshot will be lost once Redis exits unexpectedly. If you cannot accept the missing data, it is recommended that you persist the data using AOF mode. two. AOF (append only file) mode
When using Redis to store non-temporal data, you need to turn on aof mode persistence. AOF appends each command that Redis executes to disk, which reduces the performance of Redis and is not enough to be acceptable. 2.1. Turn on AoF mode
By default, Redis does not have the AOF mode turned on, and can be enabled by the AppendOnly command, such as:
AppendOnly Yes
You can specify a storage file path, such as:
Dir c:\redis_aof
Appendfilename appendonly.aof
2.2. Sync to disk
Although each operation, Redis writes commands to the AoF file, but because of the operating system caching mechanism, it does not actually write all the data to disk, but into the hard disk cache. In general, the system will do every 30 seconds to write the data in the hard disk cache to disk.
You can do this with the following command:
# Appendfsync Always
# Appendfsync No
Appendfsync everysec
The first command is that each time a write is performed, synchronization is performed, the most secure and slowest;
The second command is to not actively perform synchronization, but by the system automatic execution, that is, 30 seconds to execute, not recommended, it is possible to lose 30 seconds of data;
The third command, executed once per second, is performed by Redis by default;
Redis supports both RDB and aof, and Redis uses AOF to recover data after the system restarts. This will minimize the loss of data.