# 春招必问的redis持久化(RDB AOF)，你能答上来么？

2019/04/10 10:10

春招面试模拟，如同雷同，纯属巧合！！！

面试的大体流程：

第一步：一般会有笔试题，也可能没有。有笔试题就要好好答了，因为会重视笔试结果，为了节约面试官时间，HR可能先会看，不合格直接让你走人了。

第二步：开始面试，面试官会让你先来个自我介绍，他在看你的简历。可能有人会有疑问？为什么简历上都写的很清楚还让我做自我介绍？因为面试官不会提前看你的简历，他要有时间去看简历。所以自我介绍，不宜过长或过短，要重点突出，怎么重点突出？提前看好岗位要求，要求都是入职之后用到的技术，所以面试官会看重那些！！！自我介绍结束，步入正题。

面试官会问：之前的公司用过redis么？

面试者有两类回答：

面试者甲：没用过，但自己学过，下载过源码，自己部署安装过，基本的命令像string/hash/lsit/set/zset,都熟悉；

面试官(心想虽然没用，但动手能力很强，也很好学，不错)：redis是内存数据库，那它怎么进行持久化的？

面试官甲（心想没注意看啊，不知道啊）：...

面试者乙：用过，比较熟悉

面试官会接着问：持久化方式有哪些？

面试者乙：RDB和AOF

面试官：原理？区别？优缺点说一下吧？

面试官乙：...

要是是你去面试，你能回答上来么？

下面让我为你一一揭晓答案！！！

## RDB持久化(Redis DataBase)

redis是内存数据库，一旦服务器进程退出，服务器中的数据库状态也会消失不见。重点介绍save和bgsave命令。

### RDB文件的创建与载入

save命令会阻塞Redis服务器进程，直到RDB文件创建为止，在服务器进程阻塞期间，服务器不能处理任何命令请求。

bgsave命令派生出子进程，，然后由子进程创建RDB文件，父进程继续处理请求。

### 自动间隔性保存

redis服务器会通过用户配置save选项，每隔一段时间去执行一下bgsave命令；默认的配置文件redis.conf，关于rdb部分的配置如下：

 1 ################################ SNAPSHOTTING  ################################
2 #
3 # Save the DB on disk:
4 #
5 #   save <seconds> <changes>
6 #
7 #   Will save the DB if both the given number of seconds and the given
8 #   number of write operations against the DB occurred.
9 #
10 #   In the example below the behaviour will be to save:
11 #   after 900 sec (15 min) if at least 1 key changed
12 #   after 300 sec (5 min) if at least 10 keys changed
13 #   after 60 sec if at least 10000 keys changed
14 #
15 #   Note: you can disable saving completely by commenting out all "save" lines.
16 #
17 #   It is also possible to remove all the previously configured save
18 #   points by adding a save directive with a single empty string argument
19 #   like in the following example:
20 #
21 #   save ""
22
23 save 900 1
24 save 300 10
25 save 60 10000
26
27 # By default Redis will stop accepting writes if RDB snapshots are enabled
28 # (at least one save point) and the latest background save failed.
29 # This will make the user aware (in a hard way) that data is not persisting
30 # on disk properly, otherwise chances are that no one will notice and some
31 # disaster will happen.
32 #
33 # If the background saving process will start working again Redis will
34 # automatically allow writes again.
35 #
36 # However if you have setup your proper monitoring of the Redis server
37 # and persistence, you may want to disable this feature so that Redis will
38 # continue to work as usual even if there are problems with disk,
39 # permissions, and so forth.
40 stop-writes-on-bgsave-error yes
41
42 # Compress string objects using LZF when dump .rdb databases?
43 # For default that's set to 'yes' as it's almost always a win.
44 # If you want to save some CPU in the saving child set it to 'no' but
45 # the dataset will likely be bigger if you have compressible values or keys.
46 rdbcompression yes
47
48 # Since version 5 of RDB a CRC64 checksum is placed at the end of the file.
49 # This makes the format more resistant to corruption but there is a performance
50 # hit to pay (around 10%) when saving and loading RDB files, so you can disable it
51 # for maximum performances.
52 #
53 # RDB files created with checksum disabled have a checksum of zero that will
55 rdbchecksum yes
56
57 # The filename where to dump the DB
58 dbfilename dump.rdb
59
60 # The working directory.
61 #
62 # The DB will be written inside this directory, with the filename specified
63 # above using the 'dbfilename' configuration directive.
64 #
65 # The Append Only File will also be created inside this directory.
66 #
67 # Note that you must specify a directory here, not a file name.
68 dir ./
rdb

主要有三条策略，满足任意一个，就会执行bgave命令：

服务器在900秒之内，对数据库至少修改了1次

服务器在300秒之内，对数据库至少修改了10次

服务器在60秒之内，对数据库至少修改了10000次

优点：

适合大规模的数据恢复

对数据完整性和一致性要求不高

缺点：

会丢失最后一次修改的数据

fork会产生额外消耗

## AOF持久化（Append Only File）

与RDB通过键值对来记录数据库状态不同，AOF是通过Redis服务器所执行的写命令来记录数据库状态的。

### AOF持久化的实现

AOF持久化功能的实现可以分为命令追加(append)、文件写入、文件同步(sync)三个步骤。

aof在redis.conf配置文件的：

  1 ############################## APPEND ONLY MODE ###############################
2
3 # By default Redis asynchronously dumps the dataset on disk. This mode is
4 # good enough in many applications, but an issue with the Redis process or
5 # a power outage may result into a few minutes of writes lost (depending on
6 # the configured save points).
7 #
8 # The Append Only File is an alternative persistence mode that provides
9 # much better durability. For instance using the default data fsync policy
10 # (see later in the config file) Redis can lose just one second of writes in a
11 # dramatic event like a server power outage, or a single write if something
12 # wrong with the Redis process itself happens, but the operating system is
13 # still running correctly.
14 #
15 # AOF and RDB persistence can be enabled at the same time without problems.
16 # If the AOF is enabled on startup Redis will load the AOF, that is the file
17 # with the better durability guarantees.
18 #
20
21 appendonly no
22
23 # The name of the append only file (default: "appendonly.aof")
24
25 appendfilename "appendonly.aof"
26
27 # The fsync() call tells the Operating System to actually write data on disk
28 # instead of waiting for more data in the output buffer. Some OS will really flush
29 # data on disk, some other OS will just try to do it ASAP.
30 #
31 # Redis supports three different modes:
32 #
33 # no: don't fsync, just let the OS flush the data when it wants. Faster.
34 # always: fsync after every write to the append only log. Slow, Safest.
35 # everysec: fsync only one time every second. Compromise.
36 #
37 # The default is "everysec", as that's usually the right compromise between
38 # speed and data safety. It's up to you to understand if you can relax this to
39 # "no" that will let the operating system flush the output buffer when
40 # it wants, for better performances (but if you can live with the idea of
41 # some data loss consider the default persistence mode that's snapshotting),
42 # or on the contrary, use "always" that's very slow but a bit safer than
43 # everysec.
44
45 # More details please check the following article:
46 # http://antirez.com/post/redis-persistence-demystified.html
47 #
48 # If unsure, use "everysec".
49
50 # appendfsync always
51 appendfsync everysec
52 # appendfsync no
53
54 # When the AOF fsync policy is set to always or everysec, and a background
55 # saving process (a background save or AOF log background rewriting) is
56 # performing a lot of I/O against the disk, in some Linux configurations
57 # Redis may block too long on the fsync() call. Note that there is no fix for
58 # this currently, as even performing fsync in a different thread will block
59 # our synchronous write(2) call.
60 #
61 # In order to mitigate this problem it's possible to use the following option
62 # that will prevent fsync() from being called in the main process while a
63 # BGSAVE or BGREWRITEAOF is in progress.
64 #
65 # This means that while another child is saving, the durability of Redis is
66 # the same as "appendfsync none". In practical terms, this means that it is
67 # possible to lose up to 30 seconds of log in the worst scenario (with the
68 # default Linux settings).
69 #
70 # If you have latency problems turn this to "yes". Otherwise leave it as
71 # "no" that is the safest pick from the point of view of durability.
72
73 no-appendfsync-on-rewrite no
74
75 # Automatic rewrite of the append only file.
76 # Redis is able to automatically rewrite the log file implicitly calling
77 # BGREWRITEAOF when the AOF log size grows by the specified percentage.
78 #
79 # This is how it works: Redis remembers the size of the AOF file after the
80 # latest rewrite (if no rewrite has happened since the restart, the size of
81 # the AOF at startup is used).
82 #
83 # This base size is compared to the current size. If the current size is
84 # bigger than the specified percentage, the rewrite is triggered. Also
85 # you need to specify a minimal size for the AOF file to be rewritten, this
86 # is useful to avoid rewriting the AOF file even if the percentage increase
87
88 # Specify a percentage of zero in order to disable the automatic AOF
89 # rewrite feature.
90
91 auto-aof-rewrite-percentage 100
92 auto-aof-rewrite-min-size 64mb
93
94 # An AOF file may be found to be truncated at the end during the Redis
95 # startup process, when the AOF data gets loaded back into memory.
96 # This may happen when the system where Redis is running
97 # crashes, especially when an ext4 filesystem is mounted without the
98 # data=ordered option (however this can't happen when Redis itself
99 # crashes or aborts but the operating system still works correctly).
100 #
101 # Redis can either exit with an error when this happens, or load as much
102 # data as possible (the default now) and start if the AOF file is found
103 # to be truncated at the end. The following option controls this behavior.
104 #
105 # If aof-load-truncated is set to yes, a truncated AOF file is loaded and
106 # the Redis server starts emitting a log to inform the user of the event.
107 # Otherwise if the option is set to no, the server aborts with an error
108 # and refuses to start. When the option is set to no, the user requires
109 # to fix the AOF file using the "redis-check-aof" utility before to restart
110 # the server.
111 #
112 # Note that if the AOF file will be found to be corrupted in the middle
113 # the server will still exit with an error. This option only applies when
114 # Redis will try to read more data from the AOF file but not enough bytes
115 # will be found.
116 aof-load-truncated yes
aof

### AOF重写

随着服务器时间的流逝，文件的体积越来越大，体积过大的AOF文件对redis服务器、甚至整个宿主计算机造成影响。并且AOF文件的体积越大，使用AOF文件进行数据还原所需的时间越多。

为了解决AOF文件体重膨胀的问题，redis提供了AOF文件重写(rewrite)的功能。

#### 触发机制

redis会记录上次重写时AOF的大小，默认配置是当AOF文件大小是上次rewrite后大小的一倍且大于64M；默认配置如下：

auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb

悄悄告诉你们个小秘密：可能在面试时，面试官会吹嘘公司很牛，redis用的出神入化，当你入职之后，可以悄悄看看“auto-aof-rewrite-min-size 64mb”，默认64M大小，根本不够用，告诉发展的公司起码要3G起。

### 优缺点

优点：

配置灵活，可以选择多种方式进行持久化

缺点：

相同数据集的数据而言，aof文件要远大于rdb文件，恢复速度慢于rdb

0
0 收藏

0 评论
0 收藏
0