0040-redis持久化策略

本文深入探讨了Redis的两种持久化方式:RDB快照和AOF日志。RDB通过生成dump.rdb文件实现数据持久化,适用于数据恢复场景;AOF则采用日志方式记录写操作,提供更高级别的数据持久性和安全性。文章详细介绍了两种方式的配置参数及优缺点。

1. RDB(Redis DataBase)

在指定的时间内,操作指定的次数redis会生成一个dump.rdb文件,生成文件位置是./跟redis.conf相同目录。

################################ SNAPSHOTTING  ################################
#
# Save the DB on disk:
#
#   save <seconds> <changes>
#
#   Will save the DB if both the given number of seconds and the given
#   number of write operations against the DB occurred.
#
#   In the example below the behaviour will be to save:
#   after 900 sec (15 min) if at least 1 key changed
#   after 300 sec (5 min) if at least 10 keys changed
#   after 60 sec if at least 10000 keys changed
#
#   Note: you can disable saving completely by commenting out all "save" lines.
#
#   It is also possible to remove all the previously configured save
#   points by adding a save directive with a single empty string argument
#   like in the following example:
#
#   save ""

save 900 1
save 300 10
save 60 10000

# By default Redis will stop accepting writes if RDB snapshots are enabled
# (at least one save point) and the latest background save failed.
# This will make the user aware (in a hard way) that data is not persisting
# on disk properly, otherwise chances are that no one will notice and some
# disaster will happen.
#
# If the background saving process will start working again Redis will
# automatically allow writes again.
#
# However if you have setup your proper monitoring of the Redis server
# and persistence, you may want to disable this feature so that Redis will
# continue to work as usual even if there are problems with disk,
# permissions, and so forth.
stop-writes-on-bgsave-error yes

# Compress string objects using LZF when dump .rdb databases?
# For default that's set to 'yes' as it's almost always a win.
# If you want to save some CPU in the saving child set it to 'no' but
# the dataset will likely be bigger if you have compressible values or keys.
rdbcompression yes

# Since version 5 of RDB a CRC64 checksum is placed at the end of the file.
# This makes the format more resistant to corruption but there is a performance
# hit to pay (around 10%) when saving and loading RDB files, so you can disable it
# for maximum performances.
#
# RDB files created with checksum disabled have a checksum of zero that will
# tell the loading code to skip the check.
rdbchecksum yes

# The filename where to dump the DB
dbfilename dump.rdb

# The working directory.
#
# The DB will be written inside this directory, with the filename specified
# above using the 'dbfilename' configuration directive.
#
# The Append Only File will also be created inside this directory.
#
# Note that you must specify a directory here, not a file name.
dir ./

2. AOF(Append only File)

AOF默认不开启,用日志的方式记录每个写操作,文件转存比rdb处理的更好,但是性能不如rdb,适合不能丢失数据的情况配置

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

appendonly no

# The name of the append only file (default: "appendonly.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 consider the default persistence mode that's snapshotting),
# or on the contrary, use "always" that's very slow but a bit safer than
# everysec.
#
# More details please check the following article:
# http://antirez.com/post/redis-persistence-demystified.html
#
# If unsure, use "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.
#
# 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.
#
# 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).
#
# 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 no

# 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.
#
# 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).
#
# 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.
#
# Specify a percentage of zero in order to disable the automatic AOF
# rewrite feature.

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 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.
#
# 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.
# 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.
#
# 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-load-truncated yes

# 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:
#
#   [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-use-rdb-preamble yes

开启aof

appendonly yes

配置文件同步策略

# 每次发生数据变化立马写入磁盘
# appendfsync always
# 每秒异步同步一次,默认配置
appendfsync everysec
# 不同步
# appendfsync no
<think>好的,我现在要处理用户关于Redis持久化策略的面试问题及答案的需求。首先,我需要确认用户的具体需求是什么。用户提到他们想了解Redis持久化相关的常见面试问题及解答,并且已经提供了一些引用资料。我需要根据这些引用内容来组织答案,同时遵循用户给出的系统级指令。 首先,我需要回顾Redis持久化机制。Redis主要有两种持久化方式:RDBRedis Database)和AOFAppend Only File),以及它们的混合模式。根据引用[1],混合持久化结合了RDBAOF的优点。引用[2]和[3]提到了RDB的配置方式,包括save指令的作用和如何禁用RDB。此外,用户提供的引用中还提到了集群模式下的问题,但当前问题主要关注持久化策略,所以可能暂时不需要涉及集群相关内容,除非问题涉及到分布式环境下的持久化。 接下来,我需要整理常见的面试问题。通常,面试官会问两者的区别、优缺点、配置方式、数据恢复过程、混合持久化等。根据引用内容,混合持久化是一个重点,因为它结合了RDB的快照和AOF的增量日志。此外,如何配置RDB和禁用RDB也是关键点,如引用[2]和[3]所述。 在组织答案时,需要确保每个问题结构清晰,回答准确,并且正确引用提供的资料。例如,在回答RDBAOF的区别时,需要指出RDB是快照,而AOF是日志追加,同时引用[1]和[3]来说明它们的优缺点。混合持久化的部分需要引用[1]详细说明其工作原理和优势。 同时,要遵循用户给出的格式要求:行内公式用$...$,独立公式用$$...$$,但这里可能涉及的技术内容较少,可能不需要数学公式,但需要注意代码块的格式。例如,如果提到配置文件中的设置,可能需要用代码块展示,如引用[2]中的save指令示例。 另外,用户要求生成相关问题,所以需要根据内容提出几个相关问题,如RDBAOF的区别、如何选择持久化方式、混合持久化的实现等。 最后,确保所有引用都正确标注,例如在提到禁用RDB时引用[3],混合持久化引用[1],配置RDB引用[2]。需要检查每个引用是否准确对应到相关内容,避免错误引用。 现在需要将以上思路转化为结构化的回答,包含问题与解答,以及相关问题部分,同时遵守格式要求,使用中文,确保LaTeX语法正确,并正确添加引用标识。</think>### Redis 持久化策略面试常见问题与解答 --- #### 1. Redis 有哪些持久化方式?它们的核心区别是什么? Redis 提供两种持久化方式:**RDBRedis Database)** 和 **AOFAppend Only File)**。 - **RDB**:通过生成数据快照(二进制文件)保存某一时刻的全量数据。触发方式包括手动执行`SAVE/BGSAVE`命令或根据配置自动触发(例如 `save 900 1` 表示 900 秒内至少 1 次修改时触发快照)[^2][^3]。 - **AOF**:记录所有写操作命令(文本日志),通过重放日志恢复数据。支持不同的同步频率(如每秒同步或每次操作同步)。 **核心区别**: - RDB 是紧凑的二进制文件,恢复速度快,但可能丢失最后一次快照后的数据。 - AOF 提供更高的数据安全性,但文件体积较大且恢复速度较慢[^1][^3]。 --- #### 2. 如何配置 RDB 持久化?如何禁用 RDB? 在 Redis 配置文件(`redis.conf`)中: ```python # 示例配置:900秒内至少1次修改触发快照 save 900 1 save 300 10 save 60 10000 ``` - **禁用 RDB**:注释所有 `save` 指令并重启 Redis 服务[^2]。 --- #### 3. 什么是混合持久化?它如何结合 RDBAOF 的优势? **混合持久化**(Redis 4.0+ 支持)将 RDB 快照与 AOF 增量日志合并: 1. 定期生成 RDB 快照作为全量数据基准。 2. 后续写操作以 AOF 格式追加到日志。 **优点**: - 快速恢复(RDB 基础) + 数据完整性(AOF 增量)[^1]。 **启用方式**: ```python aof-use-rdb-preamble yes ``` --- #### 4. 数据恢复时 Redis 如何选择持久化文件? Redis 重启时按以下优先级恢复数据: 1. 如果启用了 AOF,优先加载 AOF 文件(包含更完整的数据)。 2. 若未启用 AOF,则加载 RDB 文件。 **注意**:混合持久化模式下,AOF 文件包含 RDB 头部和后续操作日志,恢复时先加载 RDB 部分再重放 AOF 命令[^1]。 --- #### 5. AOF 重写(Rewrite)的作用是什么? AOF 文件体积过大时,Redis 会通过重写机制生成更紧凑的新 AOF 文件: - 删除冗余命令(如多次修改同一键值的操作)。 - 基于当前数据状态生成等效的最小命令集。 **触发方式**:手动执行 `BGREWRITEAOF` 或根据配置自动触发(例如 `auto-aof-rewrite-percentage 100`)。 --- ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值