redis学习--的持久化数据备份(RDB和AOF)

接上一篇:安装window下的redis,redis可视化管理工具(Redis Desktop Manager)安装,基础使用,实例化项目

 

一、dump.rdb文件是怎么生成的

二、什么是redis持久化

三、redis的RDB是什么?

四、redis配置文件redis.config相关配置

五、redis优点

六、redis缺点

 

redismemcache作为缓存数据库强大的地方:(1)支持数据类型比较多,(2)redis持久化功能。

 

一、dump.rdb文件是怎么生成的

在redis服务挂掉的时候,根据redis的配置文件,会自动备份数据到本地。

dump.rdb是由redis服务器自动生成的。

默认情况下,每隔一段时间redis服务器程序会自动对数据库做一次遍历,把内存快照写在一个叫做“dump.rdb”文件里,这种持久化机制叫做SNAPSHOT。有了SNAPSHOT后,如果服务器宕机,重新启动redis服务器程序时redis会自动加载dump.rdb,将数据库恢复到上一次SNAPSHOT的状态。

 

至于多久一次做一次SNAPSHOT,SNAPSHOT文件的路径和文件名,你可以在redis的config文件中指定。

 

二、什么是redis的持久化

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

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

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

(3)如果你只希望你的数据在服务器运行的时候存在,你可以不使用任何持久化方式。

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

 

三、Redis的RDB是什么

RDB在指定的时间间隔内将内存中的数据集快照写入磁盘,也就是SNAPSHOT快照。

它恢复时是将快照文件直接写入到内存里,redis会单独创建(fork)一个子进程进行持久化吗,会先将数据写入到一个临时文件中,持久化过程都结束了,在用这个临时文件替换上从持久化好的文件。

整个过程中,主进程是不进行任何IO操作的,这就确保了极高的性能。如果需要进行大规模数据的恢复,且对于数据恢复的完整性不是敏感,那RDB方式要比AOF方式更加高效。、

redis的缺点是最后一次持久化后的数据可能丢失。

 

四、redis配置文件redis.config相关配置

(一)RDB快照方式持久化磁盘

先看redis.window.config文件

################################ 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

# 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 DB
dbfilename 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 ./

 

4.1如何触发RDB快照

配置文件中默认的快照配置

save 900 1
save 300 10
save 60 10000

上面的意思就是说:

(1)如果至少一个键改变,会在900秒(15分钟)之后执行save操作

(2)如果至少改变10个键,则在300秒(5分钟)之后执行save操作

(3)如果至少改变10000个键,则在60秒(1分钟)之后执行save操作

命令save:只管保存,其他不管

命令bgsave:redis会在后台异步进行快照操作,快照的同时还可以响应客户端请求。

 

4.2默认的RDB方式保存的是dump.rdb文件,恢复识别也是dump.rdb

 

4.3stop-writes-on-bgsave-error yes

如果后台保存到磁盘发生错误,将停止写操作,使用LZF压缩rdb文件,这会耗CPU, 但是可以减少磁盘占用。

 

4.4rdbcompression yes

保存rdb和加载rdb文件的时候校验,可以防止错误,但是要付出约10%的性能,可以关闭,提高性能。

 

4.5rdbchecksum yes

导出的rdb文件名

 

4.6dbfilename dump.rdb

设置工作目录,rdb文件会写到该目录,append only file也会存储在该目录下

 

4.7dir ./

redis会自动快照保存到磁盘或者调用bgsave,是后台进程完成的,其他客户端任然可以读写redis服务,后台保存快照到磁盘会占用大量的内存。

 

(二)AOF(append-only file)方式持久化

另外一种方式为递增的方式,将会引起数据变化的操作,持久化到文件中,重启redis的时候,通过操作命令,恢复数据。

每次执行写操作命令之后,都会将数据写到server.aofbuf中。

#
# More details please check the following article:
# http://antirez.com/post/redis-persistence-demystified.html
#
# If unsure, use "everysec".

# appendfsync always
appendfsync everysec
# appendfsync no

# When the AOF fsync policy is set to always or everysec, and a background
# saving process (a background save or AOF log background rewriting) is
# performing a lot of I/O against the disk, in some Linux configurations
# Redis may block too long on the fsync() call. Note that there is no fix for
# this currently, as even performing fsync in a different thread will block
# our synchronous write(2) call.

当配置为always的时候,每次server.aofbuf中的数据写入到文件之后,才会返回到客户端,这样可以保证数据不丢失,但是频繁的IO操作,会降低性能。

everysec每秒写一次,这可能会丢失一秒内的操作。

 

五、redis优点

(1)适合大规模的数据恢复

(2)对数据完整性和一致性要求不高

 

六、redis缺点

(1)在一定间隔时间做一次备份,所以redis意外的挂掉的话,就会丢失最后一次快照后的所有修改

(2)fork的时候,内存中的数据被被克隆一份,大致2倍的膨胀性需求考虑

 

posted @ 2018-08-13 22:47  saucxs  阅读(1180)  评论(0编辑  收藏  举报