Redis: five, Redis persistence

1 Redis persistence

  Redis persistence, that is, the data in memory, permanently saved to disk.

  Redis persistence in two ways: RDB (Redis DB), AOF (AppendOnlyFile)

  Redis can be used simultaneously AOF RDB persistence and persistence. In this case, when Redis restarted, it will give priority to the use of AOF file to restore the data set because the saved file AOF file RDB data sets than are usually saved more complete data set.

1.1 RDB (Snapshot mode)

  By default, Redis database snapshots stored in a binary file named dump.rdb, it is possible to modify the persistent information redis.conf configuration file.

wps6

  save 900 1 represented within 900 seconds, the at least one updated data. Redis will persist data to the hard disk

  save 300 10 300 represented in at least 10 updated data, Redis will trigger the persistent data to the hard disk

  save 60 10000 represents 60 seconds, data is updated at least 10,000, it will trigger the Redis persistence data to the hard disk

1.1.1 Strategy

  (1) Automatic: BGSAVE

    According to the configuration file is executed bgsave condition is satisfied;

    Nonblocking, Redis normal service client request receiving process;

    Redis will folk () a new child process to create the RDB file, the child has been dealt with will send a signal to the parent process, informing it processed;

    The parent process to replace the old file with the new dump.rdb.

wps7

wps8

  (2) Manual: SAVE

    The client (redis-cli) launched SAVE command;

    Redis service blocking, unable to respond to client requests;

    Create a new dump.rdb replace the old file.

wps9

1.1.2 advantage

  (1) high efficiency;

  (2) Recovery rate than AOF large data sets quickly.

1.1.3 shortcomings

  (1)会丢失最近写入、修改的而未能持久化的数据;

  (2)folk过程非常耗时,会造成毫秒级不能响应客户端请求。

1.2 AOF(追加模式、文本重演)

1.2.1 特点

  Append only file,采用追加的方式保存,默认文件appendonly.aof;

  记录所有的写操作命令,在服务启动的时候使用这些命令就可以还原数据库;

  AOF默认关闭,需要在配置文件中手动开启。

wps10

wps11

wps12

wps13

1.2.2 写入机制

  说明:AOF机制,添加了一个内存缓冲区(buffer)。

    (1)将内容写入缓冲区

    (2)当缓冲区被填满、或者用户手动执行fsync、或者系统根据指定的写入磁盘策略自动调用fdatasync命令,才将缓冲区里的内容真正写入磁盘里。

    (3)在缓冲区里的内容未写入磁盘之前,可能会丢失。

1.2.3 写入磁盘的策略

  appendfsync选项,这个选项的值可以是always、everysec或者no

wps14

    Always:服务器每写入一个命令,就调用一次fdatasync,将缓冲区里面的命令写入到硬盘。这种模式下,服务器出现故障,也不会丢失任何已经成功执行的命令数据

    Everysec(默认):服务器每一秒重调用一次fdatasync,将缓冲区里面的命令写入到硬盘。这种模式下,服务器出现故障,最多只丢失一秒钟内的执行的命令数据

    No:服务器不主动调用fdatasync,由操作系统决定何时将缓冲区里面的命令写入到硬盘。这种模式下,服务器遭遇意外停机时,丢失命令的数量是不确定的,no 选项并不能给服务器性能带来多大的提升,而且也会增加系统奔溃时数据丢失的数量。

    运行速度:always的速度慢,everysec和no都很快

1.2.4 aof重写机制

  AOF文件过大,合并重复的操作,AOF会使用尽可能少的命令来记录。

1.2.4.1 重写过程

  (1)folk一个子进程负责重写AOF文件

  (2)子进程会创建一个临时文件写入AOF信息

  (3)父进程会开辟一个内存缓冲区接收新的写命令

  (4)子进程重写完成后,父进程会获得一个信号,将父进程接收到的新的写操作由子进程写入到临时文件中

  (5)新文件替代旧文件

  重写的本质:就是将操作同一个键的命令,合并。从而减小AOF文件的体积

wps15

1.2.4.2 aof重写触发机制

  (1)手动:

    客户端向服务器发送BGREWRITEAOF命令

  (2)自动:

    配置文件中的选项,自动执行BGREWRITEAOF命令

wps16

  auto-aof-rewrite-min-size <size>,

    触发AOF重写所需的最小体积:只要在AOF文件的体积大于等于size时,才会考虑是否需要进行AOF重写,这个选项用于避免对体积过小的AOF文件进行重写

  auto-aof-rewrite-percentage <percent>

    指定触发重写所需的AOF文件体积百分比:当AOF文件的体积大于auto-aof-rewrite-min-size指定的体积,并且超过上一次重写之后的AOF文件体积的percent %时,就会触发AOF重写。(如果服务器刚刚启动不久,还没有进行过AOF重写,那么使用服务器启动时载入的AOF文件的体积来作为基准值)。将这个值设置为0表示关闭自动AOF重写。

1.2.5 优点

  写入机制,默认fysnc(手工同步)每秒执行,性能很好不阻塞服务,最多丢失一秒的数据;

  重写机制,优化AOF文件;

  If you misuse the (FLUSHALL, etc.), as long as the AOF has not been overwritten, stop the service end of the file is removed AOF FLUSHALL command to restart Redis, you can restore the data set to the state before FLUSHALL execution.

1.2.6 shortcomings

  The same data set, AOF file size is much larger than the RDB;

  RDB database recovery rate than slower (text, command repeat).

Guess you like

Origin www.cnblogs.com/wozibi/p/11234042.html