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

接上一篇:

一、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文件

900 sec (15 min) at least 1300 sec (5 min) at least 1060 sec at least 10000"save"""save 900 1<span style="color: #000000;">
save
300 10<span style="color: #000000;">
save
60 10000<span style="color: #000000;">

By <span style="color: #0000ff;">default Redis will stop accepting writes <span style="color: #0000ff;">if<span style="color: #000000;"> RDB snapshots are enabled

(at least one save point) and the latest background save failed.

This will make the user aware (<span style="color: #0000ff;">in<span style="color: #000000;"> 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 <span style="color: #0000ff;">if<span style="color: #000000;"> you have setup your proper monitoring of the Redis server

and persistence,you may want to disable <span style="color: #0000ff;">this<span style="color: #000000;"> feature so that Redis will

<span style="color: #0000ff;">continue to work as usual even <span style="color: #0000ff;">if there are problems <span style="color: #0000ff;">with<span style="color: #000000;"> disk,# permissions,and so forth.

stop-writes-on-bgsave-<span style="color: #000000;">error yes

Compress string objects using LZF when dump .rdb databases?<span style="color: #000000;">

For <span style="color: #0000ff;">default that's set to 'yes' as it'<span style="color: #000000;">s almost always a win.

If you want to save some CPU <span style="color: #0000ff;">in the saving child set it to 'no'<span style="color: #000000;"> but

the dataset will likely be bigger <span style="color: #0000ff;">if<span style="color: #000000;"> you have compressible values or keys.

rdbcompression yes

Since version 5<span style="color: #000000;"> 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%<span style="color: #000000;">) when saving and loading RDB files,so you can disable it

<span style="color: #0000ff;">for<span style="color: #000000;"> maximum performances.

RDB files created <span style="color: #0000ff;">with<span style="color: #000000;"> 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 <span style="color: #0000ff;">this directory,<span style="color: #0000ff;">with<span style="color: #000000;"> the filename specified

above using the 'dbfilename'<span style="color: #000000;"> configuration directive.

The Append Only File will also be created inside <span style="color: #0000ff;">this<span style="color: #000000;"> directory.

Note that you must specify a directory here,not a file name.

dir ./

4.1如何触发RDB快照

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

save 900 1300 1060 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中。

"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 <span style="color: #0000ff;">long on the fsync() call. Note that there is no fix <span style="color: #0000ff;">for<span style="color: #000000;">

<span style="color: #0000ff;">this currently,as even performing fsync <span style="color: #0000ff;">in<span style="color: #000000;"> a different thread will block

our synchronous write(2) call.

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

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

五、redis优点

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

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

六、redis缺点

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

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

相关文章

文章浏览阅读1.3k次。在 Redis 中,键(Keys)是非常重要的概...
文章浏览阅读3.3k次,点赞44次,收藏88次。本篇是对单节点的...
文章浏览阅读8.4k次,点赞8次,收藏18次。Spring Boot 整合R...
文章浏览阅读978次,点赞25次,收藏21次。在Centos上安装Red...
文章浏览阅读1.2k次,点赞21次,收藏22次。Docker-Compose部...
文章浏览阅读2.2k次,点赞59次,收藏38次。合理的JedisPool资...