Best selections for Redis optimization

Source: Internet
Author: User
Tags compact

Redis is a single-threaded, in-memory database. As follows:
Http://download.redis.io/releases/redis-2.8.11.tar.gz
Run the make command in the Redis src directory, and then copy the executable file to the new bin directory. The following forms of organization can be used.


Redis persistence has two ways of Rdb and aof

1.RDB mode

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

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

RDB is ideal for disaster recovery (disaster recovery): It has only one file, and the content is very compact

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.

However, when the Rdb method fails, there is no guarantee that data will not be lost.


2.AOF mode

Using AOF persistence makes Redis very durable (much more durable): You can set different fsync policies, such as no fsync, one fsync per second, or fsync each time you write a command. The default policy for AOF is Fsync once per second, and in this configuration, Redis can still maintain good performance and will lose up to one second of data, even in the event of a failure outage.

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 size 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.

The disadvantage of aof is that the volume is usually larger than the Rdb file. The recovery time is also longer than the Rdb method.

If you turn on logging every time you write, instead of logging logs once per second, performance can degrade. However, if you write every second, the last second of data will be lost once the failure occurs.

The following program can be used for simple testing.


Set the Redis parameter to Appendfsync everysec

It takes about 4s to write 20w data



Set the Redis parameter to Appendfsync always

To write the same 20W data, you need about 400s



Write performance is reduced by 100 times times if the data is guaranteed to be absolutely secure.

If you are writing a dense system, you need to consider it carefully. Because Redis is a single-threaded program.


3.RDB Migrating to AoF

The RDB is the default persistence mode for Redis.

The experiment assumes that the program runs in the RDB mode and has some data. Need to switch to AOF mode.


Initialize Redis running in RDB mode with the following configuration parameters:



Login to Redis and enter config set appendonly yes.

This 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.




You can see that the aof file is larger than the Rdb file.

Append the AOF setting in the configuration file, otherwise the next boot will fail.


If you start the AOF mode, you can turn off the RDB mode.

The shutdown method is as follows, emptying the save configuration and removing the configuration from the configuration file


Migrating an RDB to aof must not be done in the following way, so that all data is lost

    1. Configuring the AOF parameter in the configuration file

    1. Normal Stop Redis

    1. Start Redis

    1. We're going to be shocked to find the data gone.


The verification process is as follows

Start with an RDB approach, with 20W of data.



Modify the configuration file to increase the AOF configuration

AppendOnly Yes

Appendfilename appendonly.aof

Appendfsync always

Then gracefully turn off Redis


You can see that the Rdb file size is 2M

Start Redis. You can see that all database data has been emptied.


Don't panic at this time, MV Dump.rdb to the backup location.

If it shuts down again, it's tragic.


After the normal shutdown, the Redis Dump.rdb has been cleared.

I guess this is because Redis starts with aof over the RDB, but there is no data in the first use of aof.
If Redis is turned off, Redis snapshots with no data will be taken to Dump.rdb and the Dump.rdb file with the original data will be overwritten.
So, to change Redis from RDB to aof mode, make sure to modify it online. This will write the memory data to the AOF file first so that it does not cause the data to be emptied.

Related documents:

Http://www.cnblogs.com/stephen-liu74/archive/2012/04/16/2370212.html
https://redis.readthedocs.org/en/latest/

Http://blog.91gaoqing.com/archives/217.html consistent Hash

Best selections for Redis optimization

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.