面试总结(三)-redis

redis

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

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

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

  • 保存文件时,rdb文件保存会阻塞主线程;解决方法:rdbbackgroud 方法后台运行
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文件到内存中,载入过程中,不支持其他请求,下面的这段代码值得好好看一下
/* 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>'

################################ 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,这个文件能够提供更好的持久性保障。

原文:
############################## 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 如有侵权请联系本人
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,064评论 5 466
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,606评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,011评论 0 328
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,550评论 1 269
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,465评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 47,919评论 1 275
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,428评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,075评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,208评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,185评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,191评论 1 328
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,914评论 3 316
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,482评论 3 302
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,585评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,825评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,194评论 2 344
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,703评论 2 339

推荐阅读更多精彩内容