Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the implementation process of Redis holding RDB and AOF?

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Today, I will talk to you about the implementation process of Redis holding RDB and AOF, which may not be well understood by many people. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.

Preface

Redis persistence supports two ways: RDB and AOF. This article records the implementation process and configuration of both.

1. RDB

RDB persistence is the process of generating a snapshot of the current process data and saving it to the hard disk. The process of triggering RDB persistence can be divided into manual trigger and automatic trigger.

1. Save command

The current Redis server will be blocked until the end of RDB. Instances with large amount of data or large memory will be blocked for a long time. It is not recommended to use it in production environment. If you execute the save command manually, Redis records the log below.

127.0.0.1 purl 6379 > save

OK

* DB saved on disk

2. Bgsave command

The Redis process executes the fork operation to create the child process, and the RDB persistence process is responsible for the child process, which ends automatically after completion. Blocking occurs only in the fork phase, usually for a short time. If you execute the bgsave command manually, Redis records the log below.

* Background saving started by pid 90338

* DB saved on disk

* RDB: 0 MB of memory used by copy-on-write

* Background saving terminated with success

Bgsave optimizes save congestion, and the RDB operations involved in Redis are done by bgsave.

3. Internally trigger RDB scene

Use save-related configurations, such as save m n to trigger a RDB when there are n modifications to the dataset in m seconds

The slave node performs a full copy operation, and the master node automatically executes bgsave to generate RDB files and sends them to the slave node.

Production RDB is also triggered when debug relad reloads Redis

By default, when you execute shutdown to turn off Redis, if AOF persistence is not enabled, RDB will be triggered.

4. RDB parameter configuration

You can configure the RDB save location by setting dir. Dbfilename can set the file name.

Config set dir / opt/redis-5.0.12/backup

Config set dbfilename myback.rdb

Redis uses the LZF algorithm to compress the generated RDB file by default. The compressed file is much smaller than the memory size. It is enabled by default and can be configured through the rdbcompression parameter.

Config set rdbcompression {yes | no}

Although compressing RDB will consume CPU, it can greatly reduce the file size and facilitate storage or sending to slave nodes over the network.

5. Shortcomings of RDB

There is no way to achieve real-time persistence / second persistence of RDB data. Because every time bgsave runs, it has to perform the fork operation to create a child process, which is a heavyweight operation, and the cost of frequent execution is too high.

RDB files are saved in a specific binary format. During the evolution of Redis versions, there are multiple RDB versions of formats. There is a problem that the old version of Redis service is not compatible with the new version of RDB format.

II. AOF

AOF (appendonlyfile) persistence: record each write command as an independent log, and then re-execute the command in the AOF file when you restart to recover the data. The main function of AOF is to solve the real-time performance of data persistence, and it has become the mainstream way of Redis persistence.

1. Parameter configuration # configuration enables the AOFconfig set appendonly yes# configuration file name. The default appendonly.aofconfig set appendfilename xxx.aof# storage location configuration is the same as RDB config set dir / opt/redis-5.0.12/backup2. AOF execution process

All commands are appended to the aof_buf (buffer); the AOF buffer synchronizes to the disk according to the corresponding policy; as the AOF file becomes larger and larger, the AOF needs to be rewritten regularly to achieve the purpose of compression; when the Redis server is restarted, the AOF file can be loaded for data recovery. 3. Rewriting mechanism

Manually triggered:

Bgrewriteaof

Automatic trigger, and set the automatic trigger mechanism according to the following two parameters:

Auto-aof-rewrite-min-size

Auto-aof-rewrite-percentage

After reading the above, do you have any further understanding of the implementation process of Redis holding RDB and AOF? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report