redis learning (17) - RDB

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.

9197037-a79471764e8f013f.jpg

RDB trigger modes - mainly three ways

  • save (synchronous)
  • bgsave (asynchronous)
  • automatic
1. save (sync)

The implementation process is as follows:

9197037-26fed3bd7c95684c.jpg
  • 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:

9197037-cfaf3632e8f589c7.jpg
  • 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:

9197037-f69004956fc6f5b1.png

It can be seen in /opt/soft/redis/data/the directory, generate the RDB file.

9197037-1d1152c9cec7d9af.png

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:

9197037-86aff8312c8be95a.png

Printing tail of the log information, see occur five times vary within 60 s, it generates a file RDB.

9197037-01de1990b9646340.png

to sum up

  1. RDB is redis memory to the hard disk snapshots for persistence.
  2. save often blocked redis.
  3. bgsave does not block redis, but will fork a new process.
  4. save automatic configuration satisfies any one will be executed.
  5. Some triggers can not be ignored.

Reproduced in: https: //www.jianshu.com/p/3a4ad6178cce

Guess you like

Origin blog.csdn.net/weixin_33675507/article/details/91095494
Recommended