Table of Contents
RDB persistence
AOF persistence
The difference between RDB and AOF
RDB or AOF Which one should I use?
AOF BGREWRITEAOF rewrite
Backup Redis data
Home Database Redis A brief analysis of RDB and AOF persistence, what are the advantages and disadvantages? How to choose?

A brief analysis of RDB and AOF persistence, what are the advantages and disadvantages? How to choose?

Mar 11, 2022 am 10:30 AM
aof rdb redis Endurance

This article will talk to you about the principles of RDB and AOF persistence in redis. What are their advantages and disadvantages? Analyze which one should be used? I hope to be helpful!

A brief analysis of RDB and AOF persistence, what are the advantages and disadvantages? How to choose?

Redis provides two persistence solutions: RDB and AOF:

  • RDB: Generates Redis in-memory data snapshot is a binary file dumpr.rdb

  • AOF: records all Redis write commands except queries, and re-executes these commands when the Redis service starts. Restore data.

RDB persistence

By default, Redis will persist data over a period of time to the hard disk in the form of RDB snapshots and save them as adumpr.rdb Binary file. [Related recommendations: Redis video tutorial]

A brief introduction to the working principle:

When Redis needs to be persisted, Redis will fork a Child process, the child process writes data to a temporary RDB file on the disk. When the child process finishes writing the temporary file, replace the original RDB. The advantage of this is that it can copy-on-write.

Of course we can also manually execute save or bgsave (asynchronously) to generate RDB files.

redis.conf default configuration

save 900  1
save 300  10
save 60  10000
Copy after login
  • If more than 1 key is modified within 900 seconds, snapshot saving will be initiated;
  • Within 300 seconds, If more than 10 keys are modified, a snapshot save is initiated;
  • Within 60 seconds, if 10,000 keys are modified, a snapshot save is initiated;

RDB snapshot command

By default, Redis saves the database snapshot in a binary file named dump.rdb.

You can set Redis to automatically save a data set when the condition "the data set has at least M changes within N seconds" is met.

You can also manually let Redis save the data set by calling SAVE or BGSAVE.

For example, the following settings will allow Redis to automatically save a data set when it meets the condition of "at least 1000 keys have been changed within 60 seconds":

save 60 1000
Copy after login

This kind of persistence This method is called a snapshot.

RDB Creation Principle

When Redis needs to save the dump.rdb file, the server performs the following operations:

  • Redis calls fork() and has both parent and child processes.
  • The subprocess writes the data set to a temporary RDB file.
  • When the child process completes writing to the new RDB file, Redis replaces the original RDB file with the new RDB file and deletes the old RDB file.

This way of working allows Redis to benefit from the copy-on-write mechanism.

Advantages of RDB

RDB is a relatively compact file that saves Redis data at a certain point in time. This kind of data More suitable for backup and disaster recovery.

For example, you can back up an RDB file every hour within the last 24 hours, and also back up an RDB file every day of the month. This way, even if you encounter a problem, you can always restore the data set to a different version.

Disadvantages of RDB

If you need to minimize data loss in the event of a server failure, then RDB is not for you. Although Redis allows you to set different save points to control the frequency of saving RDB files, it is not an easy operation because RDB files need to save the state of the entire data set. So you may save the RDB file at least once every 5 minutes. In this case, in the event of an outage, you may lose several minutes of data.

AOF persistence

Use AOF for persistence. Each write command is appended to appendonly.aof# through the write function. ## file.

AOF can achieve full persistence. It only needs to be turned on in the configuration file (the default is no).

appendfsync yes After turning on AOF, every time Redis executes a command to modify data, it will Add it to the AOF file, and when Redis restarts, the AOF file will be read and "replayed" to restore the last moment before Redis was shut down.

AOF configuration

You can configure how often Redis fsyncs data to disk.

redis.conf default configuration

appendfsync yes
appendfsync always     #每次有数据修改发生时都会写入AOF文件。
appendfsync everysec   #每秒钟同步一次,该策略为AOF的缺省策略。
Copy after login
has three options:

1, execute fsync every time a new command is appended to the AOF file : very slow and very safe.
2, fsync once per second: fast enough (almost the same as using RDB persistence), and only 1 second of data will be lost in the event of a failure.
3, never fsync: Leave the data to the operating system for processing. Faster and less secure option.

The recommended (and default) measure is to fsync once per second. This fsync strategy can take into account both speed and security.

AOF creation principle

AOF rewriting is the same as RDB snapshot creation, both of which cleverly utilize the copy-on-write mechanism.

The following are the execution steps of AOF rewriting:

Redis executes fork() and now has both the parent process and the child process.

The child process starts writing the contents of the new AOF file to the temporary file.

For all newly executed write commands, the parent process accumulates them into a memory cache and appends these changes to the end of the existing AOF file: This way, even if a shutdown occurs in the middle of a rewrite, Existing AOF files are still safe.

When the child process completes the rewriting work, it sends a signal to the parent process. After receiving the signal, the parent process appends all the data in the memory cache to the end of the new AOF file.

Done! Redis now atomically replaces the old file with the new one, and all commands thereafter are appended directly to the end of the new AOF file.

Advantages of AOF

1. Using AOF for persistence, you can set different fsync strategies, such as no fsync, once per second fsync, or fsync every time a write command is executed.

The default policy of AOF is to fsync once per second. Under this configuration, Redis can still maintain good performance, and even if a failure occurs, only one second of data will be lost at most.

fsync will be executed on a background thread, so the main thread can continue to work hard processing command requests.

2. The AOF file is a log file that only performs append operations. It is not one that is replaced after generating a new one. Even if the log contains incomplete commands for some reasons (such as when writing The disk is full, writing is stopped midway, etc.), the redis-check-aof tool can also easily fix this problem.

3, Redis can automatically rewrite the AOF in the background when the AOF file size becomes too large: the new AOF file after rewriting contains the minimum set of commands required to restore the current data set.

The entire rewriting operation is absolutely safe, because Redis rewriting creates a new AOF file. During the rewriting process, commands will continue to be appended to the existing old AOF file, even if an error occurs during the rewriting process. Even if there is a shutdown, the existing old AOF files will not be lost. Once the new AOF file is created, Redis will switch from the old AOF file to the new AOF file and start appending to the new AOF file.

4. The AOF file stores all write operations performed on the database in an orderly manner. These write operations are saved in the format of the Redis protocol, so the content of the AOF file is very easy to read. Analysis (parse) is also easy. Exporting AOF files is also very simple: for example, if you accidentally execute the _FLUSH ALL_ (clear the data of the entire Redis server (delete all keys in all databases).) command, but as long as the AOF file has not been overwritten , then as long as you stop the server, remove the FLUSHALL command at the end of the AOF file, and restart Redis, you can restore the data set to the state before FLUSHALL was executed.

Disadvantages of AOF

For the same data set, the size of the AOF file is usually larger than the size of the RDB file.

Depending on the fsync strategy used, AOF may be slower than RDB. Under normal circumstances, fsync performance per second is still very high, and turning off fsync can make AOF as fast as RDB, even under heavy load.

However, when dealing with huge write loads, RDB can provide a more guaranteed maximum latency (latency).

The difference between RDB and AOF

RDB persistence refers to writing a snapshot of the data set in memory to disk within a specified time interval. The actual operation process It is to fork a child process and first write the data set into a temporary file. After the writing is successful, the previous file is replaced and stored using binary compression.

AOF persistence records every write and delete operation processed by the server in the form of a log. Query operations will not be recorded. Records are appended in the form of text. You can open the file to see detailed operation records.

RDB or AOF Which one should I use?

If you care deeply about your data but can still afford data loss within minutes, then you can just use RDB persistence.

AOF appends every command executed by Redis to the disk. Processing huge writes will reduce the performance of Redis. I don’t know if you can accept it.

Database backup and disaster recovery:

Regularly generating RDB snapshots is very convenient for database backup, and RDB restores data sets faster than AOF.

Redis supports opening RDB and AOF at the same time. After the system restarts, Redis will give priority to using AOF to restore data, so that the data lost will be the least.

AOF BGREWRITEAOF rewrite

Because AOF works by continuously appending commands to the end of the file, so as the number of written commands continues to increase, the AOF file The size will also become larger and larger.

For example

If you call INCR 100 times on a counter, then just to save the current value of the counter, the AOF file needs Use 100 records (entry).

However, in reality, using only one SET command is enough to save the current value of the counter, and the remaining 99 records are actually redundant.

In order to handle this situation, Redis supports an interesting feature: the AOF file can be reconstructed without interrupting the service client.

Execute the BG REWRITE AOF command, Redis will generate a new AOF file, which contains the minimum commands required to reconstruct the current data set.

Redis 2.2 needs to manually execute the BGREWRITEAOF command; Redis 2.4 can automatically trigger AOF rewriting. For specific information, please see the sample configuration file of 2.4.

Backup Redis data

Disk failure, node failure, and other problems may cause your data to disappear. Failure to back up is very dangerous.

Redis is very friendly for data backup, because you can copy the RDB file while the server is running: Once the RDB file is created, no modifications will be made. When the server wants to create a new RDB file, it first saves the contents of the file in a temporary file. When the temporary file is written, the program uses rename(2) to atomically replace the original RDB file with the temporary file.

This means that it is absolutely safe to copy RDB files at any time.

The following are our suggestions:

1. Create a regular task (cron job) to back up an RDB file to a folder every hour, and backup it every day. Back up an RDB file to another folder.

2. Make sure that the snapshot backups have corresponding date and time information. Every time you execute the regular task script, use the find command to delete expired snapshots: For example, you can keep the snapshots within the last 48 hours. Hourly snapshots, and daily snapshots for the last one or two months can also be retained.

3. At least once a day, back up the RDB outside your data center, or at least outside the physical machine where you are running the Redis server.

For more programming related knowledge, please visit: Programming Video! !

The above is the detailed content of A brief analysis of RDB and AOF persistence, what are the advantages and disadvantages? How to choose?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to build the redis cluster mode How to build the redis cluster mode Apr 10, 2025 pm 10:15 PM

Redis cluster mode deploys Redis instances to multiple servers through sharding, improving scalability and availability. The construction steps are as follows: Create odd Redis instances with different ports; Create 3 sentinel instances, monitor Redis instances and failover; configure sentinel configuration files, add monitoring Redis instance information and failover settings; configure Redis instance configuration files, enable cluster mode and specify the cluster information file path; create nodes.conf file, containing information of each Redis instance; start the cluster, execute the create command to create a cluster and specify the number of replicas; log in to the cluster to execute the CLUSTER INFO command to verify the cluster status; make

How to use the redis command How to use the redis command Apr 10, 2025 pm 08:45 PM

Using the Redis directive requires the following steps: Open the Redis client. Enter the command (verb key value). Provides the required parameters (varies from instruction to instruction). Press Enter to execute the command. Redis returns a response indicating the result of the operation (usually OK or -ERR).

How to implement the underlying redis How to implement the underlying redis Apr 10, 2025 pm 07:21 PM

Redis uses hash tables to store data and supports data structures such as strings, lists, hash tables, collections and ordered collections. Redis persists data through snapshots (RDB) and append write-only (AOF) mechanisms. Redis uses master-slave replication to improve data availability. Redis uses a single-threaded event loop to handle connections and commands to ensure data atomicity and consistency. Redis sets the expiration time for the key and uses the lazy delete mechanism to delete the expiration key.

How to view all keys in redis How to view all keys in redis Apr 10, 2025 pm 07:15 PM

To view all keys in Redis, there are three ways: use the KEYS command to return all keys that match the specified pattern; use the SCAN command to iterate over the keys and return a set of keys; use the INFO command to get the total number of keys.

How to start the server with redis How to start the server with redis Apr 10, 2025 pm 08:12 PM

The steps to start a Redis server include: Install Redis according to the operating system. Start the Redis service via redis-server (Linux/macOS) or redis-server.exe (Windows). Use the redis-cli ping (Linux/macOS) or redis-cli.exe ping (Windows) command to check the service status. Use a Redis client, such as redis-cli, Python, or Node.js, to access the server.

How to read the source code of redis How to read the source code of redis Apr 10, 2025 pm 08:27 PM

The best way to understand Redis source code is to go step by step: get familiar with the basics of Redis. Select a specific module or function as the starting point. Start with the entry point of the module or function and view the code line by line. View the code through the function call chain. Be familiar with the underlying data structures used by Redis. Identify the algorithm used by Redis.

How to use redis lock How to use redis lock Apr 10, 2025 pm 08:39 PM

Using Redis to lock operations requires obtaining the lock through the SETNX command, and then using the EXPIRE command to set the expiration time. The specific steps are: (1) Use the SETNX command to try to set a key-value pair; (2) Use the EXPIRE command to set the expiration time for the lock; (3) Use the DEL command to delete the lock when the lock is no longer needed.

How to implement redis counter How to implement redis counter Apr 10, 2025 pm 10:21 PM

Redis counter is a mechanism that uses Redis key-value pair storage to implement counting operations, including the following steps: creating counter keys, increasing counts, decreasing counts, resetting counts, and obtaining counts. The advantages of Redis counters include fast speed, high concurrency, durability and simplicity and ease of use. It can be used in scenarios such as user access counting, real-time metric tracking, game scores and rankings, and order processing counting.

See all articles