Redis Combat (eight) the persistence of Redis

Source: Internet
Author: User

One of the big advantages of Redis over memcached is that it supports data persistence ,

One of the most persistent scenarios is to do database use, and the other is to prevent the cache from invalidating when Redis is doing cache servers.

The persistence of Redis is mainly two ways of snapshot snapshotting and aof log files .

The former will persist the in-memory data to the hard disk according to the configured rules,

The latter records the command after each execution of the write command.

>>rdb Way

Redis persists data to disk as a snapshot.

By default, the snapshot files are stored in the Dump.rdb file in the working directory of the Redis current process.

You can specify the storage path and file name of the snapshot file by using the Dir and dbfilename two parameters in the configuration file, respectively.

How to perform a snapshot save

Redis uses the fork function to copy a copy of the current process (the parent process) (the child process);
The parent process continues to process requests from the client, and the child process begins to write the in-memory data to the temporary file on the hard disk;
Once the child process has finished writing all the data, replacing the old Rdb file with the temporary file, a snapshot operation is completed.
It is important to note that:
When the fork is executed, the operating system (Unix-like operating system) uses a write-time copy (copy-on-write) policy,
That is, the moment the fork function occurs, the parent process and the child process share the same block of memory data, and when the parent process needs to modify one of the pieces of data, such as executing a write command,
The operating system copies the piece of data to ensure that the child process is not affected, so the Rdb file stores the memory data at the moment the fork operation is performed.
So the RDB approach is theoretically a case of data loss (those modified after fork are not written into the Rdb file).
An RDB file is a compressed binary file, so the space used is less than the size of the in-memory data and is more advantageous for transmission.
When Redis starts, the Rdb snapshot files are automatically read and the data is loaded into memory from the hard disk.

Rules for Redis execution snapshots

1. Automatic snapshots based on configuration rules

The following is the default snapshot save configuration:
Save 1 #900秒内如果超过1个key被修改 to initiate a snapshot save
Save #300秒内如果超过10个key被修改, the snapshot save is initiated
Save 10000 #60秒内如果超过10000个key被修改, initiate a snapshot save
Each snapshot condition is | | The relationship that satisfies a system will be executed.

2. User executes save, bgsave command
When you execute the Save command, Redis synchronizes the snapshot operation, which blocks all requests from the client;
When executing the bgsave command, this command is performed asynchronously in the background, while the snapshot operation can also handle requests from the client.

3. When performing replication (replication)
When using multiple servers, REDIS provides replication capabilities that enable automatic synchronization. The same principle of replication is the use of snapshots,
The primary database saves the snapshot in the background, caches the client request commands during the snapshot, and sends the snapshot files and cache commands to the slave database when the snapshot is complete, thus synchronizing the data.

>>aof Way

By default, Redis does not have the AOF mode enabled to view the configuration file:

AppendOnly No

The appendonly parameter needs to be turned on in the configuration file:

AppendOnly Yes

When on, Redis writes the command to the AoF file on the hard disk each time a write command is executed.

aof files and Rdb files under the same path, you can modify the configuration with the Appendonlyfilename parameter:

# The name of the Append only file (default: "Appendonly.aof")
Appendfilename "Appendonly.aof"

The realization of AOF

AOF records the write commands that Redis executes in plain text, and after I modify the above configuration, restart Redis and execute the following command:
redis> set Key1 value1
OK
redis> set Key2 value2
OK
redis> set Key1 value3
OK
Then look at the files under the Redis path:
[email protected]:/data/redis-3.0.3$ cat appendonly.aof
Set
$4
Key1
$6
value1
*
$
Set
$4
Key2
$6
value2
*
$
Set
$4
Key1
$6
Value3

Redis rewrite

Note that I did a overwrite operation above and was recorded by the AoF file,
It is a waste of resources to use the data that has been repeatedly modified in the actual usage.
Redis can purge invalid commands in the AoF file, that is, Redis overrides, and you can configure rewrite rules in the configuration file:

# Automatic Rewrite of the append only file.
Auto-aof-rewrite-percentage 100
Auto-aof-rewrite-min-size 64MB

The two items are described in the configuration file:

# Redis is able to automatically rewrite the log file implicitly calling
# bgrewriteaof When the AOF log size grows by the specified percentage.
# This is what it Works:redis remembers the size of the AOF file after the
# Latest Rewrite (if no rewrite has happened since the restart, the size of
# The AOF at startup is used).
# This base size is compared to the current size. If the current size is
# Bigger than the specified percentage, the rewrite is triggered. Also
# need to specify a minimal size for the AOF file to being rewritten, this
# is useful to avoid rewriting the AOF file even if the percentage increase
# is reached and it is still pretty small.
# Specify a percentage of zero in order to disable the automatic AOF
# rewrite feature.

That is, the Redis rewrite action is triggered when the aof file size exceeds the number of files that were last rewritten, and the AoF file is larger than the configured minimum.

You can also execute the bgrewriteaof command, manually override it,

I did this once, and then I looked at the aof file:
set
$4
key2
$6
value2
*3
$3
set
$4
key1
$6
value3

Although the AoF file is recorded every time a change is made, it is actually due to the operating system's caching mechanism,
By default, the system performs a synchronization every 30 seconds, and the data in the hard disk cache is actually written to the hard disk.
This part of the data cannot be saved if an application exception occurs.

Redis can configure rules to perform synchronization operations when writing to a aof file:

# Appendfsync always performs a synchronous operation on every write
Appendfsync everysec synchronization per second
# Appendfsync No does not actively synchronize

It is generally sufficient to synchronize every second, which is also the default rule for Redis.

Redis Combat (eight) the persistence of Redis

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.