V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
hecz
V2EX  ›  程序员

redis 持久化问题

  •  
  •   hecz · 2018-03-22 23:08:48 +08:00 · 3002 次点击
    这是一个创建于 2197 天前的主题,其中的信息可能已经有所发展或是发生改变。

    当 redis 调用 bgsave 命令进行持久化操作时,会创建子进程进行生成 rdb 临时文件,这时候如果有写请求进入父进程,是怎么操作的呢?求解答。

    9 条回复    2018-03-23 14:45:51 +08:00
    hecz
        1
    hecz  
    OP
       2018-03-22 23:13:18 +08:00
    up
    Mirana
        2
    Mirana  
       2018-03-22 23:19:09 +08:00
    fork 和 copy on write 了解一下
    fcten
        3
    fcten  
       2018-03-23 00:04:58 +08:00
    copy on write。写入的数据会被复制,最坏的情况下 bgsave 会占用两倍的内存。在 master 上 做 bgsave 容易导致 oom,一般会选择在 slave 上做 bgsave
    etc
        4
    etc  
       2018-03-23 00:07:48 +08:00
    @fcten 关于这个持久化的时候 fork 出来的子进程会不会占用多一倍的内存,我曾经去翻过官方文档,也没见到有提过这么回事。网上个人博客有些人说有,另外的人也是没提过。
    hecz
        5
    hecz  
    OP
       2018-03-23 09:14:03 +08:00
    @Mirana 好的
    hecz
        6
    hecz  
    OP
       2018-03-23 09:14:37 +08:00
    @fcten 那写的也会同时写入子进程么?
    wobutwo
        7
    wobutwo  
       2018-03-23 10:26:39 +08:00   ❤️ 1
    @hecz bgsave 过程中写请求只在父进程进行,子进程不处理(只需把当时 fork 得到的数据存盘就好).

    bgsave 时启动的 fork 进程的主要内存开销是拷贝页表、fd 列表等进程数据结构,只有父进程同时在处理写请求从而影响到某些内存页变动才对这些页进行拷贝增加内存占用.
    lolizeppelin
        8
    lolizeppelin  
       2018-03-23 13:56:03 +08:00 via Android
    人家说理论最坏情况是 1 倍

    理论最坏情况就是 fork 复制部分的内存都被修改过

    那么复制或者说快照时间点的内存就会完全的多出来而不是一份映射
    fcten
        9
    fcten  
       2018-03-23 14:45:51 +08:00   ❤️ 1
    @etc

    https://redis.io/topics/faq

    Redis background saving schema relies on the copy-on-write semantic of fork in modern operating systems: Redis forks (creates a child process) that is an exact copy of the parent. The child process dumps the DB on disk and finally exits. In theory the child should use as much memory as the parent being a copy, but actually thanks to the copy-on-write semantic implemented by most modern operating systems the parent and child process will share the common memory pages. A page will be duplicated only when it changes in the child or in the parent. Since in theory all the pages may change while the child process is saving, Linux can't tell in advance how much memory the child will take
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3207 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 11:58 · PVG 19:58 · LAX 04:58 · JFK 07:58
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.