面试总结(三)-redis

Author Avatar
杨小强 1月 21, 2018
  • 在其它设备中阅读本文章

redis

写在前面:要想深入了解redis,《redis设计与实现(第二版)》这本书是很不错的选择;关于redis的底层存储结构会在下一篇文章中写,本章切掉这部分内容了。

一、redis 持久化方案(RDB、AOF)

1、RDB(默认持久化方案)
服务运行时将内存文件保存到.rdb 文件中,重启时:读取数据存储文件.rdb,加载到内存中,还原DB。

  • 保存文件时,rdb文件保存会阻塞主线程;解决方法:rdbbackgroud 方法后台运行
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
int rdbSaveBackground(char *filename, rdbSaveInfo *rsi) {
pid_t childpid;
long long start;

if (server.aof_child_pid != -1 || server.rdb_child_pid != -1) return C_ERR;
if ((childpid = fork()) == 0) {
int retval;

/* Child */
closeListeningSockets(0);
redisSetProcTitle("redis-rdb-bgsave");
retval = rdbSave(filename,rsi);
.............省略一部分
/* Parent */
server.stat_fork_time = ustime()-start;
server.stat_fork_rate = (double) zmalloc_used_memory() * 1000000 / server.stat_fork_time / (1024*1024*1024); /* GB per second. */
latencyAddSampleIfNeeded("fork",server.stat_fork_time/1000);
if (childpid == -1) {
closeChildInfoPipe();
server.lastbgsave_status = C_ERR;
serverLog(LL_WARNING,"Can't save in background: fork: %s",
strerror(errno));
return C_ERR;
}
serverLog(LL_NOTICE,"Background saving started by pid %d",childpid);
server.rdb_save_time_start = time(NULL);
server.rdb_child_pid = childpid;
server.rdb_child_type = RDB_CHILD_TYPE_DISK;
updateDictResizePolicy();
return C_OK;
}
return C_OK; /* unreached */
}
  • 启动时会读取rdb文件到内存中,载入过程中,不支持其他请求,下面的这段代码值得好好看一下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/* Load an RDB file from the rio stream 'rdb'. On success C_OK is returned,
* otherwise C_ERR is returned and 'errno' is set accordingly. */
int rdbLoadRio(rio *rdb, rdbSaveInfo *rsi) {

rdb->update_cksum = rdbLoadProgressCallback;
rdb->max_processing_chunk = server.loading_process_events_interval_bytes;

while(1) {
。。。 省略一部分代码
/* Read type. */
if ((type = rdbLoadType(rdb)) == -1) goto eoferr;

/* Handle special types. */
if (type == RDB_OPCODE_EXPIRETIME) {
/* EXPIRETIME: load an expire associated with the next key
* to load. Note that after loading an expire we need to
* load the actual type, and continue. */
if ((expiretime = rdbLoadTime(rdb)) == -1) goto eoferr;
/* We read the time so we need to read the object type again. */
if ((type = rdbLoadType(rdb)) == -1) goto eoferr;
/* the EXPIRETIME opcode specifies time in seconds, so convert
* into milliseconds. */
expiretime *= 1000;
} else if (type == RDB_OPCODE_EXPIRETIME_MS) {
/* EXPIRETIME_MS: milliseconds precision expire times introduced
* with RDB v3. Like EXPIRETIME but no with more precision. */
if ((expiretime = rdbLoadMillisecondTime(rdb)) == -1) goto eoferr;
/* We read the time so we need to read the object type again. */
if ((type = rdbLoadType(rdb)) == -1) goto eoferr;
} else if (type == RDB_OPCODE_EOF) {
/* EOF: End of file, exit the main loop. */
break;
}
decrRefCount(auxkey);
decrRefCount(auxval);
continue; /* Read type again. */
}
。。。 省略一部分代码
}

rdb 文件保存时间点,在redis.conf中进行配置 ‘ save <seconds> <changes>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
################################ 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秒保存10000个key

2、AOF
aof是一个协议文本方式的存储方案,对数据库的操作命令记录到aof文件中,达到记录记录数据的目的(有点类似MySQL的bin log)
写入流程:

  • 1、将请求命令转换为网络协议文本方式
  • 2、将协议文本内容写到server.aof_buf
  • 3、达到某种条件的时候,讲aof_buf写入到磁盘中
    • AOF_FSYNC_NO 不保存只会追加到cof_buf 文件中,在缓存写满的情况下,会保存;正常关闭redis,会保存;AOF功能关闭,会保存。
    • AOF_FSYNC_ALWAYS 每次执行一条命令都会进行一次保存操作
    • AOF_FSYNC_EVERYSEC 每秒保存一次,默认配置后台执行,不会阻塞住进程。

上面的常量,也是在redis.conf 中配置aof的参数。 默认appendfsync no

aof的出现是为了解决什么问题?官方说明:rds 这种方案本来已经是一种很好的方案了,能够适应绝大多数的情况,但是在redis进程或电源发生故障的情况下,可能会造成小部份的数据丢失,这取决于配置的保存时间点;
Appendonly是一种能够提供非常好的持久化的模式,例如使用默认的Fsync方案,redis能在发生服务器电源故障或操作系统仍然正常运行但redis进程莫名挂掉的情况下,只丢失1秒的数据。 aof与rdb模式可以同时启用,这并不冲突。如果aof是可用的,那redis启动时将自动加载aof,这个文件能够提供更好的持久性保障。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
原文:
############################## 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.
  • AOF保存数据的还原:讲协议文本转换为命令执行,从而还原数据。
AOF VS RDB
  • AOF 文件通常会大于RDB文件
  • AOF 的效率取决于配置策略
  • RDB 设置过大,操作耗时过多,可能会出现短时间无法响应的情况;设置过小,也会有IO瓶颈

二、redis 常用数据结构

  • string: key value 服务;应用:一般用于缓存
  • set: 集合去重,集合的交、并、差操作;应用: 可以用作共同、二度好友的计算
  • sortset: 带有排序的set集合; 应用:取Top10、消息队列、设置权重
  • hash: 结构化数据的存储、通常用于对象属性的更新;(结构化数据存储建议使用hash,效率更高)
  • list: 链表 ,pop push,也可以用作队列
  • pub/sub: 消息系统,key的发布和订阅(也可以用作定时任务)

三、redis的启动流程分析

任何一个组件,包括redis,MySQL或者说servlet的启动过程都是有着很多的相似之处,对比学习是一个很好的学习方法;

redis 启动过程包括两个主要操作:初始化服务器配置、初始化功能模块
初始化服务器配置,主要做以下事情

  • 初始化server变量、设置redis默认值
  • 读取配置文件,接受参数,替换服务器默认配置
  • 初始化功能模块
    • 1 注册信号事件
    • 2 初始化客户端连接
    • 3 初始共享对象
    • 4 监测最大连接数
    • 5 初始化DB
    • 6 初始化network
    • 7 初始化服务器实时统计
    • 8 初始化后台后台计划任务
    • 9 初始化lua脚本
    • 10 初始化慢查询日志
    • 11 初始化后台线程任务系统
  • 从RDB或者AOF重载数据(磁盘数据加载内存)
  • 网络监听服务启动前的准备操作
  • 开启事件监听、开始接受客户端请求

下面是转的一个流程图,整个过程表达的很清晰
图片转自http://processon.com/chart_image/id/58441b02e4b0e742e493404c.png 如有侵权请联系本人