redis persistence (rdb, aof)

redis persistence in two ways

  1. RDB

                 RDB is redis default persistence persistent way, every so often refers to the data in memory in a binary fashion persisted to disk. Redis View profile redis.windows.conf

# 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, save command in three ways:

save 900 1: every 900s, and at least one key changed
save 300 10: every 300s, and at least 10 key changed
save 60 10000: every 60s, and at least 10,000 key changed


Meet any one of these three cases, redis will be persistent, generate .rdb file in the default path below

# 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 ./rdbfile

 

In addition to redis default mode, it can also perform the save, bgsave command client manually

save: redis service is blocked, can not respond to requests from other clients until the persistence to complete.

bgsave: That background save, redis will fork a child process to complete the work of persistent data. Redis will not affect the blocking service

redis restart when it will be back to work by .rdb data files automatically.

 

 

       2.AOF

            AOF are either write by way of recording redis files, delete operation (operating, but not the data), queries are not recorded. Files can be seen in the detailed operation of the recording, e.g.

*2
$6
SELECT
$1
0
*3
$3
SET
$1
?
$1
1

By default, AOF of feature is turned off, the path .aof file can be specified in the configuration file

#
# The Append Only File is an alternative persistence mode that provides
# much better durability. For instance using the default data fsync policy
# (see later in the config file) Redis can lose just one second of writes in a
# dramatic event like a server power outage, or a single write if something
# wrong with the Redis process itself happens, but the operating system is
# still running correctly.
#
# AOF and RDB persistence can be enabled at the same time without problems.
# If the AOF is enabled on startup Redis will load the AOF, that is the file
# with the better durability guarantees.
#
# Please check http://redis.io/topics/persistence for more information.

appendonly no

# The name of the append only file (default: "appendonly.aof")
appendfilename "appendonly.aof"

AOF persistence can be configured appendfsync attributes:

appendfsync no: operating system decides when to persist, faster

appendfsync always: after each write operation, for persistence, higher security, speed is relatively slow

appendfsync everysec: default configuration, once every second persistence, compared to the above two methods, compromise efficiency

# The fsync() call tells the Operating System to actually write data on disk
# instead of waiting for more data in the output buffer. Some OS will really flush
# data on disk, some other OS will just try to do it ASAP.
#
# Redis supports three different modes:
#
# no: don't fsync, just let the OS flush the data when it wants. Faster.
# always: fsync after every write to the append only log. Slow, Safest.
# everysec: fsync only one time every second. Compromise.
#
# The default is "everysec", as that's usually the right compromise between
# speed and data safety. It's up to you to understand if you can relax this to
# "no" that will let the operating system flush the output buffer when
# it wants, for better performances (but if you can live with the idea of
# some data loss consider the default persistence mode that's snapshotting),
# or on the contrary, use "always" that's very slow but a bit safer than
# everysec.
#
# 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

The client can perform manual bgrewirteaof command AOF persistence, generate a appendonly.aof (default) file. redis start time will give priority to recover data from appendonly.aof in.

AOF overwriting files:

 Open AOF time, over time, aof documents will become increasingly large, redis in certain conditions are met will be bgrewirteaof, optimize the file to reduce the file size, conducive to the restoration of data

# Automatic rewrite of the append only 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 how 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
# you need to specify a minimal size for the AOF file to be rewritten, this
# is useful to avoid rewriting the AOF file even if the percentage increase
# is reached but it is still pretty small.
#
# Specify a percentage of zero in order to disable the automatic AOF
# rewrite feature.

auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb

 

 

Compare RDB and AOF in two ways:

    1.RDB record data in binary mode, AOF in text mode recording operation

    Security 2.AOF higher than RDB, RDB is a time lag for persistence, redis between failure if persistent, data loss can occur. So this approach is more suitable for data requirements are not particularly stringent time

    3.AOF files are typically large files than RDB, RDB to recover more slowly than the way

 

 

Related command:

  info persistence

  bgsave

  save

  bgrewriteaof

 

Guess you like

Origin www.cnblogs.com/chenzhubing/p/11346106.html