Persistence of Redis

By XiaoXin
A Bit Randomly

selectpollepoll select Select first appeared in 4.2BSD in 1983. It monitors an array of multiple file descriptors through a select() system call. When select() returns, the ready file descriptors in the array will be modif... Read Linux Select, Poll and Epoll difference

Main Contents
  1. RDBs 1 Introduction
  2. Implementation principle
  3. Advantages and disadvantages
  4. Related configuration
  5. AOF 1 Introduction
  6. Implementation principle
  7. Synchronization interval configuration
  8. Advantages and disadvantages
  9. Related configuration
  10. Persistence selection strategy

Persistence of Redis

Redis is a non-relational database based on memory. Although memory is fast, data is more likely to be lost, so Redis provides two persistence methods, namely RDB and AOF. Today, I will introduce these two persistence methods and their principles.

RDBs 1 Introduction

Rdb is a kind of snapshot storage and also the default persistence strategy of Redis. It persists the data in memory to disk without affecting the performance of Redis; it has two persistence methods, one is blocking Persistence (save) The other is non-blocking background persistence (bgSave); 

Implementation principle

Using the concept of Linux parent-child process, fork a child process when persisting, but it does not copy all the data in the memory of the parent process, but only moves the pointer, and uses the copy-on-write mechanism to actually copy the data only when writing, and the speed Fast; the parent process does not need to do other IO operations so that the RDB persistence method can maximize the performance of Redis.

Note: It needs to be clear that the parent and child processes of linux are isolated from each other. After the save is enabled, the child process only focuses on the file storage operation. At this time, the main process can still receive client requests, but use the copyonwrite mechanism to copy data when there is a write operation.

Advantages and disadvantages

RDB restores data quickly, and bgsave will not affect the performance provided by Redis, but there will be a greater risk of data loss; it does not support zipper storage, that is, the latest rdb file will overwrite the historical version of the file, the hard disk There is always only one persistent file 

  • save time count triggers rdb when the modification operation occurs count pens within the time period
  • stop-writes-on-bgsave-error Whether to stop the external write operation if an error occurs during persistence, the default is yes; that is, when there is a problem with persistence, such as when the disk is full, the external write will report an error
  • rdbcompression defaults to yes, which means that the snapshot stored in the disk will be compressed by the LZF algorithm
  • rdbchecksum Whether to use the CRC64 algorithm to check whether the rdb file is damaged, the default is yes; if you need to improve the performance of Redis, you can not check here 
  • dbfilename The name of the rdb persistent file, the default is dump.rdb
  • dir The directory where rdb persistent files are stored, the default is the current directory./

AOF 1 Introduction

A write-based persistence method that can achieve almost real-time configuration through configuration, which will affect the performance of the system correspondingly;

Implementation principle

Write each Redis command into the aof file, and has the ability to rewrite: with more and more synchronization operations, the size of the aof file will become larger and larger, and the rewrite mechanism will be triggered at this time; Redis can be used without interruption In the case of serving clients, rebuild the AOF file. Execute the BGREWRITEAOF command, and Redis will generate a new AOF file, this file contains the minimum commands needed to rebuild the current dataset. It should be noted that the rewriting after redis4.0 will write the rdb file into the aof file, and only add the commands after the rdb; this greatly improves the efficiency 

Synchronization interval configuration

You can configure how long Redis will fsync data to disk; Redis provides three methods:

  • 1. Never; it is a faster and less safe choice to hand over the data to the operating system for processing and not take the initiative to flash the disk.
  • 2. Every second (default); fsync once per second: fast enough (similar to using RDB persistence), and only lose 1 second of data in case of failure.
  • 3. Always; execute fsync every time a new command is appended to the AOF file: very slow and very safe

Advantages and disadvantages

AOF is safer and less likely to lose data, but AOF restores data not as fast as RDB, and AOF theoretically has the possibility that the file will always grow infinitely, even if there is a rewriting and merging RDB mechanism, but when Redis runs long enough There will be problems with too large files

  • appendonly Whether to enable aof persistence, the default is no
  • appendfilename AOF file name
  • appendfsync synchronization interval, that is, how often it is written, three candidate values: always, everysec and no, the default is everysec
  • no-appendfsync-on-rewrite Whether to use the no brushing mechanism when rewriting, the default is no, do not use

    Analysis: When redis is restarted, the main process will inevitably have a large number of write operations, both of which will operate the disk and affect performance; this configuration means whether to use the no brushing mechanism, the above is also over, no this This mechanism is the fastest but least reliable writing method; redis defaults to no, which means that it still does not use the no method when rewriting, and would rather block and wait than lose data; if it is yes, it means the master will rewrite The process uses no to wipe the disk witty;

  • auto-aof-rewrite-percentage The rewrite percentage is the trigger timing of the next rewrite. The default is 100, which means that when the growth size of the current aof file reaches 100% of the size of the aof file after the last rewrite, rewrite will be triggered automatically; if Setting it to 0 means that no rewrite operation will be triggered
  • auto-aof-rewrite-min-size specifies the size of the aof file when the rewrite operation is triggered. The default is 64M, that is, the rewrite will only be triggered when the aof file reaches 64M. Even if it is less than 64M, auto-aof-rewrite-percentage will not trigger;
  • aof-load-truncated whether the damaged aof file is still loaded in redis; the default is yes;

Note: if the aof file is corrupted in the middle, redis will still exit with an error.

Persistence selection strategy

  • If some data loss is allowed, you can choose RDB.
  • If you need to avoid data loss as much as possible, you can use the hybrid method of RDB+AOF. 
  • If you need to neutralize performance and loss, you can use AOF's strategy of flushing disks once per second.
Please Share This Article Thank You!

Inter-thread Communication For Android Development

When our software starts, the computer will assign a process to the program we are running, and the process contains multiple threads to improve the speed of the software. In the android network request, we know that in da...

Difference between ArrayDeque and LinkedList in Java

Review LinkedList In terms of data structure, LinkedList not only implements the same List interface as ArrayList, but also implements the Deque interface, and the ArrayDeque we are going to talk about today is a dynamic a...