春招必問的redis持久化(RDB AOF),你能答上來麼?

 

  面試的大體流程:

  第一步:一般會有筆試題,也可能沒有。有筆試題就要好好答了,因爲會重視筆試結果,爲了節約面試官時間,HR可能先會看,不合格直接讓你走人了。

  第二步:開始面試,面試官會讓你先來個自我介紹,他在看你的簡歷。可能有人會有疑問?爲什麼簡歷上都寫的很清楚還讓我做自我介紹?因爲面試官不會提前看你的簡歷,他要有時間去看簡歷。所以自我介紹,不宜過長或過短,要重點突出,怎麼重點突出?提前看好崗位要求,要求都是入職之後用到的技術,所以面試官會看重那些!!!自我介紹結束,步入正題。

  面試官會問:之前的公司用過redis麼?

  面試者有兩類回答:

  面試者甲:沒用過,但自己學過,下載過源碼,自己部署安裝過,基本的命令像string/hash/lsit/set/zset,都熟悉;

  面試官(心想雖然沒用,但動手能力很強,也很好學,不錯):redis是內存數據庫,那它怎麼進行持久化的?

  面試官甲(心想沒注意看啊,不知道啊):...

 

  面試者乙:用過,比較熟悉

  面試官會接着問:持久化方式有哪些?

  面試者乙:RDB和AOF

  面試官:原理?區別?優缺點說一下吧?

  面試官乙:...

  要是是你去面試,你能回答上來麼?

  下面讓我爲你一一揭曉答案!!!

  RDB持久化(Redis DataBase)

  redis是內存數據庫,一旦服務器進程退出,服務器中的數據庫狀態也會消失不見。重點介紹save和bgsave命令。

  RDB文件的創建與載入

  save命令會阻塞Redis服務器進程,直到RDB文件創建爲止,在服務器進程阻塞期間,服務器不能處理任何命令請求。

  bgsave命令派生出子進程,,然後由子進程創建RDB文件,父進程繼續處理請求。

  創建RDB文件實際rdb.c/rdbLoad函數完成的

  自動間隔性保存

  redis服務器會通過用戶配置save選項,每隔一段時間去執行一下bgsave命令;默認的配置文件redis.conf,關於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 1save 300 10save 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 DBdbfilename 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 ./
rdb

  主要有三條策略,滿足任意一個,就會執行bgave命令:

  服務器在900秒之內,對數據庫至少修改了1次

  服務器在300秒之內,對數據庫至少修改了10次

  服務器在60秒之內,對數據庫至少修改了10000次

  優缺點

  優點:

  適合大規模的數據恢復

  對數據完整性和一致性要求不高

  缺點:

  會丟失最後一次修改的數據

  fork會產生額外消耗

  AOF持久化(Append Only File)

  與RDB通過鍵值對來記錄數據庫狀態不同,AOF是通過Redis服務器所執行的寫命令來記錄數據庫狀態的。

  AOF持久化的實現

  AOF持久化功能的實現可以分爲命令追加(append)、文件寫入、文件同步(sync)三個步驟。

  aof在redis.conf配置文件的:

  

############################## APPEND ONLY MODE ###############################
# By default Redis asynchronously dumps the dataset on disk. This mode is# good enough in many applications, but an issue with the Redis process or# a power outage may result into a few minutes of writes lost (depending on# the configured save points).## 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"
# 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 alwaysappendfsync 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.## In order to mitigate this problem it's possible to use the following option# that will prevent fsync() from being called in the main process while a# BGSAVE or BGREWRITEAOF is in progress.## This means that while another child is saving, the durability of Redis is# the same as "appendfsync none". In practical terms, this means that it is# possible to lose up to 30 seconds of log in the worst scenario (with the# default Linux settings).## If you have latency problems turn this to "yes". Otherwise leave it as# "no" that is the safest pick from the point of view of durability.
no-appendfsync-on-rewrite no
# 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
# Specify a percentage of zero in order to disable the automatic AOF# rewrite feature.
auto-aof-rewrite-percentage 100auto-aof-rewrite-min-size 64mb
# An AOF file may be found to be truncated at the end during the Redis# startup process, when the AOF data gets loaded back into memory.# This may happen when the system where Redis is running# crashes, especially when an ext4 filesystem is mounted without the# data=ordered option (however this can't happen when Redis itself# crashes or aborts but the operating system still works correctly).## Redis can either exit with an error when this happens, or load as much# data as possible (the default now) and start if the AOF file is found# to be truncated at the end. The following option controls this behavior.## If aof-load-truncated is set to yes, a truncated AOF file is loaded and# the Redis server starts emitting a log to inform the user of the event.# Otherwise if the option is set to no, the server aborts with an error# and refuses to start. When the option is set to no, the user requires# to fix the AOF file using the "redis-check-aof" utility before to restart# the server.## Note that if the AOF file will be found to be corrupted in the middle# the server will still exit with an error. This option only applies when# Redis will try to read more data from the AOF file but not enough bytes# will be found.aof-load-truncated yes
aof

 

  AOF重寫

  隨着服務器時間的流逝,文件的體積越來越大,體積過大的AOF文件對redis服務器、甚至整個宿主計算機造成影響。並且AOF文件的體積越大,使用AOF文件進行數據還原所需的時間越多。

  爲了解決AOF文件體重膨脹的問題,redis提供了AOF文件重寫(rewrite)的功能。

  觸發機制

  redis會記錄上次重寫時AOF的大小,默認配置是當AOF文件大小是上次rewrite後大小的一倍且大於64M;默認配置如下:  

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

  悄悄告訴你們個小祕密:可能在面試時,面試官會吹噓公司很牛,redis用的出神入化,當你入職之後,可以悄悄看看“auto-aof-rewrite-min-size 64mb”,默認64M大小,根本不夠用,告訴發展的公司起碼要3G起。

  優缺點

  優點:

  配置靈活,可以選擇多種方式進行持久化

  缺點:  

  相同數據集的數據而言,aof文件要遠大於rdb文件,恢復速度慢於rdb

 

發佈了236 篇原創文章 · 獲贊 127 · 訪問量 35萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章