AOF和RDB模式

1.官方说明:

BydefaultRedisasynchronouslydumpsthedatasetondisk.Thismodeis

goodenoughinmanyapplications,butanissuewiththeRedisprocessor

apoweroutagemayresultintoafewminutesofwriteslost(dependingon

theconfiguredsavepoints).

TheAppendOnlyFileisanalternativepersistencemodethatprovides

muchbetterdurability.Forinstanceusingthedefaultdatafsyncpolicy

(seelaterintheconfigfile)Rediscanlosejustonesecondofwritesina

dramaticeventlikeaserverpoweroutage,orasinglewriteifsomething

wrongwiththeRedisprocessitselfhappens,buttheoperatingsystemis

stillrunningcorrectly.

AOFandRDBpersistencecanbeenabledatthesametimewithoutproblems.

IftheAOFisenabledonstartupRediswillloadtheAOF,thatisthefile

withthebetterdurabilityguarantees.

Pleasecheckhttp://redis.io/topics/persistenceformoreinformation.

2.配置

#RDB模式配置

save9001#当有一条Keys数据被改变时,15分钟刷新到Disk一次

save30010#当有10条Keys数据被改变时,5分钟刷新到Disk一次

save6010000#当有10000条Keys数据被改变时,1分钟刷新到Disk一次

#AOF模式配置

appendonlyyes#启用AOF持久化方式

appendfilenameappendonly.aof#AOF文件的名称,默认为appendonly.aof

#appendfsyncalways#每次收到写命令就立即强制写入磁盘,是最有保证的完全的持久化,但速度也是最慢的,一般不推荐使用。

appendfsynceverysec#每秒钟强制写入磁盘一次,在性能和持久化方面做了很好的折中,是受推荐的方式。

#appendfsyncno#完全依赖OS的写入,一般为30秒左右一次,性能最好但是持久化最没有保证,不被推荐。

auto-aof-rewrite-percentage100#当前AOF文件大小是上次日志重写得到AOF文件大小的二倍时,自动启动新的日志重写过程。

auto-aof-rewrite-min-size64mb#当前AOF文件启动新的日志重写过程的最小值,避免刚刚启动Reids时由于文件尺寸较小导致频繁的重写。

3.比较测试:

在测试环境下,分别在RDB与AOF模式下,向redis-3.0.7写入同一数值。1秒后,使用killall-9redis-server模拟生产事故发生。exists命令查看key,AOF模式恢复了kill之前写入的数值,而RDB模式没有恢复。

参考:http://redis.io/topics/persistence