Redis RDB operation and maintenance and principles

Console command:

  • Save executing this command will block the main thread, making the main thread unable to provide external services. The following official document is as intended as: The save command executes an operation to synchronously save all data sets and save them to the RDB file. In a production environment, you don't want to use this command to operate, because it will block all clients. It is usually replaced by the BGSAVE command in production situations. If a system call error occurs in fork, the save command can be used as the last option for dump data. If the command is executed successfully, it will return OK.
    Insert picture description here

  • bgsave executes this command to fork a child thread, and the main thread can continue to provide services to the outside world. The main idea of ​​the official document is: save data in the background, usually the OK code will return immediately, redis is fork a child process, the parent process continues to provide services, and the child process will do the DB save work. An error will be returned in the following two situations: ① There is already a fork process currently being executed. ②The current AOF is executing the write operation. After the command is executed, it will return immediately. You can use the LASTSAVE command to check whether bgsave is executed successfully.
    Insert picture description here
    Configuration file RDB strategy; in fact, sava is used to identify bgsave in the configuration file. Because the program basically does not use the save command operation when it is running.

#15分钟有一个键值被修改
save 900 1
#5分钟至少10个键值被修改触发RDB
save 300 10
#一分钟至少10000个键值被修改触发RDB
save 60 10000
#当异步保存出错是是否停止
stop-writes-on-bgsave-error yes
#是否启用rdb文件压缩
rdbcompression yes
#是否校验和
rdbchecksum yes
#文件名称
dbfilename dump.rdb
#文件路径
dir /home/redis/redis-cluster/RDB/

Principle of bgsave:
When bgsave is executed, a system call will occur. Fork creates a child process of the current process and copies all memory pointers of the current process to the child process. No specific memory copy has occurred. Use the copy-on-write (copy-on-write) mechanism to ensure that the parent process continues to provide services to the outside world. When the key value changes, the system will reallocate the memory address of the current key value. The content of the location pointed to by the original key-value pointer remains unchanged, which guarantees the timeliness of the data. When a fork call occurs, the system will first initiate a memory request to the system. If your current thread uses 3 G of memory, but the system only has 4 G of memory, you will need to apply for 3 G of memory for protection when the fork process occurs. Completely reliable state, but the system memory is insufficient, the system will return an error to redis. At this time, you can configure the operating system memory strategy in conjunction with stop-writes-on-bgsave-error no to forcibly skip, because the copy-on-write mechanism is used, the most extreme situation will appear, and the memory will be updated once. This extreme situation is almost impossible.
The kernel parameter overcommit_memory is set to 1
optional values: 0, 1, 2.

  • 0 means that the kernel will check whether there is enough available memory for the application process; if there is enough available memory, the memory application is allowed; otherwise, the memory application fails and an error is returned to the application process.
  • 1 means that the kernel allows all physical memory to be allocated, regardless of the current memory state.
  • 2 means that the kernel allows the allocation of memory that exceeds the sum of all physical memory and swap space.

Guess you like

Origin blog.csdn.net/a807719447/article/details/110959523