Redis5.0.3配置文件详解(易读白话翻译)惰性回收与AOF持久化

编程

############################# LAZY FREEING 惰性回收 ####################################

# Redis has two primitives to delete keys. One is called DEL and is a blocking

# deletion of the object. It means that the server stops processing new commands

# in order to reclaim all the memory associated with an object in a synchronous

# way. If the key deleted is associated with a small object, the time needed

# in order to execute the DEL command is very small and comparable to most other

# O(1) or O(log_N) commands in Redis. However if the key is associated with an

# aggregated value containing millions of elements, the server can block for

# a long time (even seconds) in order to complete the operation.

# Redis提供了两个命令来手动删除keys,其中一个是大家熟知的del,另外一个是unlink

# "del"命令:删除是阻塞式(执行删除时,后续的命令就要排队等待)删除以便释放空间,如果一个Key比较小则删除很快,影响小,但如果这个Key对应的对象非常大,那么删除会很耗时,在高并发的系统里面会阻塞后面的请求,如果系统架构设计不合理则可能导致整个业务系统不可供,造成严重的生产事故

# "unlink"命令:是异步的尽可能快的逐步删除,它所需的时间复杂度是O(1),Redis会启动另外一个线程来执行真正的删除并回收内存的操作,它不会阻塞后续命令。比如flushall flushdb命令也是异步执行的。

#

# For the above reasons Redis also offers non blocking deletion primitives

# such as UNLINK (non blocking DEL) and the ASYNC option of FLUSHALL and

# FLUSHDB commands, in order to reclaim memory in background. Those commands

# are executed in constant time. Another thread will incrementally free the

# object in the background as fast as possible.

#

# DEL, UNLINK and ASYNC option of FLUSHALL and FLUSHDB are user-controlled.

# It"s up to the design of the application to understand when it is a good

# idea to use one or the other. However the Redis server sometimes has to

# delete keys or flush the whole database as a side effect of other operations.

# Specifically Redis deletes objects independently of a user call in the

# following scenarios:

# 除了用户可以使用del,unlink,flushall,flushdb删除key,Redis Server在某些情况下不得不删除Key,甚至清空整个db以保证服务的可用性,下面列举了4种情况

#

# 1) On eviction, because of the maxmemory and maxmemory policy configurations,

# in order to make room for new data, without going over the specified

# memory limit.

# 为了避免Redis使用的内存超过"maxmemory",且一直在这种状态下运行,Redis Server会根据选择的删除策略去自动删除一些Key,以释放空间给其他数据使用。

#

# 2) Because of expire: when a key with an associated time to live (see the

# EXPIRE command) must be deleted from memory.

# Key设置的过期时间到了,当用户访问这个Key会自动删除,或者Redis Server定期将这种Key删除。

#

# 3) Because of a side effect of a command that stores data on a key that may

# already exist. For example the RENAME command may delete the old key

# content when it is replaced with another one. Similarly SUNIONSTORE

# or SORT with STORE option may delete existing keys. The SET command

# itself removes any old content of the specified key in order to replace

# it with the specified string.

# 一些命令的底层实现就是先删除再新增,所以再使用这些命令的时候会执行删除操作,比如SET,SORT,RENAME

#

# 4) During replication, when a replica performs a full resynchronization with

# its master, the content of the whole database is removed in order to

# load the RDB file just transferred.

# 主从模式下,如果断网重连后触发了"完全同步",也会将整个DB数据删除掉,然后再从RDB文件/SOCKET中加载所有数据

#

# In all the above cases the default is to delete objects in a blocking way,

# like if DEL was called. However you can configure each case specifically

# in order to instead release memory in a non-blocking way like if UNLINK

# was called, using the following configuration directives:

# 上面的4种情况,Redis Server删除数据都是阻塞式删除,就像"del"命令。我们可以将这4种情况的设置为异步删除,就像命令"unlink"一样

lazyfree-lazy-eviction no

lazyfree-lazy-expire no

lazyfree-lazy-server-del no

replica-lazy-flush no

############################## 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).

# Redis的"bgsave"可以异步的将数据集导出到RDB文件中,这种持久化方式满足了大多数的应用,但是有一种情况是当因为一些情况挂掉,比如断电,根据"save xxx"的配置可能会导致几分钟的数据丢失,在一些要求高的系统中这种情况是不被允许的。

#

# 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.

# Redis提供了"Append Only File"新的持久化技术,该技术理论上可以做到当发生断电时让丢失的数据小于等于1秒,或者服务器本身没有挂,只是Redis Server程序挂了,甚至只有一个single write丢失

#

# 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.

# AOF和RDB两种持久化技术可以同时开启,如果AOF开启了,那么启动Redis时,是从AOF文件中加载数据的,因为它保存的数据更完整,提供更好的持久化功能

#

# Please check http://redis.io/topics/persistence for more information.

# 更多的信息请出门左转到:http://redis.io/topics/persistence for more information

# 开启AOF,"appendonly"设置为yes

appendonly no

# The name of the append only file (default: "appendonly.aof")

# 指定AOF文件名,此文件存放的目录和RDB是共用的,使用"dir"进行指定

appendfilename "appendonly.aof"

# 对于没有OS知识的朋友,接下来的appendfsync功能可以先要去百度找操作系统写文件缓冲区的知识点,fsync不同的选项决定了写入缓冲区的数据什么时候真正写到磁盘上

# 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.

# 系统调用"fsync()"告诉OS要真正的将数据写入到磁盘上,而不是写入到缓冲区当中。一些OS会立即写到磁盘,一些OS可能会尽可能快的藏尸将数据写到磁盘

#

# Redis supports three different modes:

# Redis支持三种不同的模式:

#

# no: don"t fsync, just let the OS flush the data when it wants. Faster.

# 模式1-"no":不调用OS的fsync函数,让OS自己决定什么时候将缓冲区的数据写入到磁盘上,该模式对Redis来说速度最快

#

# always: fsync after every write to the append only log. Slow, Safest.

# 模式2-"always":每次"写操作"都会调用一次fsync函数,这种方式最安全,但是速度是最慢的

#

# everysec: fsync only one time every second. Compromise.

# 模式3-"everysec":每一秒钟调用一次fsync,这是一种这种折中方案。

#

# 看到这里顺便提一下,在Redis中随处可见这种思想,比如前面近似LRU的随机算法,有序集合底层数据结构中结合Hash表和跳跃表实现高效的单个和范围查询,过期key的惰性删除等等

# 在我们自己设计系统、开发模块、甚至生活中也可以将这个思想好好运用

#

# 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 consider the default persistence mode that"s snapshotting),

# or on the contrary, use "always" that"s very slow but a bit safer than

# everysec.

# 默认模式是"everysec"的,这是结合速度和安全性的这种方案。如果你不考虑系统DOWN可能带来的数据丢失,可以将模式设置为"no",而如果你想数据完全不丢,且愿意牺牲性能,可以将模式设置为"always"

#

# More details please check the following article:

# http://antirez.com/post/redis-persistence-demystified.html

# 更多的细节请出门左转:http://antirez.com/post/redis-persistence-demystified.html

# 另外大牛"antirez"还开发了基于Redis的神经网络训练模块(neural-redis)和分布式作业队列(Disque)

# If unsure, use "everysec".

# 如果自己不确定到底使用哪一种,就使用默认值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 long on the fsync() call. Note that there is no fix for

# this currently, as even performing fsync in a different thread will block

# our synchronous write(2) call.

# 当AOF模式设置为"everysec"或者"always",执行后台保存AOF文件操作或者AOF文件重写(可以单独百度一下,有的面试官会问这个问题)会产生大量的IO,而一些LINUX OS的fsync调用会被阻塞很长时间(目前还未解决这个问题),这种情况会阻塞另外线程的同步写操作

#

# In order to mitigate this problem it"s possible to use the following option

# that will prevent fsync() from being called in the main process while a

# BGSAVE or BGREWRITEAOF is in progress.

# 为了减轻这个问题带来的影响,可以使用"no-appendfsync-on-rewrite"配置,一旦有BGSAVE和BGREWRITEAOF在执行,阻止fsync函数调用

#

# This means that while another child is saving, the durability of Redis is

# the same as "appendfsync none". In practical terms, this means that it is

# possible to lose up to 30 seconds of log in the worst scenario (with the

# default Linux settings).

# 简单点说就是:当"no-appendfsync-on-rewrit"设置为no,那么有一个进程在执行SAVE操作,AOF持久化模式相当于被设置成了"no",也就是说根据OS的设置,糟糕的情况下可能丢失30秒以上的数据

#

# If you have latency problems turn this to "yes". Otherwise leave it as

# "no" that is the safest pick from the point of view of durability.

# 如果你知道上面说的潜在风险,可以将"no-appendfsync-on-rewrite"设置为yes,否则就不要瞎搞,就保持为no

no-appendfsync-on-rewrite no

# AOF文件重写是Redis面试的一个点,也是优化Redis的一个点,将它设置得足够大,可以保存更多日志数据

# Automatic rewrite of the append only file.

# Redis is able to automatically rewrite the log file implicitly calling

# BGREWRITEAOF when the AOF log size grows by the specified percentage.

# 当AOF文件大小超过指定值"auto-aof-rewrite-min-size",就会发生AOF文件重写

#

# This is how it works: Redis remembers the size of the AOF file after the

# latest rewrite (if no rewrite has happened since the restart, the size of

# the AOF at startup is used).

# Redis会记住AOF重写后的AOF文件大小,如果重启后还未发生重写,那么记住的就是刚开始加载AOF文件的大小

# 这个文件大小值会与下面的配置项值进行比较,决定什么时候做AOF文件重写

#

# This base size is compared to the current size. If the current size is

# bigger than the specified percentage, the rewrite is triggered. Also

# you need to specify a minimal size for the AOF file to be rewritten, this

# is useful to avoid rewriting the AOF file even if the percentage increase

# is reached but it is still pretty small.

# 如果当"当前大小/最后一次重写大小"的比值大于"auto-aof-rewrite-percentage"指定的值,则会触发AOF重写

# 为了避免AOF已经很小还进行AOF重写的尴尬情况,因此需要设置一个AOF重写最小AOF文件大小

# 比如"auto-aof-rewrite-min-size"设置为64M,只有当AOF文件超过64M,且"当前大小/最后一次重写大小">"auto-aof-rewrite-percentage"才会触发AOF重写

#

# Specify a percentage of zero in order to disable the automatic AOF

# rewrite feature.

# 如果将"auto-aof-rewrite-percentage"设置为0,表示不允许执行自动AOF重写

auto-aof-rewrite-percentage 100

auto-aof-rewrite-min-size 64mb

# An AOF file may be found to be truncated at the end during the Redis

# startup process, when the AOF data gets loaded back into memory.

# This may happen when the system where Redis is running

# crashes, especially when an ext4 filesystem is mounted without the

# data=ordered option (however this can"t happen when Redis itself

# crashes or aborts but the operating system still works correctly).

# 如果运行Redis的OS崩溃掉,特别是ext4格式的文件系统使用"data=ordered"选项执行mount操作,在这些情况下

# AOF文件可能是截断(损坏)的,重启Redis时如果"aof-load-truncated"被设置为yes,那么AOF文件在加载时可能会丢失掉崩溃前的一些数据

#

# Redis can either exit with an error when this happens, or load as much

# data as possible (the default now) and start if the AOF file is found

# to be truncated at the end. The following option controls this behavior.

# 针对损坏的AOF文件,在重启Redis的时候,支持两种方式

# 1.发现文件损坏,直接报错

# 2.尽可能的从找到的截断(损坏)文件中恢复数据到内存中,这是Redis的默认方式

#

# If aof-load-truncated is set to yes, a truncated AOF file is loaded and

# the Redis server starts emitting a log to inform the user of the event.

# 如果"aof-load-truncated"被设置为yes,且发现了被截断的AOF文件,那么在启动Redis时日志或者控制台中会输出日志,让运维人员或者监控看到这条信息

# Otherwise if the option is set to no, the server aborts with an error

# and refuses to start. When the option is set to no, the user requires

# to fix the AOF file using the "redis-check-aof" utility before to restart

# the server.

# 如果将"aof-load-truncated"设置no,且发现了被截断的AOF文件,重启Redis会报错,这个时候就需要借用redis-check-aof工具修复AOF文件

# 其实在主从模式下,是否可以到从节点拿AOF文件进行恢复,好像这个方法是多想了,因为哨兵、Codis、Cluster模式会自动进行故障转移,只有单机和纯主从模式也许这种方式可以尝试,但是现在的企业至少应该是哨兵模式了,大企业都用Cluster了或者豌豆荚搞的Codis

#

# Note that if the AOF file will be found to be corrupted in the middle

# the server will still exit with an error. This option only applies when

# Redis will try to read more data from the AOF file but not enough bytes

# will be found.

# 如果AOF文件在文件中间损坏了,即使"aof-load-truncated"设置为yes,重启Redis一样会报错且退出启动

# 这个选项只适合AOF被截断的情况,也就是AOF没有足够的字节

aof-load-truncated yes

# 混合持久化,Redis 4提供的新功能

# When rewriting the AOF file, Redis is able to use an RDB preamble in the

# AOF file for faster rewrites and recoveries. When this option is turned

# on the rewritten AOF file is composed of two different stanzas:

# 如果"aof-use-rdb-preamble"设置为yes,那么AOF文件由"rdb file"+"aof tail"两部分组成,这种组合方式可以发挥RDB持久化加载速度快和压缩存储使用空间小的优势,与AOF持久化丢失数据小于1S的优势

#

# [RDB file][AOF tail]

#

# When loading Redis recognizes that the AOF file starts with the "REDIS"

# string and loads the prefixed RDB file, and continues loading the AOF

# tail.

# 该混合持久化方式下的AOF文件用"REDIS"字符串区分,前面是RDB内容,后面是AOF内容

aof-use-rdb-preamble yes

 

以上是 Redis5.0.3配置文件详解(易读白话翻译)惰性回收与AOF持久化 的全部内容, 来源链接: utcz.com/z/513254.html

回到顶部