brief introduction
Redis provides two persistence methods: RDB (Redis DataBase) and AOF (Append Only File).
- RDB, in short, is to generate snapshots of data stored in redis at different time points and store them on disks and other media;
- AOF implements persistence from a different perspective, that is, record all the write instructions executed by redis. When redis restarts next time, just repeat these write instructions from front to back to realize data recovery.
In fact, RDB and AOF can also be used at the same time. In this case, if redis is restarted, AOF will be preferred for data recovery because AOF has higher data recovery integrity.
If you don't need data persistence, you can turn off RDB and AOF. In this way, redis will become a pure memory database, just like memcache.
advantage
- rdb files are small and suitable for backup and transmission
- Performance will be better than aof (aof needs to write log to file)
- rdb recovery is faster than aof
shortcoming
- When the server fails, the data after the last backup will be lost
- When Redis saves an rdb, the Redis service will stop responding (usually in milliseconds) during this operation of the fork sub process. However, if the amount of data is large and the cpu time is tight, the time to stop responding may be as long as 1 second
redis persistent RDB
RDB mode is to persist the data of redis to disk at a certain time. It is a snapshot persistence method.
In the process of data persistence, redis will first write the data to a temporary file. After the persistence process is completed, redis will replace the last persistent file with this temporary file. It is this feature that allows us to back up at any time, because the snapshot file is always completely available.
RBD persistence is the process of saving the snapshot generated by the current data to the hard disk. The process of triggering RBD persistence is divided into manual trigger and automatic trigger
- Manual trigger: bgsave
- Auto trigger: (when the conditions are not met, if shutdown is executed, bgsave will be executed first and then shutdown will be executed)
Manual trigger: bgsave
The bgsave command generates snapshots asynchronously. Redis will fork out a sub process to generate RDB files.
Redis is blocked only when the child process is fork ed. While the child process completes the snapshot generation, redis can work normally.
[root@redis1 ~]# redis-cli -h 10.0.0.31 bgsave Background saving started #An rbd file will be automatically generated. This path is the directory of the previously configured local database, which can be configured according to requirements [root@redis1 ~]# ll /data/redis_cluster/redis_6379/redis_6379.rdb -rw-r--r-- 1 root root 148025 10 November 14:04 /data/redis_cluster/redis_6379/redis_6379.rdb
Automatic trigger
The most common way to use RDB for persistence is to configure Redis in the configuration file to save snapshots:
save [seconds] [changes]
Redis starts RDB snapshot by default. The default RDB policy is as follows:
save 900 1
save 300 10
save 60 10000
Configure
[root@redis1 conf]# vim /opt/redis_cluster/redis_6379/conf/redis_6379.conf ###Start in daemon mode daemonize yes ###Bound host address bind 10.0.0.31 ###Monitoring port port 6379 ###Save address of pid file and log file pidfile /opt/redis_cluster/redis_6379/pid/redis_6379.pid logfile /opt/redis_cluster/redis_6379/logs/redis_6379.log ###Set the number of databases. The default database is 0 databases 16 ###Specify the file name of the local persistent file. The default is dump.rdb dbfilename redis_6379.rdb ###bgsave trigger condition save 900 1 save 300 10 save 60 10000 ###Directory of local database dir /data/redis_cluster/redis_6379 #Restart and start the service [root@redis1 conf]# redis-cli -h 10.0.0.31 shutdown [root@redis1 conf]# redis-server /opt/redis_cluster/redis_6379/conf/redis_6379.conf #Write data and write 10000 pieces of data [root@redis1 conf]# ll /data/redis_cluster/redis_6379/ Total dosage 148 -rw-r--r-- 1 root root 148025 10 November 14:13 redis_6379.rdb [root@redis1 conf]# sh ~/for.sh #A new rbd file is automatically generated. If the conditions are met, bgsave is automatically triggered [root@redis1 conf]# ll /data/redis_cluster/redis_6379/ Total dosage 148 -rw-r--r-- 1 root root 148025 10 November 14:16 redis_6379.rdb
redis persistent AOF
Aof persistence records every write command in the form of an independent log, and then executes the command in the AOF file when restarting. The main function of AOF is to solve the real-time of data persistence, which has become the mainstream way of Redis persistence
Modify configuration
[root@redis1 conf]# vim /opt/redis_cluster/redis_6379/conf/redis_6379.conf ###Start in daemon mode daemonize yes ###Bound host address bind 10.0.0.31 ###Monitoring port port 6379 ###Save address of pid file and log file pidfile /opt/redis_cluster/redis_6379/pid/redis_6379.pid logfile /opt/redis_cluster/redis_6379/logs/redis_6379.log ###Set the number of databases. The default database is 0 databases 16 ###Specify the file name of the local persistent file. The default is dump.rdb dbfilename redis_6379.rdb ###bgsave trigger condition save 900 1 save 300 10 save 60 10000 ###Directory of local database dir /data/redis_cluster/redis_6379 #Turn on the AOF persistent log function appendonly yes #Every command is synchronized to aof immediately appendfsync always ##Write once per second appendfsync everysec #The writing work is handed over to the operating system, which determines the size of the buffer and writes to aof uniformly appendfsync no appendfilename "appendonly.aof" #Restart [root@redis1 conf]# redis-cli -h 10.0.0.31 shutdown [root@redis1 conf]# redis-server redis_6379.conf [root@redis1 conf]# ll /data/redis_cluster/redis_6379/ Total consumption 596 -rw-r--r-- 1 root root 367811 10 November 14:29 appendonly.aof -rw-r--r-- 1 root root 147886 10 November 14:29 redis_6379.rdb [root@redis1 conf]# ll /data/redis_cluster/redis_6379/ Total consumption 596 -rw-r--r-- 1 root root 367811 10 November 14:29 appendonly.aof -rw-r--r-- 1 root root 147886 10 November 14:29 redis_6379.rdb #Any modification will be written to the aof file [root@redis1 conf]# redis-cli -h 10.0.0.31 set key8 v8 OK [root@redis1 conf]# ll /data/redis_cluster/redis_6379/ Total consumption 508 -rw-r--r-- 1 root root 367842 10 November 14:30 appendonly.aof -rw-r--r-- 1 root root 147886 10 November 14:29 redis_6379.rdb