美文网首页
Redis持久化的取舍与选择

Redis持久化的取舍与选择

作者: 第二套广播体操 | 来源:发表于2020-10-15 15:58 被阅读0次

    什么是Redis的持久化

    Redis上所有的数据都保存在内存中,对数据的更新将异步地保存在磁盘上。

    Redis持久化的两种方式

    快照 (RDB)

    将数据保存在本地中,对数据进行恢复

    写日志 (AOF)

    将命令保存在本地日志中,恢复时通过恢复命令进行恢复数据

    RDB

    由redis创建RDB文件保存在磁盘中。每次启动进行载入。

    触发的三种机制

    save

    同步创建会阻塞进程(其他命令无法执行)

    bgsave

    异步创建由redis执行fork方法创建redis子进程 由子进程创建RDB文件

    区别.png

    自动生成RDB

    多少秒之内发生多少次变化自动保存 关闭 性能太差啦

    save 900 1

    save 300 10

    save 60 10000

    命令

    stop-writes-on-bgsave-error yes 当写入出现错误是是否停止写入 yes

    rdbcompression yes 压缩格式

    rdbchecksum yes 是否对rdb进行检验

    AOF

    保存命令的方式对数据进行持久化

    三种策略

    always 实时进行存储 每条命令都执行fsync 进入硬盘 数据不会丢失 影响性能 IO 开销大

    everysec 每秒进行存储 丢失丢失1s数据

    no 操作系统决定什么时候刷新进aof 不稳定 不要用!

    AOF重写

    对大量命令进行整理保留有用命令

    减少硬盘占用量 加速恢复速度

    例如

    <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="sql" cid="n37" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background: rgb(51, 51, 51); position: relative !important; padding: 10px 10px 10px 30px; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">set hello world
    set hello hehe
    set hello java
    -- 最后经过AOF重写会只保留 set hello java 来节省空间</pre>

    重写实现的方式

    bgrewriteof 命令

    通过命令使redis 生成子进程来进行AOF重写生产AOF文件

    配置

    auto-aof-rewrite-min-size AOF重写需要的尺寸

    auto-aof-rewrite-percentage AOF 文件增长率

    使用配置

    appendonly yes 启动重写

    appendfsync everysec 每秒

    no-appendfync-on-rewrite yes 重写不做aof操作

    auto-aof-rewrite-min-size 64mb

    auto-aof-rewrite-percentage 100

    统计名

    aof_current_size aof当前尺寸

    aof_base_size aof上次启动和重写尺寸

    自动触发机制

    aof_current_size>auto-aof-rewrite-min-size

    aof_current_size-aof_base_size/aof_base_size > auto-aof-rewrite-percentage

    权衡

    RDB关闭 集中管理时可以使用

    AOF 开启 AOF在集中管理时进行重写 everysec 自动生成

    相关文章

      网友评论

          本文标题:Redis持久化的取舍与选择

          本文链接:https://www.haomeiwen.com/subject/hukopktx.html