本文转载自https://www.cnblogs.com/itdragon/p/7906481.html
Redis中数据存储模式有2种:cache-only,persistence
一下介绍的都是persistence的相关设置
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
文件名
# The filename where to dump the DB
dbfilename dump.rdb
# 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
默认开启数据压缩(LZF压缩方式,占用一点CPU时间。但节省空间很有效)
# 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 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 ./
如何通过RDB文件恢复数据?
将dump.rdb 文件拷贝到redis的安装目录的bin目录下,重启redis服务
优点:
√大规模数据恢复
√对数据完整性与一致性要求不高
缺点:
×RDB最后一次备份可能宕机,完整性与一致性
×备份占内存。
1. redis备份独立创建一个子进程,数据写到临时文件(临时文件也在内存中。太占空间了。。。。)
2. 临时文件完成后,题换之前的备份文件
AOF相关设置
手动开启,弥补RDB的数据不一致性。
记录形式:日志(记录写操作,追加至文件)
开启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
# 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
重写触发条件。AOF文件大小是上次rewirte后大小一倍,且文件大小大于64M.
实现:fork出一条新进程,读取内存数据,重新写道一个临时文件中。最后替换旧文件。没有与旧文件进行交互
# 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
如何通过AOF文件恢复数据?
appendonly.aof 文件拷贝到redis的安装目录的bin目录下,重启redis服务
意外情况:
AOF文件格式异常,需修复
redis-check-aof --fix appendonly.aof
优点:
数据完整性与一致性高,但不能保证完全一致(毕竟也有异步处理或突发情况,只是异步处理的粒度变细了)
缺点:
AOF记录内容多时,数据恢复慢(因为相当于把之前的命令有执行了一遍)
RDB | AOF | |
存储数据 | 保存键空间的所有键值对(包括过期字典中的数据),并以二进制形式保存,符合rdb文件规范,根据不同数据类型会有不同处理 | 保存redis服务器所执行的所有写命令来记录数据库状态,在写入之前命令存储在aof_buf缓冲区。 |
持久化时间选择 | 通过conf的save选项设置持久化行为(单位时间内的修改次数)。 | 通过conf的appendfsync选项设置持久化行为 |
数据还原 | 服务器载入rdb文件,阻塞线程,在载入完成之前不接受任何命令。 | 服务器创建不带网络连接的伪客户端,读取aof文件中的所有命令并执行(redis服务开启aof持久化在服务器启动时会选择aof文件恢复数据库状态) 有时AOF文件出错可以使用rdis-check恢复 |
过期键 | 在写入或读取时会忽略过期键 | 不会忽略过期键,在键被惰性删除或定期删除时向aof文件追加一条删除命令 |
文件大小 | 随着存储数据量的变化而变化(根据不同数据类型有不同的数据压缩优化) | 随着执行命令的增加而增加(通过aof重写进行优化) |
执行命令 | SAVE和BGSAVE | --
|