Bootstrap

Redis的内存淘汰策略(八)

🚀 优质资源分享 🚀

学习路线指引(点击解锁) 知识定位 人群定位
🧡 Python实战微信订餐小程序 🧡 进阶级 本课程是python flask+微信小程序的完美结合,从项目搭建到腾讯云部署上线,打造一个全栈订餐系统。
💛Python量化交易实战💛 入门级 手把手带你打造一个易扩展、更安全、效率更高的量化交易系统

一:Redis的AOF是什么?

日志的形式来记录每个写操作(读操作不记录),将Redis执行过的所有写指令记录下来(读操作不记录),只许追加文件但不可以改写文件,redis启动之初会读取该文件重新构建数据,换言之,redis重启的话就根据日志文件的内容将写指令从前到后执行一次以完成数据的恢复工作。RDB可以搞定备份恢复的事情,为什么还会出现AOF?

使用RDB进行保存时候,如果Redis服务器发送故障,那么会丢失最后一次备份的数据!AOF出现是来解决这个问题!同时出现RDB和AOF是冲突呢?还是协作?是协作,但是首先启动找的是aof。当redis服务器挂掉时,重启时将按照以下优先级恢复数据到内存:

  • 如果只配置AOF,重启时加载AOF文件恢复数据;
  • 如果同时 配置了RBD和AOF,启动是只加载AOF文件恢复数据;
  • 如果只配置RBD,启动是加载RDB文件恢复数据。

恢复时需要注意,要是主库挂了不能直接重启主库,否则会直接覆盖掉从库的AOF文件,一定要确保要恢复的文件都正确才能启动,否则会冲掉原来的文件。

二:Redis配置文件redis.conf中关于AOF的相关配置

############################## APPEND ONLY MODE ###############################
# By default Redis asynchronously dumps the dataset on disk. This mode is
# good enough in many applications, but an issue with the Redis process or
# a power outage may result into a few minutes of writes lost (depending on
# the configured save points).
#
# The Append Only File is an alternative persistence mode that provides
# much better durability. For instance using the default data fsync policy
# (see later in the config file) Redis can lose just one second of writes in a
# dramatic event like a server power outage, or a single write if something
# wrong with the Redis process itself happens, but the operating system is
# still running correctly.
#
# AOF and RDB persistence can be enabled at the same time without problems.
# If the AOF is enabled on startup Redis will load the AOF, that is the file
# with the better durability guarantees.
#
# Please check http://redis.io/topics/persistence for more information.
#默认redis使用的是rdb方式持久化,这种方式在许多应用中已经足够用了。
但是redis如果中途宕机,会导致可能有几分钟的数据丢失,根据save来策略进行持久化,
Append Only File是另一种持久化方式,可以提供更好的持久化特性。
Redis会把每次写入的数据在接收后都写入 appendonly.aof 文件,
每次启动时Redis都会先把这个文件的数据读入内存里,先忽略RDB文件
appendonly no #如果要开启,改为yes
# The name of the append only file (default: "appendonly.aof")
# aof文件名
appendfilename "appendonly.aof"
# The fsync() call tells the Operating System to actually write data on disk
# instead of waiting for more data in the output buffer. Some OS will really flush
# data on disk, some other OS will just try to do it ASAP.
#
# Redis supports three different modes:
#
# no: don't fsync, just let the OS flush the data when it wants. Faster.
# always: fsync after every write to the append only log. Slow, Safest.
# everysec: fsync only one time every second. Compromise.
#
# The default is "everysec", as that's usually the right compromise between
# speed and data safety. It's up to you to understand if you can relax this to
# "no" that will let the operating system flush the output buffer when
# it wants, for better performances (but if you can live with the idea of
# some data loss 
;