持久化操作-RDB(Redis Data Base)

1. RDB是什么

在指定的时间间隔内将内存的数据集快照写入磁盘,它恢复时是将快照文件直接读到内存中

2. RDB如何执行?

Redis会单独创建(fork)一个子进程来进行持久化,会将数据写入到一个临时文件中,待持久化过程都结束了,再用这个临时文件替换上次持久化好的文件整个过程中,主进程是不进行任何IO操作的,这就确保了极高的性能如果需要进行大规模数据恢复,且对于数据恢复的完整性不是非常敏感,那RDB方式要比AOF方式更加的高效.
RDB的缺点是最后一次持久化后的数据可能会丢失

3. Fork

  • Fork的作用是复制一个与当前进程一样的进程。新进程的所有的数据(变量。环境变量、程序计数器等)数值和原进程一致,但是是一个全新的进程,并作为原进程的子进程
  • 在Linux进程中,fork()会产生一个和父进程完全相同的子进程,但子进程在此后多会exec系统调用,出于效率考虑,Linux中引入了“写时复制技术”(先复制再写入)
  • 一般情况父进程和子进程都会共用同一段物理内存,只有进程空间的各段的内容要发生改变时,才会将父进程的内容复制一份给子进程

4.Redis.config配置文件中关于RDB持久化的相关配置

################################ 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  # Redis无法写入磁盘的话,直接关闭Redis的写操作,

# 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  # 是否压缩rdb文件

# 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  # 是否检查文件的完整性,这个需要耗费10%的性能,但还是很有必要开启

# The filename where to dump the DB
dbfilename dump.rdb  # 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 ./  # rdb文件储存的位置

5. 优势

  • 适合大规模的数据恢复
  • 对数据完整性和一致性要求不高更适合使用
  • 节省磁盘空间
  • 恢复速度快
    image

6. 劣势

  • Fork的时候,内存中的数据被克隆了一份,大致2倍的膨胀性需要考虑
  • 虽然Redis在fork时使用了写时拷贝技术,但是数据庞大时还是比较消耗性能
  • 在备份周期在一定时间做一份备份,所以如果Redis意外down掉的话,就会丢失最后一次快照后的所有修改
posted @ 2022-06-22 11:25  小罗要有出息  阅读(176)  评论(0)    收藏  举报