Redis持久化策略

1、RDB:快照-纯缓存使用可只用RDB作为持久化方案,甚至不持久化

  • 时间点问题:比如 8:00开始快照,8:10分才快照成功,那么快照的是8:00还是8:10分的数据
  • 方式:
    • save    【命令】  
    • bgsave 【命令】- fork[linux]另起一个子进程,数据隔离(写时复制)进行数据快照
    •  
    • save <seconds> <changes> 【配置-注意:这边实际上调用的是bgsave命令】

      参考配置文件中的说明:大概指的是指定时间内至少有多少个数据改变

ps:关于写时复制:解决的问题-全量复制的问题,如果我们全量复制出一份8点时候的快照数据,则需要的内存会很大。而写时复制,强调的是当需要修改数据的时候才去拷贝(而且一般只是指针修改);那根据软件的局部原理,其实我们需要修改的数据一般是没有那么多的。那就大大减少了空间和时间资源的损耗。      

################################ SNAPSHOTTING  ################################
#
# Save the DB on disk:
#
#   save <seconds> <changes>
#
#   Will save the DB if both the given number of seconds and the given
#   number of write operations against the DB occurred.
#
#   In the example below the behaviour will be to save:
#   after 900 sec (15 min) if at least 1 key changed
#   after 300 sec (5 min) if at least 10 keys changed
#   after 60 sec if at least 10000 keys changed
#
#   Note: you can disable saving completely by commenting out all "save" lines.
#
#   It is also possible to remove all the previously configured save
#   points by adding a save directive with a single empty string argument
#   like in the following example:
#
#   save ""

save 900 1
save 300 10
save 60 10000

 

 

ps:涉及点-父子进程之间,数据隔离&CopyOnWrite(仅写时复制)

 

 

 

   

2、AOF:日志(一般做数据库使用时候需要用到,保证数据的完整性)

  • 开启【配置文件】:appendonly yes
  • 文件名【配置文件】:appendfilename "appendonly.aof"
  • 刷出缓冲【配置文件】:appendfsync everysec[always|no]             - 使用always会影响redis的性能
  • auto-aof-rewrite-percentage 100
    auto-aof-rewrite-min-size 64mb

  • 重写aof,主要是为了避免文件过大,AOF和混合模式的格式会有不同

     

     

3、混合 - 兼具两者的优点:利用rdb的快速恢复和aof的数据完整性【比如用于主从复制中,先读取rdb文件,再从aof读取增量操作】

  • 开启【配置文件】:aof-use-rdb-preamble yes
posted @ 2020-09-14 13:19  gabin  阅读(120)  评论(0编辑  收藏  举报