Redis从入门到放弃系列(六) 持久化

本文深入探讨Redis的RDB和AOF两种持久化机制。RDB通过快照方式保存内存数据,而AOF则记录每次写操作,提供更高的数据安全性。文章详细解释了触发条件、配置参数及混合持久化策略。

Redis从入门到放弃系列(六) 持久化

本文例子基于:5.0.4 Redis持久化机制包括两种,RDB和AOF,当两种机制都存在的时候,redis启动时会采用aof来恢复数据

Redis为了保证数据的持久性,提供了RDB跟AOF机制,RDB是内存数据的二进制序列化形式,在存储上非常紧凑,AOF日志记录的是数据修改的指令记录文本。

RDB

RDB机制其具体实现为使用系统的多进程写时复制(Copy On Write),写时复制是一种可以推迟甚至避免拷贝数据的技术。内核此时并不复制整个进程的地址空间,而是让父子进程共享同一个地址空间。只用在需要写入的时候才会复制地址空间,从而使各个进行拥有各自的地址空间。

我们知道redis是单线程的,当redis在接受请求的时候还要处理rdb save,如果数据量很大,那么对于业务的影响是致命的,所以redis采用了多进程写时复制技术来解决该问题.父进程首先fork一个子进程,RDB持久化交给了子进程来进行,父进程继续处理客户端的请求.

那么redis在什么时候会触发RDB持久化呢? 我们来看一下redis.conf里面关于RDB持久化的说明:

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

我们可以看到,redis默认有3个措施触发RDB的持久化:

  • 60秒有10000个key改变
  • 300秒有10个key改变
  • 900秒有1个key改变 当以上任意条件满足则触发RDB持久化机制.如果不需要RDB持久化,可以把该配置注释掉.RDB最严重的情况下会存在redis占据原先数据内存的2倍大小。 RDB默认启用LZF压缩字符串对象,以节省储存空间。

AOF

让我们先来了解一下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.

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日志记录了服务器接收到的每一次写入操作,这些操作会在redis服务器启动的时候重新执行一遍,重构原始数据集。通过只能追加的方式,使用与redis协议相同的格式记录。当aof日志文件过大的时候,会触发aof-rewriete。

触发aof持久化有三种策略:

  • appendfsync always(当有写入操作就触发一次持久化)
  • appendfsync everysec(每秒持久化一次,生产使用)
  • appendfsync no(不进行同步,让操作系统决定在需要的时候将数据持久化)

当aof日志文件过大时会在后台自动重写它。aof-rewrite是安全的。首先redis会开启一个子进程对内存进行遍历转换成对应的redis的操作指令,将对应的操作指令序列化到一个新的aof日志文件中。当序列化完成之后,redis会将序列化期间产生的aof变化同步到该aof文件中,当同步完毕之后替代原本旧的aof文件.

触发aof-rewrite配置: - no-appendfsync-on-rewrite(重写时是否可以运行Appendfsync) - auto-aof-rewrite-percentage(当文件大小是上次rewrite后的大小的百分比) - auto-aof-rewrite-min-size(当aof文件不小于该数值)

在redis4.0之后启用了混合持久化. 前一部分是RDB格式,后一部分是AOF,即在持久化的时候,接受客户端请求过来的另外的缓存数据~

aof-use-rdb-preamble yes

转载于:https://my.oschina.net/u/4131421/blog/3057922

### Redis 入门到精通教程 #### 、初识 Redis Redis种开源的键值对存储系统,通常被用作数据库、缓存和消息中间件。它支持多种数据结构如字符串(Strings),散列(Hashes),列表(Lists),集合(Sets)及其有序版本(sorted sets)[^1]。 #### 二、安装与基本操作 对于初次使用者来说,在本地环境中设置个简单的实例是个不错的起点。可以通过官方文档获取不同平台下的具体部署指南。旦完成安装,则可通过命令行工具`redis-cli`来进行交互测试[^2]: ```bash $ redis-cli 127.0.0.1:6379> PING PONG ``` 上述例子展示了如何验证服务器是否正常工作——发送PING指令后收到PONG响应表示切就绪。 #### 三、核心概念理解 - **持久化**:为了防止意外断电等情况造成的数据丢失问题,提供了RDB快照方式以及AOF日志记录两种方案。 - **性能优化**:合理配置内存管理策略(比如最大可用RAM大小),调整淘汰算法以适应实际应用场景需求;另外还可以考虑压缩技术减少磁盘占用空间等措施提升效率。 - **高可用架构设计**:当单机版难以满足业务增长带来的压力时,就需要构建主从复制模式或者更进步采用哨兵机制自动监控故障转移过程中的各个细节确保服务连续性不受影响[^3]。 #### 四、进阶特性探索 随着技能水平提高,可以尝试掌握更多高级特性和最佳实践技巧: - 利用Lua脚本来编写复杂查询逻辑,从而简化应用程序开发流程并增强灵活性; - 掌握模块插件生态体系,例如利用 `RedisModule`, `RediSearch`, 或者 `RedisJSON` 来快速搭建特定领域解决方案; - 学习分布式环境下的集群管理和运维要点,包括但不限于分片原理、迁移策略等方面的知识点。 #### 五、事务处理能力 Redis 支持基于乐观锁思想实现简单形式上的原子性批量更新动作,这主要依赖于MULTI/EXEC组合拳来达成目的。此外还有WATCH语句允许客户端监视某些key的变化情况以便做出相应反应,而DISCARD则用于中途放弃当前正在进行中的多条命令序列执行计划[^4]: ```python # Python伪代码模拟次完整的事务提交全过程 r = redis.Redis() pipe = r.pipeline() try: pipe.multi() # 开启事务状态 pipe.set('foo', 'bar') pipe.incr('counter') results = pipe.execute() # 正式生效之前的所有变更项 except Exception as e: pipe.discard() # 出错情况下回滚至最初未修改的状态 finally: del(pipe) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值