赞
踩
在指定的间隔时间内,将内存中的数据集快照写入磁盘,也就是行话讲的Snapshot快照,它恢复时是将快照文件直接读到内存中
比如每隔10分钟,将现在redis的存储快照写到硬盘中去。
Redis会单独创建(fork)一个子进程来进行持久化,会先将数据写入到一个临时文件中,待持久化过程都结束了,再用这个临时文件替换上次持久化好的文件。整个过程中,主进程是不进行任何IO操作的,这就确保了极高的性能,如果需要进行大规模数据的恢复,且对于数据恢复的完整性不是非常敏感,那RDB方式要比AOF方式更加的高效。
RDB的缺点:最后一持久化后的数据可能丢失。
在redis.conf配置文件中,默认名称为dump.conf
同时可以设置文件保存的位置
# 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的修改就进行保存;
比如如果设置了30秒内10个变化就存储,如果有12个key发生了变化,只会持久化前10个key
################################ 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 # 900秒内一个key变化 save 300 10 # 300秒内10个key变化 save 60 10000 # 60秒内1万个key变化
我们也可以手动执行命令进行持久化写入操作:
stop-writes-on-bgsave-error yes
:当Redis无法写入磁盘的话,直接关掉Redis的写操作rdbcompression yes
:进行rdb保存时,将文件压缩rdbchecksum yes
:在存储快照后,还可以让Redis使用CRC64算法来进行数如图,redis是不能直接操作物理内存的,是有linux系统给每个进程分配一个虚拟内存,redis只能操作虚拟内存,redis会维护一个页表,用来记录虚拟地址与物理地址的映射,所以redis就能访问到物理内存。
上述的fork吃子进程其实是复制的页表,这样子进程也就有了映射关系。
如果写入的时候,主进程对数据进行了写入,这样不就出现了脏数据么?
在fork的过程中,会把全部数据设置为read-only,如果主进程需要进行写操作,会将具体数据拷贝一份出来,用于写入操作。接着,主进程进行读操作时,也是读取这份拷贝的数据。而子进程还是写入read-only的数据。
以日志的形式来记录每个写操作,将Redis执行过的所有写指令记录下来(读操作不记录),只可以追加文件但不可以改写文件,Redis启动之初会读取该文件重新构建数据,换言之,Redis重启的话就是根据日志文件的内容将写指令从前到后执行一次以完成数据的恢复工作
可以配置文件名称,默认为appendonly.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. # 改成yes 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". # always:始终同步,每次Redis的写入都会立刻记入日志 # everysec:每秒同步,每秒记入日志一次,如果宕机,本秒的数据可能丢失 # no:不主动进行同步,把同步时机交给操作系统 # appendfsync always appendfsync everysec # appendfsync no
AOF采用文件追加方式,文件会越来越大,为避免出现此种情况,新增了重写机制,当AOF的大小超过了所定的阙值时,Redis就会启动AOF文件的内容压缩,只保留可以恢复数据的最小指令集,可以使用命令bgrewriteaof
比如set a a
;然后又执行了set a b
;只会记录set a b
。
AOF文件持续增长过大时,会fork出一条新进程来将文件重写(也是也写临时文件最后rename),redis4.0版本后的重写,是指就是把rdb的快照,以二进制的形式附在新aof的头部,作为已有的历史数据,替换掉原来的流水账操作
重写条件:Redis会记录上一次重写时AOF文件的大小,默认配置是当前AOF文件大小是:上次重写时文件大小+文件大小*100%且文件大于64M时触发
例:上次重写大小为size,则当前大小>= size+size*100%(设置值) 且当前大小>=64mb(设置值)的情况下,才会进行重写
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
注意:重写虽然可以节约大量磁盘空间,减少恢复时间。但是每次重写还是有一定的负担的,因为设定redis要满足一定条件才能进行重写
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。