Redis Learning--Persistent data backup (RDB and AOF)

Source: Internet
Author: User
Tags lzf disk usage redis desktop manager redis server


Next: Installation of the Redis,redis Visual management tool (Redis Desktop Manager) under Windows Setup, Basic use, instantiation of the project






I. How the Dump.rdb file was generated



Ii. What is Redis persistence



Iii. What is the RDB for Redis?



Iv. Redis configuration file redis.config related configuration



V. Advantages of Redis



Vi. shortcomings of Redis






Redis is more powerful than memcache as a cache database: (1) More data types are supported, (2) Redis persistence.






I. How the Dump.rdb file was generated



When the Redis service is hung up, data is automatically backed up locally, depending on the Redis configuration file.



The DUMP.RDB is automatically generated by the Redis server.



By default, the Redis server program automatically iterates through the database every once in a while and writes memory snapshots in a file called "Dump.rdb", a persistence mechanism called snapshot. With snapshot, if the server goes down and the Redis server program restarts, Redis automatically loads the DUMP.RDB and restores the database to the last snapshot state.






How often do you do it once? The path and file name of the Snapshot,snapshot file, which you can specify in the Redis config file.






Second, what is the persistence of Redis



Redis provides different levels of persistence:



(1) RDB persistence mode: Ability to snapshot your data at specified intervals.



(2) aof persistence mode: Each operation to the server, when the server restarts the time to re-execute these commands to restore the original data, the AOF command appends the Redis protocol to save each write operation to the end of the file. Redis also enables background rewriting of aof files to make the aof file less bulky.



(3) If you only want your data to exist when the server is running, you can not use any persistence mode.



(4) You can also open both of these persistence modes at the same time. When the Redis service restarts, it will load the aof file to restore the original data, since the data set saved by the AoF file is generally more complete than the data set saved by the Rdb file.






Iii. What is the RDB for Redis



The RDB writes a snapshot of the in-memory dataset to disk, which is the snapshot snapshot, within a specified time interval .



It recovers when the snapshot files are written directly into memory, and Redis creates (fork) a child process for persistence, writes the data to a temporary file, and the persistence process ends with the temporary file replaced with a file that is persisted.



Throughout the process, the main process is not performing any IO operations, which ensures very high performance. If large-scale data recovery is required and the integrity of the data recovery is not sensitive, the RDB approach is more efficient than the AOF approach. 、



The disadvantage of Redis is that the last persisted data may be lost.






Iv. Redis configuration file redis.config related configuration



(i) Rdb snapshot mode persistent disk



See Redis.window.config file First


 
################################ SNAPSHOTTING  ################################
#
# Save the DB on disk:
#
#   save <seconds> <changes> #
#   Will save the DB if both the given number of seconds and the given
#   number of write operations against the DB occurred.
#
#   In the example below the behaviour will be to save:
#   after 900 sec (15 min) if at least 1 key changed
#   after 300 sec (5 min) if at least 10 keys changed
#   after 60 sec if at least 10000 keys changed
#
#   Note: you can disable saving completely by commenting out all "save" lines.
#
#   It is also possible to remove all the previously configured save
#   points by adding a save directive with a single empty string argument
#   like in the following example:
#
#   save "" save 900 1 save 300 10 save 60 10000 # By default Redis will stop accepting writes if RDB snapshots are enabled
# (at least one save point) and the latest background save failed.
# This will make the user aware (in a hard way) that data is not persisting
# on disk properly, otherwise chances are that no one will notice and some
# disaster will happen.
#
# If the background saving process will start working again Redis will
# automatically allow writes again.
#
# However if you have setup your proper monitoring of the Redis server
# and persistence, you may want to disable this feature so that Redis will
# continue to work as usual even if there are problems with disk,
# permissions, and so forth.
stop-writes-on-bgsave-error yes

# Compress string objects using LZF when dump .rdb databases? # For default that's set to 'yes' as it's almost always a win.
# If you want to save some CPU in the saving child set it to 'no' but
# the dataset will likely be bigger if you have compressible values or keys.
rdbcompression yes

# Since version 5 of RDB a CRC64 checksum is placed at the end of the file.
# This makes the format more resistant to corruption but there is a performance
# hit to pay (around 10%) when saving and loading RDB files, so you can disable it
# for maximum performances.
#
# RDB files created with checksum disabled have a checksum of zero that will
# tell the loading code to skip the check.
rdbchecksum yes

# The filename where to dump the DB
dbfilename dump.rdb

# The working directory.
#
# The DB will be written inside this directory, with the filename specified
# above using the 'dbfilename' configuration directive.
#
# The Append Only File will also be created inside this directory.
#
# Note that you must specify a directory here, not a file name.
dir ./





4.1 How to trigger an RDB snapshot



Default snapshot configuration in a configuration file


save 900 1
save 300 10
save 60 10000


The above meaning means:



(1) If at least one key is changed, the save operation is performed after 900 seconds (15 minutes)



(2) If at least 10 keys are changed, the save operation is performed after 300 seconds (5 minutes)



(3) If at least 10,000 keys are changed, the save operation is performed after 60 seconds (1 minutes)



Command Save: Save, whatever



The command Bgsave:redis asynchronously snapshot operations in the background, while the snapshot can also respond to client requests.






4.2 The default Rdb method holds the Dump.rdb file, and recovery recognition is also DUMP.RDB






4.3stop-writes-on-bgsave-error Yes



If an error occurs in the background save to disk, the write operation is stopped, and the RDB file is compressed using LZF, which consumes the CPU, but can reduce disk usage.






4.4rdbcompression Yes



when you save an RDB and load an RDB file, the checksum prevents errors, but it takes about 10% performance to shut down and improve performance.






4.5rdbchecksum Yes



Exported RDB file name






4.6dbfilename Dump.rdb



set the working directory, the Rdb file will be written to the directory , append only file will be stored in the directory






4.7dir./



Redis will automatically save the snapshot to disk or call Bgsave, which is done by the background process, the other clients can read and write the Redis service, the background to save the snapshot to disk will consume a lot of memory.






(ii) Persistence of AOF (append-only file) mode



Another way is to increment the way that will cause data to change the operation, persist to the file, restart Redis, by Operation Command, restore the data.



Each time the write command is executed, the data is written to Server.aofbuf.


 
#
# More details please check the following article:
# http://antirez.com/post/redis-persistence-demystified.html #
# If unsure, use "everysec".

# appendfsync always appendfsync everysec # appendfsync no

# When the AOF fsync policy is set to always or everysec, and a background
# saving process (a background save or AOF log background rewriting) is
# performing a lot of I/O against the disk, in some Linux configurations
# Redis may block too long on the fsync() call. Note that there is no fix for 
# this currently, as even performing fsync in a different thread will block
# our synchronous write(2) call.


When configured as always, each time the data in the SERVER.AOFBUF is written to a file, it is returned to the client, which guarantees that the data is not lost, but frequent IO operations can degrade performance.



Everysec writes once per second, which can lose operations within one second.






V. Advantages of Redis



(1) Suitable for large-scale data recovery



(2) The data integrity and consistency requirements are not high






Vi. shortcomings of Redis



(1) Make a backup at a certain interval, so if Redis accidentally hangs up, it loses all modifications after the last snapshot.



(2) Fork, the data in memory is cloned, roughly twice times the expansion of the need to consider





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.