配置示例:

save 900 1

save 300 10

save 60 3600

配置解读:

1) “save 900 1”表示如果900秒内至少1个key发生变化(新增、修改和删除),则重写rdb文件;

2) “save 300 10”表示如果每300秒内至少10个key发生变化(新增、修改和删除),则重写rdb文件;

3) “save 60 3600”表示如果每60秒内至少10000个key发生变化(新增、修改和删除),则重写rdb文件。

作用:

控制什么时候生成rdb文件(快照,也可叫Checkpoint,即检查点)。

进程启动的时候,会将每一行save读进到类型为struct saveparam的数组中。这个没有排序,依在redis.conf中的先后顺序。在检查时,只要满足就不会再检查下一条规则。

配置策略:

如果同时开启了aof,则可考虑将save的参数调大一点,以减少写rdb带来的压力。实际上如果开启了aof,redis在启动时只会读取aof文件,而不会读取rdb文件:

// Function called at startup to load RDB or AOF file in memory.

void loadDataFromDisk(void) {

if (server.aof_state == AOF_ON) {

// 允许空的aof文件,

// 如果读取aof文件出错,则调用exit(1)直接退出进程

if (loadAppendOnlyFile(server.aof_filename) == C_OK)

serverLog(LL_NOTICE,"DB loaded from append only file: %.3f seconds",(float)(ustime()-start)/1000000);

} else {

if (rdbLoad(server.rdb_filename,&rsi) == C_OK) {

}

}

// #define AOF_OFF 0             /* AOF is off */

// #define AOF_ON 1              /* AOF is on */

void loadServerConfigFromString(char *config) {

。。。。。。

} else if (!strcasecmp(argv[0],"appendonly") && argc == 2) {

int yes;

if ((yes = yesnotoi(argv[1])) == -1) {

err = "argument must be 'yes' or 'no'"; goto loaderr;

}

server.aof_state = yes ? AOF_ON : AOF_OFF;

}

。。。。。。

}

int yesnotoi(char *s) {

if (!strcasecmp(s,"yes")) return 1;

else if (!strcasecmp(s,"no")) return 0;

else return -1;

}

调用顺序:

main()/server.c ->

aeMain()/ae.c -> while (!stop) { aeProcessEvents()/ae.c } ->

serverCron()/server.c -> rdbSaveBackground()/server.c

注:

aeProcessEvents可看作是个epoll_wait调用,在Linux上实际正是epoll_wait调用,而在Solaris上则是port_getn调用。

相关源代码:

int serverCron(struct aeEventLoop *eventLoop, long long id, void *clientData) {

。。。。。。

// Check if a background saving or AOF rewrite in progress terminated.

// 如果已有rdb和aof进程,检查进程是否已退出。

// 如果已退出,则会善后处理,否则什么也不做,等待下一次循环时再次检查

if (server.rdb_child_pid != -1 ||

server.aof_child_pid != -1 ||

ldbPendingChildren()) {

int statloc;

pid_t pid;

if ((pid = wait3(&statloc,WNOHANG,NULL)) != 0) {

int exitcode = WEXITSTATUS(statloc);

。。。。。。

}

} else {

// If there is not a background saving/rewrite

// in progress check if we have to save/rewrite now.

// 按在redis.conf中定义的顺序依次遍历每一行配置项

// 最终是否进行写rdb操作(即生成快照文件),不仅由redis.conf

// 中的配置项决定,还要看上一次操作的结果和状态。

for (j = 0; j < server.saveparamslen; j++) {

struct saveparam *sp = server.saveparams+j;

// Save if we reached the given amount of changes,

// the given amount of seconds, and if the latest bgsave was

// successful or if, in case of an error, at least

// CONFIG_BGSAVE_RETRY_DELAY seconds already elapsed.

// CONFIG_BGSAVE_RETRY_DELAY(5): Wait a few secs before trying again.

if (server.dirty >= sp->changes &&

server.unixtime-server.lastsave > sp->seconds &&

(server.unixtime-server.lastbgsave_try>CONFIG_BGSAVE_RETRY_DELAY ||

server.lastbgsave_status == C_OK)) {

serverLog(LL_NOTICE,"%d changes in %d seconds. Saving...",

sp->changes, (int)sp->seconds);

rdbSaveInfo rsi, *rsiptr;

rsiptr = rdbPopulateSaveInfo(&rsi);

rdbSaveBackground(server.rdb_filename,rsiptr);

break; // 遇到一条满足的即结束处理,因为已没有必要判断是否满足下一条配置规则

}

}

}

。。。。。。

}

// rdb.c

int rdbSaveBackground(char *filename, rdbSaveInfo *rsi) {

。。。。。。

server.lastbgsave_try = time(NULL);

。。。。。。

// 创建写rdb的子进程

if ((childpid = fork()) == 0) {

redisSetProcTitle("redis-rdb-bgsave");

retval = rdbSave(filename,rsi);

}

。。。。。。

}

/* Save the DB on disk. Return C_ERR on error, C_OK on success. */

// rdb.c

// rdbSave调用rdbSaveRio将数据写入到rdb文件中

int rdbSave(char *filename, rdbSaveInfo *rsi) {

。。。。。。

// 写rdb文件

if (rdbSaveRio(&rdb,&error,RDB_SAVE_NONE,rsi) == C_ERR) {

errno = error;

goto werr;

}

。。。。。。

serverLog(LL_NOTICE,"DB saved on disk");

server.dirty = 0;

server.lastsave = time(NULL);

server.lastbgsave_status = C_OK;

return C_OK;

}

/* Produces a dump of the database in RDB format sending it to the specified

* Redis I/O channel. On success C_OK is returned, otherwise C_ERR

* is returned and part of the output, or all the output, can be

* missing because of I/O errors.

*

* When the function returns C_ERR and if 'error' is not NULL, the

* integer pointed by 'error' is set to the value of errno just after the I/O

* error. */

int rdbSaveRio(rio *rdb, int *error, int flags, rdbSaveInfo *rsi) {

for (j = 0; j < server.dbnum; j++) {

。。。。。。

// Iterate this DB writing every entry

while((de = dictNext(di)) != NULL) {

。。。。。。

// 将一对对KV写入到rdb文件

if (rdbSaveKeyValuePair(rdb,&key,o,expire) == -1) goto werr;

。。。。。。

}

}

。。。。。。

werr:

if (error) *error = errno;

if (di) dictReleaseIterator(di);

return C_ERR;

}

// 以SADD命令为例,所有写操作,均会修改dirty 的值

void saddCommand(client *c) {

。。。。。。

for (j = 2; j < c->argc; j++) {

if (setTypeAdd(set,c->argv[j]->ptr)) added++;

}

。。。。。。

server.dirty += added;

addReplyLongLong(c,added);

}

redis.conf之save配置项解读的更多相关文章

  1. 30个redis.conf 配置项说明

    redis.conf 配置项说明如下: 1. Redis默认不是以守护进程的方式运行,可以通过该配置项修改,使用yes启用守护进程 daemonize no 2. 当Redis以守护进程方式运行时,R ...

  2. redis.conf 配置项说明

    redis.conf 配置项说明如下: Redis默认不是以守护进程的方式运行,可以通过该配置项修改,使用yes启用守护进程   daemonize no 当Redis以守护进程方式运行时,Redis ...

  3. Redis之配置文件redis.conf

    解读下 redis.conf 配置文件中常用的配置项,为不显得过于臃长,已选择性删除原配置文件中部分注释. # Redis must be started with the file path as ...

  4. 4、解析配置文件 redis.conf、Redis持久化RDB、Redis的主从复制

    1.Units单位 配置大小单位,开头定义了一些基本的度量单位,只支持bytes,不支持bit 对大小写不敏感 2.INCLUDES包含 和我们的Struts2配置文件类似,可以通过includes包 ...

  5. redis配置文件redis.conf说明

    redis.conf 配置项说明如下:1. Redis默认不是以守护进程的方式运行,可以通过该配置项修改,使用yes启用守护进程    daemonize no2. 当Redis以守护进程方式运行时, ...

  6. redis学习之三配置文件redis.conf 的含义

    摘自http://www.runoob.com/redis/redis-conf.html 安装redis之后的第一件事,我就开始配置密码,结果总是不生效,而我居然还没想到原因.今天突然用命令行设置了 ...

  7. redis的redis.conf文件详解

    常用的: GENERAL: daemonize  yes  守护进程  port 6379 指定Redis监听端口 requirepass 1  设置认证密码为1 REPLICATION: slave ...

  8. redis配置文件redis.conf参数说明

    redis配置文件redis.conf参数说明 (2013-01-09 21:20:40)转载▼ 标签: redis配置 redis.conf 配置说明 杂谈 分类: nosql # By defau ...

  9. Redis配置文件(redis.conf)说明

    Redis 配置 Redis 的配置文件位于 Redis 安装目录下,文件名为 redis.conf. 你可以通过 CONFIG 命令查看或设置配置项. 语法3> Redis CONFIG 命令 ...

随机推荐

  1. (八) .launch文件 ---编写简单的启动脚本文件

    下面我们将介绍,如何编写一个启动脚本程序:(.launch文件) 还记得我们在 创建ROS软件包教程 中创建的第一个程序包(beginner_tutorials)吗,现在我们要使用它. 在 begin ...

  2. ThreadExecutorPool 线程池组件

    ThreadExecutorPool是使用最多的线程池组件: Executor是用来执行提交的Runnable任务的对象,并以接口的形式定义,提供一种提交任务(submission task)与执行任 ...

  3. vuex写法

    <template> <div class="hello"> <p>{{count}}</p> <p> <butt ...

  4. axios基本使用

    1,安装axios cnpm install axios --save 2.在main.js里面引入 import axios from 'axios' /*解决在其他组件中不能用*/Vue.prot ...

  5. MongoDB安装成为Windows服务及日常使用遇到问题总结

    安装MongoDB: http://blog.csdn.net/liuzhoulong/article/details/6124566 严格按照上面的步骤,设置数据库目录,设置日志目录,安装服务.可是 ...

  6. mysql utf8mb4

    遇到的问题 有一个项目需要存储 emoji 表情.另外极少数中文在存储的时候也遇到 utf8 编码格式的数据库在储存时报错. Rails creating schema_migrations - My ...

  7. ofo退押金脚本

    同事钉钉给的 因为押金一直没退,电话很难打进去,咨询客服排队要等好久,一直几千位. 长时间挂机就自动退出客服了,所以自动写了一个脚本,目前已经成功退押金了.所以共享出来 1.关注ofo小黄车订阅号,注 ...

  8. git回退文件修改

    假设git仓库某个文件的提交信息如下: [cxy@localhost-live mate-power-manager]$ git log -n3 SPECS/mate-power-manager.sp ...

  9. You have more than one version of ‘org.apache.commons.logging.Log’ visible, which is not allowed问题解决

    https://zeroturnaround.com/forums/topic/jrebel-reports-more-than-one-version-of-org-apache-commons-l ...

  10. Java 中转换为String类型的四种方法

    1. 使用 String 的构造方法,用于 byte[], char[], StringBuffer, StringBuilder 类型 2. 使用 String 的静态方法 valueOf() 推荐 ...