repeat - RDB
What is RDB
After RDB, create the data will redis in memory to the hard disk of a snapshot, called RDB file (binary)
When redis restarts, loads RDB files on your hard disk, loaded into memory for data recovery.
RDB trigger modes - mainly three ways
- save (synchronous)
- bgsave (asynchronous)
- automatic
1. save (sync)
The implementation process is as follows:
- In the save while other commands will block waiting
- If the old RDB file exists, it will create a temporary file, and then replace the old files
- Time complexity, O (n)
redis> save
OK
2. bgsave (asynchronous)
The implementation process is as follows:
- After calling bgsave, linux will call the fork () function to create a child process
- If the old RDB file exists, it will create a temporary file, and then replace the old files
- Time complexity, O (n)
- Sub Process name: redis-rdb-bgsave
redis> bgsave
BAckground saving started
3. By configuring the operation of the automatic RDB
- Internal equivalent bgsave
- Redis default save configuration
Configuration | second | changes |
---|---|---|
save | 900 | 1 |
save | 300 | 10 |
save | 60 | 10000 |
Meet any of the above conditions will be created (bgsave) RDB file (binary).
Within 60 seconds for example, 10,000 data changes, the automatically generated file RDB.
Disadvantage of
poor control generated RDB file, if a large amount of written words RDB generated too often, frequently written to the hard, hard burden for a lot.
RDB configuration
Configuration Item | Defaults | meaning |
---|---|---|
dbfilename | dump.rdb | RDB snapshot file name |
to you | ./ | RDB directory where the snapshot files generated |
stop-writes-on-bgsave-error | yes | bgsave whether the error occurred when stop writing |
rdbcompression | yes | RDB file is a compressed |
rdbchecksum | yes | Verify whether RDB |
RDB best configuration
- Automatic operation is not disposed RDB
- dbfilename dump-${port}.rdb
- dir / redisDataPath
- stop-writes-on-bgsave-error yes
- rdbcompression yes
- rdbchecksum yes
The trigger mechanism can not be ignored
- From the master copy of the full amount of time to copy, master node performs bgsave
- debug reload
- shutdown
- flushDB 、 flushAll
Show
Here again write a redis-server startup configuration file named redis-6379.conf
, put /opt/soft/redis/config/
the directory, modified as follows, other configuration remains unchanged default.
daemonize yes
pidfile /var/run/redis-6379.pid
port 6379
logfile "6379.log"
# save 900 1
# save 300 10
# save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump-6379.rdb
dir /opt/soft/redis/data
Start redis service:
redis-server /opt/soft/redis/config/redis-6379.conf
Start the client:
redis-cli
carried out:
It can be seen in /opt/soft/redis/data/
the directory, generate the RDB file.
Now we show you automatically generate the RDB file, modify the redis-6379.conf files, configuration RDB automatic operation:
save 60 5
Redis then restart the service, start redis client, execute:
Printing tail of the log information, see occur five times vary within 60 s, it generates a file RDB.
to sum up
- RDB is redis memory to the hard disk snapshots for persistence.
- save often blocked redis.
- bgsave does not block redis, but will fork a new process.
- save automatic configuration satisfies any one will be executed.
- Some triggers can not be ignored.
Reproduced in: https: //www.jianshu.com/p/3a4ad6178cce