redis------persistence scheme

why persist

Redis is an in-memory database . If the state of the database in memory is not saved to disk, once the server process exits, the state of the database in the server will also be lost. Data loss is a serious production and failure, so Redis data needs to be for persistence. Redis provides the following different levels of persistence

  • RDB snapshot persistence can generate point-in-time snapshots of memory datasets at specified intervals
  • AOF persistently records all write commands executed by the server, and restores the data through command replay when the service restarts
  • Hybrid persistence, taking into account the characteristics of RDB and AOF

RDB snapshot

By default, Redis saves in-memory database snapshots in dump.rdbbinary files named .

You can modify redis.confthe configuration to allow Redis to automatically save the data set once when the condition that the data set has at least M changes within N seconds is met.

 1. RDB trigger mechanism

# 触发机制-主要三种方式
# save(同步)--->阻塞--》如果数据量很多---》影响redis的查询操作
save

# bgsave(异步,Backgroud saving started)


# 自动(通过配置)
配置   seconds   changes
save   900        1
save   300        10
save   60         10000

如果60s中改变了1w条数据,自动生成rdb
如果300s中改变了10条数据,自动生成rdb
如果900s中改变了1条数据,自动生成rdb

2、AOF (append-only file)

The snapshot function is not very durable (durable): if redis fails for some reason, the server will lose the most recently written data that has not yet been saved in the snapshot.

# AOF的三种策略
日志不是直接写到硬盘上,而是先放在缓冲区,缓冲区根据一些策略,写到硬盘上
always:redis–》写命令刷新的缓冲区—》每条命令fsync到硬盘—》AOF文件
everysec(默认值):redis——》写命令刷新的缓冲区—》每秒把缓冲区fsync到硬盘–》AOF文件
no:redis——》写命令刷新的缓冲区—》操作系统决定,缓冲区fsync到硬盘–》AOF文件\



# AOF 重写
本质就是把过期的,无用的,重复的,可以优化的命令,来优化
这样可以减少磁盘占用量,加速恢复速度

# aof配置
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec 
dir /root/lqz/redis/data

Details:
x Depth Good Article: Nanny-Level Tutorial to Thoroughly Understand Redis Persistence - Tencent Cloud Developer Community - Tencent Cloud (tencent.com)

 

Guess you like

Origin blog.csdn.net/xiaolisolovely/article/details/132417241