测试人员应该知道的Redis知识(十一) Redis持久化之RDB

本文介绍了Redis的RDB持久化机制,包括默认的快照备份策略,如900秒内1次变更、300秒内10次变更和60秒内10000次变更触发保存。此外,讨论了配置选项如`stop-writes-on-bgsave-error`、`rdbcompression`和`rdbchecksum`的用途,以及它们对性能和数据安全的影响。RDB文件的默认存储位置也在文中提及。
摘要由CSDN通过智能技术生成

一、概述

 

Redis优于Memcached的重要一点就是Redis的数据可以进行持久化,在发生一些特殊情况后,可以用备份的数据进行还原。

Redis 提供了不同级别的持久化方式:

  1. RDB持久化方式能够在指定的时间间隔能对你的数据进行快照存储。

  2. AOF持久化方式记录每次对服务器写的操作,当服务器重启的时候会重新执行这些命令来恢复原始的数据,AOF命令以redis协议追加保存每次写的操作到文件末尾.Redis还能对AOF文件进行后台重写,使得AOF文件的体积不至于过大。

  3. 如果我们只希望自己的数据在服务器运行的时候存在,也可以不使用任何持久化方式。

  4. 我们也可以同时开启两种持久化方式, 在这种情况下, 当redis重启的时候会优先载入AOF文件来恢复原始的数据,因为在通常情况下AOF文件保存的数据集要比RDB文件保存的数据集要完整。

接下去让我们先来看一下RDB相关的配置内容。Reids配置文件是位于安装目录下的redis.conf,如果没有找到的话,可以从源码文件夹中copy,一般我们不会对原始文件进行修改,而是copy一份。

cp redis.conf myredis.conf

让Redis以制定配置文件启动的话,我们可以执行以下语句

./redis-server /path/to/redis.conf

 

二、SNAPSHOTTING

################################ 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 1save 300 10save 60 10000

这部分配置主要决定了RDB的备份策略,以save <seconds> <changes>的方式进行设定,我们可以看到,默认的策略有3条,分别表示,如果在900秒内发生1次变更、300秒内发生10次变更、60秒内发生10000次变更,则对数据库进行快照存储。​​​​​​​

# By default Redis will stop accepting writes if RDB snapshots are enabled# (at least one save point) and the latest background save failed.# This will make the user aware (in a hard way) that data is not persisting# on disk properly, otherwise chances are that no one will notice and some# disaster will happen.## If the background saving process will start working again Redis will# automatically allow writes again.## However if you have setup your proper monitoring of the Redis server# and persistence, you may want to disable this feature so that Redis will# continue to work as usual even if there are problems with disk,# permissions, and so forth.stop-writes-on-bgsave-error yes# Compress string objects using LZF when dump .rdb databases?# For default that's set to 'yes' as it's almost always a win.# If you want to save some CPU in the saving child set it to 'no' but# the dataset will likely be bigger if you have compressible values or keys.rdbcompression yes# Since version 5 of RDB a CRC64 checksum is placed at the end of the file.# This makes the format more resistant to corruption but there is a performance# hit to pay (around 10%) when saving and loading RDB files, so you can disable it# for maximum performances.## RDB files created with checksum disabled have a checksum of zero that will# tell the loading code to skip the check.rdbchecksum yes# The filename where to dump the DBdbfilename dump.rdb# The working directory.## The DB will be written inside this directory, with the filename specified# above using the 'dbfilename' configuration directive.## The Append Only File will also be created inside this directory.## Note that you must specify a directory here, not a file name.dir /usr/local/var/db/redis/

stop-writes-on-bgsave-error设置为yes,当后台进程在进行dump rdb的时候因为一些原因,比如磁盘写满等原因,造成此次dump失败,则Redis会拒绝新的写入。

rdbcompression设置为yes,那么Redis会使用LZF压缩字符串,然后写到rdb文件中去;设置为no,RDB进程节省一点CPU时间,但是可能最后的rdb文件会很大,在当前的硬件条件下,我们一般都会设置为yes

rdbchecksum设置为yes,会在redis重启后,从rdb文件向内存写数据之前,是否先检测该rdb文件是否损坏。此选项在RDB的保存及加载的时候会有10%左右的开销,追求极致性能的话,可以关闭此选项,不过因为这个过程本身还是比较快的,与rdbcompression类似,在当前的硬件条件下,我们一般都会设置为yes

dbfilename 及dir 主要决定了我们保存RDB文件的位置,上例中,RDB文件将存储在/usr/local/var/db/redis/dump.rdb。

三、总结

今天给大家介绍了一下Redis持久化之RDB配置相关的内容,如果大家还有别的什么问题,可以在评论中留言。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值