RDB问题:
- 耗时耗性能
- 容易丢失数据
耗时耗性能
- O(n)数据 耗时
- fork() :消耗内存,copy-on-write 策略
- Disk I/O :IO性能
不可控 丢失数据
时间 | save |
---|---|
T1 | 执行多个写命令 |
T2 | 满足RDB自动创建的条件 |
T3 | 再次执行多个写命令 |
T4 | 宕机 |
AOF 运行原理 -创建
set hello world
AOF文件
set hello world
hmset myhash a b
sadd myset a b
key | value |
---|---|
hello | string:world |
myhash | hash:{“a”:“b”} |
myset | set:[“a”:“b”] |
AOF 三种策略
- always
- eversec
- no
always
1:redis写命令到刷新的缓冲区
2:每条命令fsync到硬盘aof文件
everysec 系统默认值
1:redis写命令到刷新的缓冲区
2:每秒把缓冲区fsync到硬盘 aof文件
缺点宕机可能会丢失1秒的数据
no
1:根据操作系统
os决定fysnc
always everysec no 优缺点
命令 | always | everysync | no |
---|---|---|---|
优点 | 不丢失数据 | 每秒一次fysnc 丢1秒数据 | 不用管 |
缺点 | IO开销比较大,一般的sata盘只有几百tps | 丢1秒数据 | 不可控 |
AOF重写
原生AOF | AOF重写 |
---|---|
set hello world | set hello hehe |
set hello java | set counter 2 |
AOF重写作用
减少硬盘用量
加速恢复速度
AOF重写实现两种方式
bgrewriteaof
aof重写配置
bgrewriteaof命令
- client发送命令bgrewriteaof 到redis
- redis master 接收
- redis开启子进程
- aof重写到aof文件
AOF从重写自动配置
配置名 | 含义 |
---|---|
auto-aof-rewrite-min-size | aof 文件重写需要的尺寸 |
auto-aof-rewrite-percentage | aof 文件增长率 |
统计
统计名 | 含义 |
---|---|
aof_current_size | aof 当前尺寸 单位字节 |
aof_base_size | aof 上次启动和重写的尺寸 |
AOF重写配置
同时满足
- aof_current_size >auto-aof-rewrite-min-size
- aof-current_size - aof_base_size/aof_base_size > auto-aof_rewrite-percentage
redis.conf
appendonly yes #打开aof功能
appendfilename "appendonly-${port}.aof" //aof文件名称
appendfsync everysec //同步策略
dir /bigdiskpath //保存rdb aof log 目录
no-appendfsync-on-rewrite yes //不进行aof 重写
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes //防止出现断电后aof不完整
AOF 演示
redis-cli
dbsize
vim redis.cong
appendonly yes
appendfilename "appendonly-6379.aof"
appendfsync
config get appendonly
"appendonly"
"no"
config get appendonly yes
ok
config rewrite
ok
exit
redis-cli
set