redis-- persistence strategy

4.2.2 Persistence mode
(1) RDB way
1. What is RDB way?
Redis Database (RDB), is within the specified time interval the memory snapshot of a dataset written to disk, the number of
time data recovery snapshot files directly re-read memory.
RDB save the data set (all data) at some point in time. In a binary file, there is only one storage
file. The default is dump.rdb. RDB technology is ideally suited for backup, you can save the last hour, a day, a
whole month's data. Save the data file is written in a separate process, does not affect the normal use of Redis. RDB number of recovery
when data faster than other AOF speed.

2. How to achieve?
Data RDB way of persistence, you can only configure the redis.conf file, the default configuration is enabled.
Search SNAPSHOTTING redis.conf in the configuration file, look in between the start and end of the comment on RDB
configuration instructions. With SNAPSHOTTING home where there are three.
①: Configure execution time strategy RDB generated snapshot file.
Redis to set, when it "N seconds dataset has at least M key change" the condition is satisfied,
automatically save a data set.
Configuration format: Save <seconds The> <Changes>
Save 900. 1
Save 300 10
Save 60 10000
②: dbfilename: RDB provided file name, the default file name dump.rdb
③: the dir: RDB specify a storage location of the file, the default Yes. / current directory
configuration steps:
①: View ps -ef | grep redis, if redis service to start, stop.


②: redis.conf modify files, back up before the amendment, execution cp redis.conf bak_redis.conf

View enabled by default RDB file

③: edit redis.conf increase save the configuration, modify the file name. vim redis.conf

 

Modify the content:

 

The original default dump.rdb deleted, modified redis.conf, restart Redis
④: In 20 seconds, change in the value of three key

⑤: see the generated file rdb

3. 总结
优点:由于存储的是数据快照文件,恢复数据很方便,也比较快
缺点:
1)会丢失最后一次快照以后更改的数据。如果你的应用能容忍一定数据的丢失,那么
使用rdb是不错的选择;如果你不能容忍一定数据的丢失,使用 rdb 就不是一个很好的选择。
2)由于需要经常操作磁盘,RDB 会分出一个子进程。如果你的 redis 数据库很大的话,
子进程占用比较多的时间,并且可能会影响 Redis 暂停服务一段时间(millisecond 级别),
如果你的数据库超级大并且你的服务器 CPU 比较弱,有可能是会达到一秒。

 

 

2 ) AOF 方式
1. 什么是 AOF 方式
Append-only File(AOF),Redis 每次接收到一条改变数据的命令时,它将把该命令写到
一个 AOF 文件中(只记录写操作,读操作不记录),当 Redis 重启时,它通过执行 AOF 文件
中所有的命令来恢复数据。
2. 如何实现
AOF 方式的数据持久化,仅需在 redis.conf 文件中配置即可
配置项:
①:appendonly:默认是 no,改成 yes 即开启了 aof 持久化
②:appendfilename:指定 AOF 文件名,默认文件名为 appendonly.aof
③:dir :指定 RDB 和 AOF 文件存放的目录,默认是 ./
④:appendfsync:配置向 aof 文件写命令数据的策略:
no:不主动进行同步操作,而是完全交由操作系统来做(即每 30 秒一次),比较快但不
是很安全。
always:每次执行写入都会执行同步,慢一些但是比较安全。
everysec:每秒执行一次同步操作,比较平衡,介于速度和安全之间。这是默认项。
⑤:auto-aof-rewrite-min-size:允许重写的最小 AOF 文件大小,默认是 64M 。当 aof 文件大
于 64M 时,开始整理 aof 文件,去掉无用的操作命令。缩小 aop 文件。
例 1:
①:停止运行的 redis ,备份要修改的 redis.conf
②:查看 redis 安装目录/src 下有无 .aof 文件。默认是在 redis 的当前目录

③:编辑 redis.conf
设置 appendonly 为 yes 即可。

查看 appendfsync 的当前策略。
查看 appendfilname 的文件名称

 

 

④:在 redis 客户端执行写入命令

 

⑤查看 aof 文件

 

 

(3 ) 总结
1)append-only 文件是另一个可以提供完全数据保障的方案;
2)AOF 文件会在操作过程中变得越来越大。比如,如果你做一百次加法计算,最后你只会
在数据库里面得到最终的数值,但是在你的 AOF 里面会存在 100 次记录,其中 99 条记
录对最终的结果是无用的;但 Redis 支持在不影响服务的前提下在后台重构 AOF 文件,让
文件得以整理变小
3)可以同时使用这两种方式,redis 默认优先加载 aof 文件(aof 数据最完整);

Guess you like

Origin www.cnblogs.com/Tpf386/p/11229226.html