##IO type
Sync |
Asynchronous |
|
Whether to block other redis commands
Yes |
No (there will be when the child process is generated to execute the fork function Short blocking) |
|
Complexity
O(n) |
O(n) |
|
Advantages
Will not consume additional memory |
Does not block client commands |
|
Disadvantages
Blocks client commands |
Needs to fork child process, consumes memory |
|
Configure the automatic generation of rdb files using the bgsave method in the background.
Execute the flushall command
Before clearing Redis, save the current Redis snapshot
Perform the master-slave replication operation (first times)
The first master-slave copy needs to generate an rdb file, and the current Redis snapshot will be saved
RDB execution process
Process Analysis
- The Redis parent process first determines: whether it is currently executing save or bgsave The child process of /bgrewriteaof (aof file rewriting command), if it is executed, the bgsave command returns directly.
- The parent process executes a fork (calling the operating system function to copy the main process) operation to create a child process. During this process, the parent process is blocked and Redis cannot execute requests from the client. any command from the terminal.
- #After the parent process forks, the bgsave command returns the "Background saving started" message and no longer blocks the parent process, and can respond to other commands.
- The child process creates an RDB file, generates a temporary snapshot file based on the memory snapshot of the parent process, and atomically replaces the original file after completion. (RDB is always complete)
- The child process sends a signal to the parent process to indicate completion, and the parent process updates statistics.
- #After the parent process forks the child process, it continues to work.
##RDB file structure
##1. The first 5 bytes are fixed as the "REDIS" string - 2. The 4-byte "RDB" version number (not the Redis version number), currently 9, will be 0009# after filling
##3. Auxiliary fields, in the form of key-value 4. Storage database number5. Dictionary size6. Expired key-
7. Main data is stored in the form of key-value8. End flag9. Checksum is to see whether the file is damaged or modified-
Advantages and disadvantages of RDBAdvantages
RDB is binary Compressed files, occupying little space, are easy to transmit (pass to slave)- The main process forks the child process, which can maximize Redis performance. The main process cannot be too large, and the main process will be blocked during the copy process.
Disadvantages
Data integrity is not guaranteed and all data changed since the last snapshot will be lost
AOFAOF (append only file) is another persistence method of Redis. Redis is not enabled by default. After turning on AOF persistence, Redis records all commands (and their parameters) (RESP) written to the database to the AOF file to achieve the purpose of recording the database status.
In this way, when Redis restarts Just play back these commands in order and the original state will be restored. AOF will record the process, and RDB only cares about the resultsAOF persistence implementationConfiguration redis.conf
# 可以通过修改redis.conf配置文件中的appendonly参数开启
appendonly yes
# AOF文件的保存位置和RDB文件的位置相同,都是通过dir参数设置的。
dir ./
# 默认的文件名是appendonly.aof,可以通过appendfilename参数修改
appendfilename appendonly.aof
Copy after login
AOF PrincipleAOF files store redis commands. The entire process of synchronizing commands to AOF files can be divided into three stages:
Command propagation: Redis sends the executed command, command parameters, command parameter number and other information to the AOF program. - Cache appending: The AOF program converts the command into the format of the network communication protocol based on the received command data, and then appends the protocol content to the server's AOF cache.
- File writing and saving: The content in the AOF cache is written to the end of the AOF file. If the set AOF saving conditions are met, the fsync function or fdatasync function will be called. , actually save the written content to disk.
Command propagation
When a Redis client needs to execute a command, it connects through the network, Send the protocol text to the Redis server. After the server receives the client's request, it will select the appropriate command function based on the content of the protocol text, and convert each parameter from a string text to a Redis string object (StringObject). Whenever the command function is successfully executed, the command parameters will be propagated to the AOF program.
Cache append
When the command is propagated to the AOF program, the program will convert the command from the string object back to the original protocol text based on the command and the parameters of the command. . After the protocol text is generated, it will be appended to the end of aof_buf in the redis.h/redisServer structure.
The redisServer structure maintains the status of the Redis server, and the aof_buf field stores all protocol texts (RESP) waiting to be written to the AOF fileFile writing and saving
每当服务器常规任务函数被执行、或者事件处理器被执行时,aof.c/flushAppendOnlyFile 函数都会被调用,这个函数执行以下两个工作:
AOF保存模式
Redis 目前支持三种 AOF 保存模式,它们分别是:
AOF_FSYNC_NO
从不fsync,将数据交给操作系统来处理。更快,也更不安全的选择。
SAVE只会在以下任意一种情况中被执行:
这三种情况下的SAVE操作都会引起Redis主进程阻塞。
AOF_FSYNC_EVERYSEC
SAVE原则上每隔一秒钟就会执行一次,因为SAVE操作是由后台子线程(fork)调用的, 所以它不会引起服务器主进程阻塞,并且在故障时只会丢失1秒钟的数据。
AOF_FSYNC_ALWAYS
每次执行完一个命令之后,WRITE和SAVE都会被执行。每次有新命令追加到AOF文件时就执行一次fsync,非常慢,也非常安全。
因为SAVE是由Redis主进程执行的,所以在SAVE执行期间,主进程会被阻塞,不能接受命令请求。
AOF保存模式对性能和安全性的影响
三种模式的比较
AOF重写
AOF记录数据的变化过程,越来越大,需要重写“瘦身”
Redis可以在AOF体积变得过大时,自动地在后台(Fork子进程)对AOF进行重写。
重写后的新AOF文件包含了恢复当前数据集所需的最小命令集合。
所谓的“重写”其实是一个有歧义的词语,实际上,AOF重写并不需要对原有的AOF文件进行任何写入和读取,它针对的是数据库中键的当前值。
举例说明
set s1 11
set s1 22
set s1 33
lpush list1 1 2 3
lpush list1 4 5 6
Copy after login
AOF重写后
set s1 33
lpush list1 1 2 3 4 5 6
Copy after login
Redis不希望AOF重写造成服务器无法处理请求,所以Redis决定将AOF重写程序放到(后台)子进程里执行,
不过,使用子进程也有一个问题需要解决:因为子进程在进行AOF重写期间,主进程还需要继续处理命令,而新的命令可能对现有的数据进行修改,这会让当前数据库的数据和重写后的AOF文件中的数据不一致。
为了解决这个问题,Redis增加了一个AOF重写缓存,这个缓存在fork出子进程之后开始启用,Redis主进程在接到新的写命令之后,除了会将这个写命令的协议内容追加到现有的AOF文件之外,还会追加到这个缓存中。
重写过程分析
Redis在创建新AOF文件的过程中,会继续将命令追加到现有的AOF文件里面,即使重写过程中发生停机,现有的AOF文件也不会丢失。而一旦新AOF文件创建完毕,Redis就会从旧AOF文件切换到新AOF文件,并开始对新AOF文件进行追加操作。
当子进程在执行AOF重写时,主进程需要执行以下三个工作:
- 处理命令请求。
- 将写命令追加到现有的AOF文件中。
- 将写命令追加到AOF重写缓存中
。这样一来可以保证:现有的AOF功能会继续执行,即使在AOF重写期间发生停机,也不会有任何数据丢失。所有对数据库进行修改的命令都会被记录到AOF重写缓存中。
当子进程完成AOF重写之后,它会向父进程发送一个完成信号,父进程在接到完成信号之后,会调用一个信号处理函数,并完成以下工作:
- 将AOF重写缓存中的内容全部写入到新AOF文件中。
- 对新的AOF文件进行改名,覆盖原有的AOF文件。
Redis数据库里的+AOF重写过程中的命令------->新的AOF文件---->覆盖老的当步骤1执行完毕之后,现有AOF文件、新AOF文件和数据库三者的状态就完全一致了。
当步骤2执行完毕之后,程序就完成了新旧两个AOF文件的交替。这个信号处理函数执行完毕之后,主进程就可以继续像往常一样接受命令请求了
。在整个AOF后台重写过程中,只有最后的写入缓存和改名操作会造成主进程阻塞,在其他时候,AOF后台重写都不会对主进程造成阻塞,这将AOF重写对性能造成的影响降到了最低。
AOF重写触发方式
#表示当前aof文件大小超过上一次aof文件大小的百分之多少的时候会进行重写。如果之前没有重写过,以启动时aof文件大小为准
auto-aof-rewrite-percentage 100
#限制允许重写最小aof文件大小,也就是文件大小小于64mb的时候,不需要进行优化
auto-aof-rewrite-min-size 64mb
Copy after login
127.0.0.1:6379>bgrewriteaof‘
Backgroundappendonlyfilerewritingstarted
Copy after login
AOF重写总结
混合持久化
RDB和AOF各有优缺点,Redis 4.0开始支持rdb和aof的混合持久化。
如果把混合持久化打开,aofrewrite的时候就直接把rdb的内容写到aof文件开头。
RDB的头+AOF的身体---->appendonly.aof
开启混合持久化
aof-use-rdb-preambleyes
Copy after login
AOF文件的载入与数据还原
如果开启了混合持久化,AOF在重写时,不再是单纯将内存数据转换为RESP命令写入AOF文件,而是将重写这一刻之前的内存做RDB快照处理,并且将RDB快照内容和增量的AOF修改内存数据的命令存在一起,都写入新的AOF文件,新的文件一开始不叫appendonly.aof,等到重写完新的AOF文件才会进行改名,覆盖原有的AOF文件,完成新旧两个AOF文件的替换。
于是在Redis重启的时候,可以先加载RDB的内容,然后再重放增量AOF日志就可以完全替代之前的AOF全量文件重放,因此重启效率大幅得到提升。
- 1、创建一个不带网络连接的伪客户端(fake client)
因为Redis的命令只能在客户端上下文中执行,而载入AOF文件时所使用的命令直接来源于AOF文件而不是网络连接,所以服务器使用了一个没有网络连接的伪客户端来执行AOF文件保存的写命令,伪客户端执行命令的效果和带网络连接的客户端执行命令的效果完全一样
Redis数据备份策略
1.写crontab定时调度脚本,每小时都copy一份rdb或aof的备份到一个目录中去,仅仅保留最近48小时的备份
2.每天都保留一份当日的数据备份到一个目录中去,可以保留最近1个月的备份
3.每次copy备份的时候,都把太旧的备份给删了
4.每天晚上将当前机器上的备份复制一份到其他机器上,以防机器损坏
RDB和AOF对比
1、RDB存某个时刻的数据快照,采用二进制压缩存储,AOF存操作命令,采用文本存储(混合)
2、RDB性能高、AOF性能较低
3、RDB在配置触发状态会丢失最后一次快照以后更改的所有数据,AOF设置为每秒保存一次,则最多丢2秒的数据
4、Redis以主服务器模式运行,RDB不会保存过期键值对数据,Redis以从服务器模式运行,RDB会保存过期键值对,当主服务器向从服务器同步时,再清空过期键值对。AOF写入文件时,对过期的key会追加一条del命令,当执行AOF重写时,会忽略过期key和del命令。
更多编程相关知识,请访问:编程视频!!