码迷,mamicode.com
首页 > 其他好文 > 详细

Redis的持久化

时间:2018-07-19 13:42:02      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:concat   http   blocking   back   idle   psu   saving   同步   ica   

Redis支持RDB和AOF两种持久化方式,持久化功能有效避免了因进程退出造成数据丢失的问题。



RDB持久化是把当前进程数据生成快照保存到磁盘的过程,触发RDB持久化的过程分为手动和自动触发。


手动触发RDB持久化的操作

1. 手动执行SAVE命令

执行命令:

127.0.0.1:6379> SAVE

OK


日志:

35650:M 18 Jul 18:10:42.795 * DB saved on disk


2. 手动执行BGSAVE命令

执行命令:

127.0.0.1:6379> BGSAVE

Background saving started


日志:

35650:M 18 Jul 18:12:48.613 * Background saving started by pid 39301

39301:C 18 Jul 18:12:48.803 * DB saved on disk

39301:C 18 Jul 18:12:48.805 * RDB: 8 MB of memory used by copy-on-write

35650:M 18 Jul 18:12:48.881 * Background saving terminated with success


BGSAVE是针对SAVE阻塞做的优化,Redis涉及RDB持久化的操作优先使用BGSAVE。


自动触发RDB持久化的机制

1. save相关配置,如save m<seconds> n<changes>,m秒内数据集有n次修改,触发BGSAVE。

配置:

save 900 1

save 300 10

save 60 10000


日志:

89428:M 18 Jul 18:46:49.809 * 10000 changes in 60 seconds. Saving...

89428:M 18 Jul 18:46:49.810 * Background saving started by pid 99472

99472:C 18 Jul 18:46:50.030 * DB saved on disk

99472:C 18 Jul 18:46:50.032 * RDB: 8 MB of memory used by copy-on-write

89428:M 18 Jul 18:46:50.113 * Background saving terminated with success


2. 新建主节点的一个从节点,主节点自动BGSAVE生成RDB文件,发送给从节点。

从节点执行命令:

127.0.0.1:6389> SLAVEOF 127.0.0.1 6379

OK


从节点日志:

99498:S 18 Jul 18:56:27.721 * SLAVE OF 127.0.0.1:6379 enabled (user request from 'id=2 addr=127.0.0.1:55238 fd=6 name= age=210 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=32768 obl=0 oll=0 omem=0 events=r cmd=slaveof')

99498:S 18 Jul 18:56:27.829 * Connecting to MASTER 127.0.0.1:6379

99498:S 18 Jul 18:56:27.829 * MASTER <-> SLAVE sync started

99498:S 18 Jul 18:56:27.830 * Non blocking connect for SYNC fired the event.

99498:S 18 Jul 18:56:27.830 * Master replied to PING, replication can continue...

99498:S 18 Jul 18:56:27.830 * Partial resynchronization not possible (no cached master)

99498:S 18 Jul 18:56:27.911 * Full resync from master: 2f949fed88eb2df127e45a4870dce9fe1b37f2b2:1

99498:S 18 Jul 18:56:28.184 * MASTER <-> SLAVE sync: receiving 5241839 bytes from master

99498:S 18 Jul 18:56:28.330 * MASTER <-> SLAVE sync: Flushing old data

99498:S 18 Jul 18:56:28.330 * MASTER <-> SLAVE sync: Loading DB in memory

99498:S 18 Jul 18:56:28.388 * MASTER <-> SLAVE sync: Finished with success


主节点日志:

89428:M 18 Jul 18:56:27.831 * Slave 127.0.0.1:6389 asks for synchronization

89428:M 18 Jul 18:56:27.831 * Full resync requested by slave 127.0.0.1:6389

89428:M 18 Jul 18:56:27.831 * Starting BGSAVE for SYNC with target: disk

89428:M 18 Jul 18:56:27.907 * Background saving started by pid 99508

99508:C 18 Jul 18:56:28.105 * DB saved on disk

99508:C 18 Jul 18:56:28.106 * RDB: 8 MB of memory used by copy-on-write

89428:M 18 Jul 18:56:28.182 * Background saving terminated with success

89428:M 18 Jul 18:56:28.270 * Synchronization with slave 127.0.0.1:6389 succeeded


3. 执行DEBUG RELOAD重新加载Redis时,会触发SAVE操作。

执行命令:

127.0.0.1:6379> DEBUG RELOAD

OK


日志:

35650:M 18 Jul 18:40:22.379 * DB saved on disk

35650:M 18 Jul 18:40:22.433 # DB reloaded by DEBUG RELOAD


4. 执行SHUTDOWN时,会触发SAVE操作。

执行命令:

127.0.0.1:6379> SHUTDOWN


日志:

89428:M 18 Jul 19:08:17.003 # User requested shutdown...

89428:M 18 Jul 19:08:17.003 * Saving the final RDB snapshot before exiting.

89428:M 18 Jul 19:08:17.098 * DB saved on disk

89428:M 18 Jul 19:08:17.098 * Removing the pid file.

89428:M 18 Jul 19:08:17.098 * Removing the unix socket file.

89428:M 18 Jul 19:08:17.098 # Redis is now ready to exit, bye bye...


RDB配置参数总览:

save 900 1

save 300 10

save 60 10000

stop-writes-on-bgsave-error yes

rdbcompression yes

rdbchecksum yes

dbfilename dump.rdb



AOF持久化,以独立日志的方式记录每次写命令,解决了数据持久化的实时性问题。AOF的工作流程包括:append(命令写入),sync(数据同步),rewrite(文件重写)和load(重启加载)。


AOF的rewrite过程可以手动触发,和自动触发。

1. 手动触发

执行命令:

127.0.0.1:6379> BGREWRITEAOF

Background append only file rewriting started


日志:

99615:M 18 Jul 19:44:28.610 * Background append only file rewriting started by pid 99639

99615:M 18 Jul 19:44:28.900 * AOF rewrite child asks to stop sending diffs.

99639:C 18 Jul 19:44:28.900 * Parent agreed to stop sending diffs. Finalizing AOF...

99639:C 18 Jul 19:44:28.900 * Concatenating 0.00 MB of AOF diff received from parent.

99639:C 18 Jul 19:44:28.902 * SYNC append only file rewrite performed

99639:C 18 Jul 19:44:28.903 * AOF rewrite: 4 MB of memory used by copy-on-write

99615:M 18 Jul 19:44:28.923 * Background AOF rewrite terminated with success

99615:M 18 Jul 19:44:28.923 * Residual parent diff successfully flushed to the rewritten AOF (0.00 MB)

99615:M 18 Jul 19:44:28.923 * Background AOF rewrite finished successfully


2. 自动触发

根据下面2个参数自动触发。

auto-aof-rewrite-min-size 64mb,运行AOF重写时,文件最小空间。

auto-aof-rewrite-percentage 100,当前AOF文件空间,和上次重写后AOF文件空间的比值。


AOF配置参数总览:

appendonly yes

appendfilename "appendonly.aof"

appendfsync everysec

no-appendfsync-on-rewrite no

auto-aof-rewrite-percentage 100

auto-aof-rewrite-min-size 64mb



RDB和AOF两种持久化方式对比:

1. RDB生成的是二进制文件,占用空间小,恢复效率高。AOF生成的是遵守Redis序列化协议的文本文件,占用空间大,恢复效率低。

~/keepmoving/6379/data $ file dump.rdb appendonly.aof 

dump.rdb:       data

appendonly.aof: ASCII text, with CRLF line terminators


2. RDB过程对操作系统造成的负载,一般比AOF要大,RDB是对全量数据集做快照,AOF是将数据集增量追加进日志文件,其解决了数据持久化的实时性。


若感兴趣可关注订阅号”数据库最佳实践”(DBBestPractice).

技术分享图片

Redis的持久化

标签:concat   http   blocking   back   idle   psu   saving   同步   ica   

原文地址:http://blog.51cto.com/coveringindex/2147120

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!