`
carlosfu
  • 浏览: 576777 次
  • 性别: Icon_minigender_1
  • 来自: 北京
博客专栏
Ba8b5055-9c58-3ab0-8a1c-e710f0495d2c
BigMemory实战与理...
浏览量:30517
53b2087e-c637-34d2-b61d-257846f73ade
RedisCluster开...
浏览量:150206
C9f66038-7478-3388-8086-d20c1f535495
缓存的使用与设计
浏览量:123519
社区版块
存档分类
最新评论

Redis AOF刷新策略分析(转载)

阅读更多

此文为转载,原文: http://afei2.sinaapp.com/?p=536

   redis支持使用aof来进行持久化,防止数据丢失,aof的刷新策略通过参数appendfsync控制,有三个值:always、everysec、no,默认是everysec。
   下面从源码的角度剖析一下aof的刷新策略。
   每次redis进入event循环准备执行这个event时,会调用beforeSleep方法

   

   src/aof.c

 

void flushAppendOnlyFile(int force) {
    ......
    /* Perform the fsync if needed. */
    if (server.aof_fsync == AOF_FSYNC_ALWAYS) {
        /* aof_fsync is defined as fdatasync() for Linux in order to avoid
         * flushing metadata. */
        latencyStartMonitor(latency);
        aof_fsync(server.aof_fd); /* Let's try to get this data on the disk */
        latencyEndMonitor(latency);
        latencyAddSampleIfNeeded("aof-fsync-always",latency);
        server.aof_last_fsync = server.unixtime;
    } else if ((server.aof_fsync == AOF_FSYNC_EVERYSEC &&
                server.unixtime > server.aof_last_fsync)) {
        if (!sync_in_progress) aof_background_fsync(server.aof_fd);
        server.aof_last_fsync = server.unixtime;
    }
}
   AOF_FSYNC_ALWAYS会调用aof_fsync进行同步写入,而aof_fsync在linux下就是fdatasync,
   AOF_FSYNC_EVERYSEC会调用aof_background_fsync,而aof_background_fsync会创建一个任务交给后台的bio线程进行处理。

 

 

/* Define aof_fsync to fdatasync() in Linux and fsync() for all the rest */
#ifdef __linux__
#define aof_fsync fdatasync
#else
#define aof_fsync fsync
#endif
/* Starts a background task that performs fsync() against the specified
 * file descriptor (the one of the AOF file) in another thread. */
void aof_background_fsync(int fd) {
    bioCreateBackgroundJob(REDIS_BIO_AOF_FSYNC,(void*)(long)fd,NULL,NULL);
}
    其中everysec是通过下面的逻辑来进行的,检测后台是否fsync任务在进行,如果有的话,判断上次的fsync距离现在的时间,如果大于2s,则阻塞,否则直接进行后台队列。

 

    如果上一次的fsync执行了2s多,则会阻塞执行,直到写入成功,这个时候日志中会记录下面一条记录,并且增加info中对应的aof_delayed_fsync值
 
[5750] 12 Aug 09:56:17.057 * Asynchronous AOF fsync is taking too long (disk is busy?). Writing the AOF buffer without waiting for fsync to complete, this may slow down Redis.
详细逻辑如下:
/*
 * 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.
 *
 * 但是如果上一次的fsync执行了2s多,则会阻塞执行,直到写入成功
 */
    /* With this append fsync policy we do background fsyncing.
     * If the fsync is still in progress we can try to delay
     * the write for a couple of seconds. */
    if (sync_in_progress) {
        if (server.aof_flush_postponed_start == 0) {
            /* No previous write postponinig, remember that we are
             * postponing the flush and return. */
            server.aof_flush_postponed_start = server.unixtime;
            return;
        } else if (server.unixtime - server.aof_flush_postponed_start < 2) {
            /* We were already waiting for fsync to finish, but for less
             * than two seconds this is still ok. Postpone again. */
            return;
        }
        /* Otherwise fall trough, and go write since we can't wait
         * over two seconds. */
 aof_pending_bio_fsync
/* Return the number of pending jobs of the specified type. */
bioPendingJobsOfType(REDIS_BIO_AOF_FSYNC),
serverCron中检查server.aof_flush_postponed_start,如果有的话,就追加一次flush,但是只有在上面的情况下会导致阻塞,其他情况下都会很快返回;
/* AOF postponed flush: Try at every cron cycle if the slow fsync
* completed. */
if (server.aof_flush_postponed_start) flushAppendOnlyFile(0);
 
 
分享到:
评论

相关推荐

    Redis AOF持久化.flv

    Redis AOF持久化.flv

    redis-analyzer:解析rdb, aof, 以及执行monitor, 来查找key和分析各种top-key(big key, hot-key, expiry-key, slowlog-key)

    redis-analyzer解析rdb, aof, 以及执行monitor, 来查找key和分析各种top-key(big key, hot-key, expiry-key, slowlog-key)安装go get github.com/lanfang/redis-analyzer如何使用执行redis-analyzer来查看帮助信息,...

    springboot整和jwt、shiro、redis实现token自动刷新

    对于Redis的使用,我们需要配置一个TokenStore,将JWT令牌保存到Redis中,并设置相应的过期策略。当JWT即将过期时,可以监听Redis的事件,触发Token的刷新。同时,为了实现自动刷新,客户端也需要配合,当收到新的...

    redis-check-aof.exe

    redis-check-aof.exe-

    Redis 持久化之RDB和AOF.doc

    Redis 持久化是确保数据安全的重要机制,它提供了两种主要的方法:RDB(Redis Database)和 AOF(Append Only File)。RDB 是一种快照式的持久化方式,而 AOF 则记录每次写操作的日志。 RDB 持久化在特定条件下将...

    Redis持久化策略详解

    Redis持久化策略详解

    redis-淘汰策略.png

    redis-淘汰策略

    AOF持久化策略配置文件

    在 Redis 中,AOF(Append Only File)持久化策略是数据保存的一种方式,它记录了所有对数据库进行修改的命令,确保即使在服务器崩溃后也能恢复数据。本文将深入探讨 AOF 持久化策略及其配置文件。 首先,我们需要...

    redis源码阅读中文分析注释

    redis源码阅读中文分析注释

    redis学习之RDB、AOF与复制时对过期键的处理教程

    Redis 是一款高性能的键值...在实际应用中,可以根据业务需求选择适合的持久化策略,例如,RDB 提供了更快的恢复速度,而 AOF 则提供了更高的数据安全性。同时,主从复制可以保证数据的实时同步,提高系统的整体性能。

    Redis技术分析及运用

    ### Redis技术分析及运用 #### 一、Redis简介与特性 Redis是一种开源的键值(Key-Value)存储系统,属于非关系型数据库(NoSQL)的一种,它将数据存储在内存中,以提高数据访问速度。由于其高效的数据结构和丰富的...

    redis之相关理解分析以及面试问题总结

    - 持久化:Redis提供了RDB(快照)和AOF(日志)两种持久化方式,确保在服务器重启后能够恢复数据。 - 主从复制:通过主从复制,可以实现数据备份和读写分离,提高系统的可用性和性能。 - 事务:Redis支持简单...

    Redis持久化 - RDB和AOF

    “Redis持久化 - RDB和AOF” Redis持久化是指将数据库中的数据保存到永久存储设备中,以避免数据丢失。Redis提供了两种持久化方式:RDB(快照方式)和AOF(写日志方式)。 RDB(Redis Database)是一种快照方式的...

    redis键值数据库源码分析

    redis_check_aof.c 用于更新日志检查的实现。 redis_check_dump.c 用于本地数据库检查的实现。 testhelp.c 一个C风格的小型测试框架。 struct:(结构体) adlist.c 用于对list的定义,它是个双向链表结构 dict.c ...

    Redis流量-流量分析

    总结,Redis流量分析涉及到多个层面,包括数据结构的选择、操作频率的监控、内存管理、持久化策略、网络优化以及使用适当的监控工具。通过对这些方面的深入了解和细致调整,可以有效控制和优化Redis的流量,确保系统...

    redis配置文件aof持久化方式

    redis配置文件aof持久化方式,修改了redis密码为123456

    Redis架构设计分析

    本文将从Redis的启动流程入手,逐步分析其内部结构与工作原理,并探讨Redis的两种数据持久化方案——RDB和AOF。 #### 二、Redis启动流程 Redis的启动流程主要包含以下几个步骤: 1. **初始化server变量**:在此...

Global site tag (gtag.js) - Google Analytics