The difference between the RDB and aof persistence for Redis [go]

Source: Internet
Author: User
Tags compact postgresql redis server


Aof,rdb is a mechanism for two types of redis persistence. For the recovery of Redis after crash.


The features of the RDB are as follows:

Code:

Fork a process, traverse hash table, and use copy on write to save the entire db dump.
Save, shutdown, slave command will trigger this operation.
Particle size ratio is large, if save, shutdown, slave before crash, then the middle of the operation has no way to recover.


AOF has the following characteristics:


Code:

Write the instructions, continue to write to a similar log file. (similar to exporting SQL from databases such as PostgreSQL, only write operations are logged)
The granularity is small, after crash, only then crash did not have time to do the log the operation cannot recover.


The two differences are that one is the continuous use of log records to write operations, crash after the use of log recovery; One is usually write operations do not trigger the write, only manually submit the Save command, or close the command, the backup operation is triggered.



The standard of choice is to see if the system is willing to sacrifice some performance in exchange for higher cache consistency (AOF), or is willing to write frequently, do not enable backup in exchange for higher performance, when manually run save, then do Backup (RDB). The RDB is a bit more eventually consistent.
The difference between the RDB and AOF persistence for Redis





Performance comparison of the RDB and AOF modes of Redis



Url:http://blog.sina.com.cn/s/blog_4458fdda010150bh.html





Because it's the same machine, I'm not going to configure it for a relative comparison. The system is Debian Testing,kernel 3.2 686. Redis 2.4.8. The test method is to write to the Redis database using a Python-written script to read the write speed. 100000/300000/1000000 is the amount of data that is inserted into a string. The first data is the minimum time, the second is the average, and the third is the data size. 100000:dbmode:4.8, 5.1, 1477792aofmode:9.1, 9.3, 3677803300000:dbmode:16.5, 17.6, 4877792aofmode:21.1, 21.4, 114778031 000000:dbmode:61, 16777792aofmode:77, 85, 38777849 from a simple analysis, aof is slower 25-80% than an RDB, but large-scale data supports slower 25%. It is estimated that the RDB model is more dominant in low data volumes. When the data scale grows, the rate ratio is close to 4:5. AOF data is 150% larger (2.5 times times up or down) than the RDB data, which is basically the same as data growth. Judging from the reading performance analysis, the two differences are not big. Again, the data is the minimum and average time. Dbmode:55, 60aofmode:62, 63


The difference is less than 10%, even weaker than the minimum-average difference. Basic can be regarded as consistent.






Redis Persistent RDB and aof


url:http://my.oschina.net/davehe/blog/174662


Redis Persistence:


Provides several different levels of persistence: one is an RDB and the other is aof.



RDB persistence can generate a point-in-time snapshot of a dataset (Point-in-time snapshot) within a specified time interval.



The AOF persists all the write commands that the server performs and restores the dataset by re-executing the commands when the server starts. The commands in the AOF file are all saved in the Redis protocol format, and the new command is appended to the end of the file. Redis can also override the AOF file in the background (rewrite) so that the volume of the AOF file does not exceed the actual size required to save the dataset State. Redis can also use both AOF persistence and RDB persistence. In this case, when the Redis restarts, it takes precedence over the AOF file to restore the dataset because the AOF file saves a dataset that is typically more complete than the data set saved by the RDB file. You can even turn off the persistence feature so that the data exists only when the server is running.




It is important to understand the similarities and differences between the RDB persistence and AOF persistence, which are described in detail in the following subsections, and are described in the same and different aspects of these two persistence features.



Advantages of the RDB:
An RDB is a very compact file that holds the data set of a Redis at a point in time. This file is ideal for backup: 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. In this case, you can restore the dataset to a different version at any time, even if you encounter problems. The RDB is ideal for disaster recovery (disaster recovery): It has only one file, and the content is very compact and can be transferred (after encryption) to another datacenter, or Amazon S3. An RDB maximizes the performance of Redis: The only thing the parent process has to do when saving an Rdb file is to fork out a child process, and the child process will handle all subsequent save work, and the parent process does not have to perform any disk I/O operations. An RDB recovers a large data set faster than AOF.




The disadvantage of the RDB:
If you need to avoid data loss in the event of a server failure, the RDB is not for you. Although Redis allows you to set different savepoint (save point) to control how often an RDB file is saved, it is not an easy operation because the Rdb file needs to preserve the state of the entire dataset. Therefore, you may be able to save the RDB file at least 5 minutes. In this case, you may lose several minutes of data in the event of a failure. Each time you save an RDB, Redis will fork () out a subprocess and perform the actual persistence work by the child process. When datasets are large, fork () can be time-consuming, causing the server to stop processing client;  within a millisecond if the data set is huge and CPU time is very tight, the stop time may even be a full second. Although the AOF rewrite also requires fork (), there is no loss of data durability regardless of the length of the AOF rewrite execution interval.




AOF Benefits:
Using AOF persistence makes Redis very durable (much more durable): You can set different fsync policies, such as no Fsync, Fsync per second, or every time you write a command Fsyn C. The default policy for AOF is Fsync once per second, in which Redis can still maintain good performance and, even if there is a failure, it will lose at most one second of data (Fsync will execute in a background thread, so the main thread can continue to work on the command request). The AOF file is an append-only log file (append only), so the write to the AOF file does not require seek, even if the log contains incomplete commands for some reason (such as a write-in disk full, write-down, etc.), The Redis-check-aof tool can also easily fix this problem.
Redis can automatically override AOF in the background when the AOF file volume becomes too large: The rewritten new AOF file contains the minimum set of commands required to restore the current dataset. The entire rewrite operation is absolutely secure, as Redis will continue to append commands to the existing AOF file during the creation of the new AOF file, and the existing AOF files will not be lost even if there is an outage during the rewrite. Once the new AOF file is created, Redis switches from the old AOF file to the new AOF file and begins appending to the new AOF file. The AOF file preserves all write operations performed on the database in an orderly manner, and these writes are saved in the format of the Redis protocol, so the contents of the AOF file are easily readable and parsing (parse) is easy. The export AOF file is also very simple: for example, if you accidentally executed the Flushall command, but as long as the AOF file is not rewritten, just stop the server, remove the Flushall command at the end of the AOF file, and restart Redis, you can The data set reverts to the state before Flushall execution.




Disadvantages of AOF:
For the same dataset, the volume of the AOF file is usually larger than the size of the RDB file. Depending on the Fsync policy you are using, the AOF may be slower than the RDB. In general, the performance of Fsync per second is still very high, while closing fsync can make AOF as fast as an RDB, even under high load. However, the RDB can provide a more guaranteed maximum delay time (latency) when handling large write loads. AOF has been a bug in the past: Because of the reason of individual commands, the AOF file cannot be restored as it was when the data set was reloaded. (for example, a blocking command Brpoplpush has caused such a bug.) The test suite adds tests to this situation: they automatically generate random, complex datasets, and re-load the data to make sure everything is fine. Although this bug is not common in AOF files, it is almost impossible for an RDB to appear in comparison.



RDB and AOF, which one should I use?
In general, if you want to achieve data security that is comparable to PostgreSQL, you should use two persistence features at the same time. If you are very concerned about your data, but can still tolerate data loss within a few minutes, you can use only the RDB persistence. Many users use AOF persistence only, but we do not recommend this approach: because the scheduled generation of an Rdb snapshot (snapshot) is very convenient for database backups, and the RDB recovers the data set faster than the AOF recovery, the use of an RDB also avoids the previously mentioned The bug of the AOF program. Because of the various reasons mentioned above, we may integrate AOF and RDB into a single persistence model in the future. (This is a long-term plan.) )



RDB Snapshot:
By default, Redis saves a database snapshot in a binary file named Dump.rdb. You can set up Redis to automatically save a dataset once the condition that the dataset has at least M changes in N seconds is met. You can also manually let Redis do the dataset save operation by calling Save or BGSAVE. For example, the following settings will allow Redis to automatically save a dataset when it satisfies the condition "at least 1000 keys are changed in 60 seconds":
Save 60 1000
This persistence is called snapshot (snapshot).

How the Snapshot works:
When Redis needs to save the Dump.rdb file, the server performs the following actions:
Redis calls fork (), with both parent and child processes.
The child process writes the dataset to a temporary RDB file.
When a child process finishes 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 enables Redis to benefit from the write-time replication (copy-on-write) mechanism.
File only for append operation (append-only file,aof)
The snapshot feature is not very durable (durable): If Redis causes downtime for some reason, the server loses the data that was recently written and is still not saved to the snapshot. While the durability of data is not the most important consideration for some programs, the snapshot feature is less applicable for programs that pursue full durability.
Starting with version 1.1, Redis adds a completely durable way to persist: AOF persistence.
You can open the AOF feature by modifying the configuration file:
AppendOnly Yes
From now on, whenever Redis executes a command that changes the dataset (such as set), the command is appended to the end of the AOF file.
That way, when Redis restarts, the program can rebuild the dataset by re-executing the commands in the AOF file.



AOF rewrite:
Because AOF works by constantly appending commands to the end of a file, the volume of the AOF file becomes larger as the write command grows. For example, if you call a counter 100 times INCR, then just to save the current value of the counter, AOF file will need to use 100 records (entry). In practice, however, using only one SET command is sufficient to hold the current value of the counter, and the remaining 99 records are actually superfluous. To handle this situation, Redis supports an interesting feature: You can rebuild the AOF file without interrupting the service client (rebuild). To execute the bgrewriteaof command, Redis will generate a new AOF file that contains the fewest commands required to rebuild the current dataset.



How durable is AOF?
You can configure how long Redis will fsync data to disk once.
There are three options:
Each time a new command is appended to the AOF file, the Fsync is executed once: very slow and very secure.
Fsync per second: fast enough (almost as long as the RDB is persisted), and only 1 seconds of data are lost in the case of a failure.
Never Fsync: Give the data to the operating system for processing. Faster and less secure choice.
The recommended (and also the default) measure is Fsync per second, and this fsync strategy can take into account speed and security.
The strategy of always fsync is very slow in practice, even after Redis 2.0 has made improvements to the related programs-frequent calls to Fsync are destined to make this strategy impossible to get up to.



What if the AOF file is wrong? 

The server may be down while the program is writing to the AOF file, and if the outage causes a AOF file error (corrupt), Redis will refuse to load the AOF file when it restarts, ensuring that data consistency is not compromised.


When this happens, you can fix the AOF file with the error:
Create a backup of the existing AOF file.
Use the REDIS-CHECK-AOF program that came with Redis to fix the original aof file.
$ redis-check-aof--fix
(optional) Use Diff-u to compare the backup of the repaired AoF file and the original aof file to see the differences between the two files.
Restart the Redis server, wait for the server to load the repaired AOF file, and perform data recovery.
How AOF works
AOF rewrite and RDB create snapshots, all subtly exploit the write-time replication mechanism.



The following steps are performed by the AOF override:
Redis executes fork () and now has both parent and child processes. The
child process begins writing the contents of the new AOF file to a temporary file. For all newly executed write commands, the parent process accumulates them into a memory cache while appending these changes to the end of the existing AOF file: This allows the existing AOF files to be safe even if there is an outage in the middle of the rewrite. When a child process finishes rewriting work, it sends a signal to the parent process that the parent process appends all the data in the memory cache to the end of the new AOF file after it receives the signal. Now Redis atomically replaces the old file with the new file, and all commands are appended directly to the end of the new AOF file.

To create a backup of the most recent Dump.rdb file. The
puts the backup in a safe place. The
executes the following two commands:
redis-cli> config set appendonly yes
redis-cli> config set save ""
ensure that the number of keys for the database has not changed since the command was executed. The
ensures that the Write command is correctly appended to the end of the AOF file.
The first command executed in step 3 turns on the AOF feature: Redis blocks until the initial AOF file creation is complete, and Redis continues to process the command request and begins appending the write command to the end of the AOF file. The second command that
Step 3 executes is used to turn off the RDB feature. This step is optional, and you can use both the RDB and AOF persistence features if you want.
Don't forget to open the AOF feature in redis.conf! Otherwise, after the server restarts, the configuration previously set by Config set will be forgotten, and the program will start the server as it was originally configured.



Interaction between the RDB and the AOF:
In Redis with a version number greater than or equal to 2.4, the BGSAVE does not perform bgrewriteaof during execution. Conversely, in the course of bgrewriteaof execution, BGSAVE cannot be executed.
This prevents two Redis background processes from doing large amounts of I/O to the disk at the same time.
If BGSAVE is executing and the user invokes the BGREWRITEAOF command, the server will reply to the user an OK state and inform the user that Bgrewriteaof has been scheduled to execute: Once the BGSAVE is executed, bgrewriteaof will be formally started. When Redis is started, if both RDB persistence and AOF persistence are turned on, the program takes precedence over the AOF file to recover the dataset, because the data that is stored in the AOF file is usually the most complete.



To back up Redis data:
Redis is very friendly for data backup because you can copy an RDB file when the server is running: Once the Rdb file is created, no modifications are made. When the server is about to create a new Rdb file, it saves the contents of the file in a temporary file, and when the temporary file is written, the program uses atomic to replace the original Rdb file with a temporary file. This means that, whenever an RDB file is copied, it is absolutely safe.



The difference between the RDB and aof persistence for Redis [go]


Alibaba Cloud Hot Products

Elastic Compute Service (ECS) Dedicated Host (DDH) ApsaraDB RDS for MySQL (RDS) ApsaraDB for PolarDB(PolarDB) AnalyticDB for PostgreSQL (ADB for PG)
AnalyticDB for MySQL(ADB for MySQL) Data Transmission Service (DTS) Server Load Balancer (SLB) Global Accelerator (GA) Cloud Enterprise Network (CEN)
Object Storage Service (OSS) Content Delivery Network (CDN) Short Message Service (SMS) Container Service for Kubernetes (ACK) Data Lake Analytics (DLA)

ApsaraDB for Redis (Redis)

ApsaraDB for MongoDB (MongoDB) NAT Gateway VPN Gateway Cloud Firewall
Anti-DDoS Web Application Firewall (WAF) Log Service DataWorks MaxCompute
Elastic MapReduce (EMR) Elasticsearch

Alibaba Cloud Free Trail

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.