Redis学习之数据持久化与数据恢复

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wzq__janeGreen_/article/details/81489526

redis缓存是支持数据持久化的操作,也就是可以把内存中的数据持久化到硬盘当中,和数据库有些相似,这也是redis和memcache的区别之一。

redis数据持久化常用的方式有两种:

(1)Snapshotting (RDB)

在指定的时间间隔内生成数据集的时间点快照(point-in-time snapshot),也是redis持久化的默认方式。

(2)Append Only File (AOF)

持久化记录服务器执行的所有操作命令,并在服务启动时,通过重新执行这些命令来还原数据集。

RDB的生成方式:

(1)通过执行命令手动生成

可通过SAVE和BGSAVE命令对数据进行持久化,生成RDB文件。

SAVE命令,会阻塞当前服务的进程,在阻塞期间,服务器不能处理任何命令请求,直到缓存数据持久化完成。

BGSAVE命令,顾名思义就是在后台执行,不会阻塞当前服务,会派生出 一个子进程,由子进程负责持久化数据,父进程继续处理命令请求。

(2)通过配置自动生成

可以通过redis.conf配置文件找到SNAPSHOTTING配置,修改save选项,让服务器每隔一段时间自动执行BGSAVE。

配置如下:

 
  1. <span style="font-size:12px;">################################ SNAPSHOTTING #################################

  2. #

  3. # Save the DB on disk:

  4. #

  5. # save <seconds> <changes>

  6. #

  7. # Will save the DB if both the given number of seconds and the given

  8. # number of write operations against the DB occurred.

  9. #

  10. # In the example below the behaviour will be to save:

  11. # after 900 sec (15 min) if at least 1 key changed

  12. # after 300 sec (5 min) if at least 10 keys changed

  13. # after 60 sec if at least 10000 keys changed

  14. #

  15. # Note: you can disable saving at all commenting all the "save" lines.

  16.  
  17. save 900 1 //服务器在900秒内,对缓存数据库至少修改了1次

  18. save 300 10 //服务器在300秒内,对缓存数据库至少修改了1次

  19. save 60 10000 //服务在60秒内,对缓存数据库至少修改了10000次

  20.  
  21. # Compress string objects using LZF when dump .rdb databases?

  22. # For default that's set to 'yes' as it's almost always a win.

  23. # If you want to save some CPU in the saving child set it to 'no' but

  24. # the dataset will likely be bigger if you have compressible values or keys.

  25. rdbcompression yes

  26.  
  27. # The filename where to dump the DB

  28. dbfilename dump.rdb //持久化数据存到磁盘的文件名称

  29.  
  30. # The working directory.

  31. #

  32. # The DB will be written inside this directory, with the filename specified

  33. # above using the 'dbfilename' configuration directive.

  34. #

  35. # Also the Append Only File will be created inside this directory.

  36. #

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

  38. dir ./ //存到磁盘的路径</span>

只要满足上面三个save配置中的一个,redis就会自动进行数据快照,持久化到硬盘中。用户可根据自己需求进行配置。

看到上面的配置我会很好奇,服务器怎么知道我在多长的时间对缓存数据修改了多少次了?后来发现Redis服务其中有个dirty和一个lastsave时间戳。

当服务器执行一个数据修改命令之后,dirty计数器数值会进行更新。

lastsave则是记录上次服务器执行BGSAVE命令的时间,在这就不详细解释了。

AOF

AOF持久化数据是通过保存Redis服务所有的操作命令,下次启动服务时,从新执行这些操作命令来还原缓存数据。

AOF文件刷新有三种方式:

(1)appendfsync always - 每提交一个修改命令都调用fsync刷新到AOF文件,非常非常慢,但也非常安全
(2)appendfsync everysec - 每秒钟都调用fsync刷新到AOF文件,很快,但可能会丢失一秒以内的数据
(3)appendfsync no - 依靠OS进行刷新,redis不主动刷新AOF,这样最快,但安全性就差
默认并推荐每秒刷新,这样在速度和安全上都做到了兼顾

数据恢复:

RDB

RDB恢复数据的方式没有专门的操作命令去执行,redis服务启动时,会自动查找RDB文件进行加载,指导RDB文件加载完成为止。

AOF

服务器在启动时,通过载入和执行AOF文件中保存的命令来还原服务器关闭之前的数据库状态,具体过程:

(1)载入AOF文件

(2)创建模拟客户端

(3)从AOF文件中读取一条命令

(4)使用模拟客户端执行命令

(5)循环读取并执行命令,直到全部完成

如果同时启用了RDB和AOF方式,AOF优先,启动时只加载AOF文件恢复数据

猜你喜欢

转载自blog.csdn.net/wzq__janeGreen_/article/details/81489526