redis.conf中的appendfysnc是对redis性能有重要影响的参数之一。可取三种值:always、everysec和no。

设置为always时,会极大消弱Redis的性能,因为这种模式下每次write后都会调用fsync(Linux为调用fdatasync)。

如果设置为no,则write后不会有fsync调用,由操作系统自动调度刷磁盘,性能是最好的。

everysec为最多每秒调用一次fsync,这种模式性能并不是很糟糕,一般也不会产生毛刺,这归功于Redis引入了BIO线程,所有fsync操作都异步交给了BIO线程。

另外,Redis在处理一条命令时,并不立即调用write写AOF文件,只是将数据写入到AOF buffer(server.aof_buf)中。调用write和命令处理是分开的,Redis只在每次进入epoll_wait之前做write操作。

/* Write the append only file buffer on disk.

*

* Since we are required to write the AOF before replying to the client,

* and the only way the client socket can get a write is entering when the

* the event loop, we accumulate all the AOF writes in a memory

* buffer and write it on disk using this function just before entering

* the event loop again.

*

* About the 'force' argument:

*

* When the fsync policy is set to 'everysec' we may delay the flush if there

* is still an fsync() going on in the background thread, since for instance

* on Linux write(2) will be blocked by the background fsync anyway.

* When this happens we remember that there is some aof buffer to be

* flushed ASAP, and will try to do that in the serverCron() function.

*

* However if force is set to 1 we'll write regardless of the background

* fsync. */

#define AOF_WRITE_LOG_ERROR_RATE 30 /* Seconds between errors logging. */

void flushAppendOnlyFile(int force) {

// aofWrite调用write将AOF buffer写入到AOF文件,处理了ENTR,其它没什么

ssize_t nwritten = aofWrite(server.aof_fd,server.aof_buf,sdslen(server.aof_buf));

。。。。。。

/* Handle the AOF write error. */

if (server.aof_fsync == AOF_FSYNC_ALWAYS) {

/* We can't recover when the fsync policy is ALWAYS since the

* reply for the client is already in the output buffers, and we

* have the contract with the user that on acknowledged write data

* is synced on disk. */

serverLog(LL_WARNING,"Can't recover from AOF write error when the AOF fsync policy is 'always'. Exiting...");

exit(1);

} else {

return; /* We'll try again on the next call... */

} else {

/* Successful write(2). If AOF was in error state, restore the

* OK state and log the event. */

}

。。。。。。

/* Perform the fsync if needed. */

if (server.aof_fsync == AOF_FSYNC_ALWAYS) {

// redis_fsync是一个宏,Linux实际为fdatasync,其它为fsync

// 所以最好不要将redis.conf中的appendfsync设置为always,这极影响性能

redis_fsync(server.aof_fd); /* Let's try to get this data on the disk */

}

else if ((server.aof_fsync == AOF_FSYNC_EVERYSEC && server.unixtime > server.aof_last_fsync)) {

// 如果已在sync状态,则不再重复

// BIO线程会间隔设置sync_in_progress

// if (server.aof_fsync == AOF_FSYNC_EVERYSEC)

//     sync_in_progress = bioPendingJobsOfType(BIO_AOF_FSYNC) != 0;

if (!sync_in_progress)

// eversec性能并不那么糟糕,因为它:

// 后台方式执行fsync

// Redis并不是严格意义上的单线程,实际上它创建一组BIO线程,专门处理阻塞和慢操作

// 这些操作就包括FSYNC,另外还有关闭文件和内存的free两个操作。

// 不像always,EVERYSEC模式并不立即调用fsync,

// 而是将这个操作丢给了BIO线程异步执行,

// BIO线程在进程启动时被创建,两者间通过bio_jobs和bio_pending两个

// 全局对象交互,其中主线程负责写,BIO线程负责消费。

aof_background_fsync(server.aof_fd);

server.aof_last_fsync = server.unixtime;

}

}

Redis的appendfsync参数详解的更多相关文章

  1. [redis] Redis 配置文件置参数详解

    ################################ 基础配置 ################################# #daemonize no 默认情况下, redis 不 ...

  2. redis常用配置参数详解

    Redis 支持很多的参数,但都有默认值. daemonize 默认情况下, redis 不是在后台运行的,如果需要在后台运行,把该项的值更改为 yes. pidfile 当 Redis 在后台运行的 ...

  3. Redis配置参数详解

    Redis配置参数详解 /********************************* GENERAL *********************************/ // 是否作为守护进 ...

  4. reids配置参数详解

    转自:http://www.jb51.net/article/60627.htm reids配置参数详解 #daemonize no  默认情况下, redis 不是在后台运行的,如果需要在后台运行, ...

  5. redis.conf 具体配置详解

    redis.conf 具体配置详解 # redis 配置文件示例 # 当你需要为某个配置项指定内存大小的时候,必须要带上单位, # 通常的格式就是 1k 5gb 4m 等酱紫: # # 1k => ...

  6. redis.windows.conf配置详解

    redis.windows.conf配置详解 转自:https://www.cnblogs.com/kreo/p/4423362.html # redis 配置文件示例 # 当你需要为某个配置项指定内 ...

  7. redis.conf配置信息详解

    redis.conf配置信息详解 配置文件 # Redis 配置文件 # 当配置中需要配置内存大小时,可以使用 1k, 5GB, 4M 等类似的格式,其转换方式如下(不区分大小写) # # 1k =& ...

  8. redis 五种数据结构详解(string,list,set,zset,hash)

    redis 五种数据结构详解(string,list,set,zset,hash) Redis不仅仅支持简单的key-value类型的数据,同时还提供list,set,zset,hash等数据结构的存 ...

  9. Redis的事务功能详解

    Redis的事务功能详解 MULTI.EXEC.DISCARD和WATCH命令是Redis事务功能的基础.Redis事务允许在一次单独的步骤中执行一组命令,并且可以保证如下两个重要事项: >Re ...

随机推荐

  1. 30-算法训练 最短路 spfa

    http://lx.lanqiao.cn/problem.page?gpid=T15 算法训练 最短路   时间限制:1.0s   内存限制:256.0MB        问题描述 给定一个n个顶点, ...

  2. 如何查看Chrome浏览器保存的账号密码

    之前告诉大家如何一键查看所有保存在IE里的所有密码(点击查看原文),现在来告诉大家如何一键查看Chrome浏览器的所有密码.某种意义上上,查看Chrome的密码比查看IE的更简单,因为查看IE密码还需 ...

  3. 如何获取google Api Key

    1.使用keytool申请获取MD5值 1.1 找到你的debug.keystore Windows Vista/7: C:\Users\<username>\.android\debug ...

  4. 小程序41028 form_id无效

    如果参数都没有问题的话,那么我的问题来了,你是发给用户自己么?如果不是,那就找到原因了,必须发给本人才可以...我淌过无数条坑,这个坑我服了...官方文档上写的不是很清楚

  5. docker 运行 elasticsearch + kibana

    一.elasticsearch的安装 1.从官网上拉取 elasticsearch 的镜像 docker pull elasticsearch:5.6.11docker images 2.运行容器 m ...

  6. Hyperscan与Snort的集成方案

    概况 Hyperscan作为一款高性能的正则表达式匹配库,非常适用于部署在诸如DPI/IPS/IDS/NGFW等网络解决方案中.Snort (https://www.snort.org) 是目前应用最 ...

  7. java byte转string 涉及到字节流中有中文

    最近遇到一个问题,我用java写了一个客户端通过socket向服务器端发送消息,发送的内容是字节流,编码格式是GBK,服务器在收到消息后,如果格式正确,会返回固定的消息格式,同样也是字节流,编码格式也 ...

  8. 20172325 2018-2019-2 《Java程序设计》第五周学习总结

    20172325 2018-2019-2 <Java程序设计>第五周学习总结 教材学习内容总结 本次学习第九章内容,主要学习查找和排序. 查找 查找的定义:是一个过程,即在某个项目组中寻找 ...

  9. tmux 快捷操作

    -- 基本使用 tmux   # 运行 tmux -2 以256终端运行 C-b d  # 返回主 shell , tmux 依旧在后台运行,里面的命令也保持运行状态 tmux ls # 显示已有tm ...

  10. There are stopped jobs

    问题背景 系统:ubuntu,当输入exit退出shell时,出现There are stopped jobs 无法退出shell 解决办法 找到这个stopped job然后终止它 jobs 或者 ...