redis學習--的持久化資料備份(RDB和AOF)

來源:互聯網
上載者:User

接上一篇:安裝window下的redis,redis可視化管理工具(Redis Desktop Manager)安裝,基礎使用,執行個體化項目

 

一、dump.rdb檔案是怎麼產生的

二、什麼是redis持久化

三、redis的RDB是什嗎?

四、redis設定檔redis.config相關配置

五、redis優點

六、redis缺點

 

redismemcache作為快取資料庫強大的地方:(1)支援資料類型比較多,(2)redis持久化功能。

 

一、dump.rdb檔案是怎麼產生的

在redis服務掛掉的時候,根據redis的設定檔,會自動備份資料到本地。

dump.rdb是由redis伺服器自動產生的。

預設情況下,每隔一段時間redis伺服器程式會自動對資料庫做一次遍曆,把記憶體快照寫在一個叫做“dump.rdb”檔案裡,這種持久化機制叫做SNAPSHOT。有了SNAPSHOT後,如果伺服器宕機,重新啟動redis伺服器程式時redis會自動載入dump.rdb,將資料庫恢複到上一次SNAPSHOT的狀態。

 

至於多久一次做一次SNAPSHOT,SNAPSHOT檔案的路徑和檔案名稱,你可以在redis的config檔案中指定。

 

二、什麼是redis的持久化

Redis提供了不同層級的持久化方式:

(1)RDB持久化方式:能夠在指定的時間間隔能對你的資料進行快照儲存

(2)AOF持久化方式:每次對伺服器寫的操作,當伺服器重啟的時候回重新執行這些命令來恢複原始的資料,AOF命令以redis協議追加儲存每次寫的操作到檔案末尾。redis還能對AOF檔案進行後台重寫,使得AOF檔案的體積不至於過大。

(3)如果你只希望你的資料在伺服器啟動並執行時候存在,你可以不使用任何持久化方式。

(4)你也可以同時開啟這兩種持久化方式。當redis服務重啟的時候回優先載入AOF檔案來恢複原始的資料,因為在通常情況下AOF檔案儲存的資料集要比RDB檔案儲存的資料集要完整

 

三、Redis的RDB是什麼

RDB在指定的時間間隔內將記憶體中的資料集快照寫入磁碟,也就是SNAPSHOT快照。

它恢複時是將快照檔案直接寫入到記憶體裡,redis會單獨建立(fork)一個子進程進行持久化嗎,會先將資料寫入到一個臨時檔案中,持久化過程都結束了,在用這個臨時檔案替換上從持久化好的檔案。

整個過程中,主進程是不進行任何IO操作的,這就確保了極高的效能。如果需要進行大規模資料的恢複,且對於資料恢複的完整性不是敏感,那RDB方式要比AOF方式更加高效。、

redis的缺點是最後一次持久化後的資料可能丟失。

 

四、redis設定檔redis.config相關配置

(一)RDB快照方式持久化磁碟

先看redis.window.config檔案

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

 

4.1如何觸發RDB快照

設定檔中預設的快照配置

save 900 1save 300 10save 60 10000

上面的意思就是說:

(1)如果至少一個鍵改變,會在900秒(15分鐘)之後執行save操作

(2)如果至少改變10個鍵,則在300秒(5分鐘)之後執行save操作

(3)如果至少改變10000個鍵,則在60秒(1分鐘)之後執行save操作

命令save:只管儲存,其他不管

命令bgsave:redis會在後台非同步進行快照操作,快照的同時還可以響應用戶端請求。

 

4.2預設的RDB方式儲存的是dump.rdb檔案,恢複識別也是dump.rdb

 

4.3stop-writes-on-bgsave-error yes

如果後台儲存到磁碟發生錯誤,將停止寫操作,使用LZF壓縮rdb檔案,這會耗CPU, 但是可以減少磁碟佔用。

 

4.4rdbcompression yes

儲存rdb和載入rdb檔案的時候校正,可以防止錯誤,但是要付出約10%的效能,可以關閉,提高效能。

 

4.5rdbchecksum yes

匯出的rdb檔案名稱

 

4.6dbfilename dump.rdb

設定工作目錄,rdb檔案會寫到該目錄,append only file也會儲存在該目錄下

 

4.7dir ./

redis會自動快照儲存到磁碟或者調用bgsave,是後台進程完成的,其他用戶端任然可以讀寫redis服務,後台儲存快照到磁碟會佔用大量的記憶體。

 

(二)AOF(append-only file)方式持久化

另外一種方式為遞增的方式,將會引起資料變化的操作,持久化到檔案中,重啟redis的時候,通過操作命令,恢複資料。

每次執行寫操作命令之後,都會將資料寫到server.aofbuf中。

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

當配置為always的時候,每次server.aofbuf中的資料寫入到檔案之後,才會返回到用戶端,這樣可以保證資料不丟失,但是頻繁的IO操作,會降低效能。

everysec每秒寫一次,這可能會丟失一秒內的操作。

 

五、redis優點

(1)適合大規模的資料恢複

(2)對資料完整性和一致性要求不高

 

六、redis缺點

(1)在一定間隔時間做一次備份,所以redis意外的掛掉的話,就會丟失最後一次快照後的所有修改

(2)fork的時候,記憶體中的資料被被複製一份,大致2倍的膨脹性需求考慮

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.