今天小編分享的是關(guān)于Redis持久化快照的方法與原理介紹,可能大家對Redis并不陌生,或者從來沒有了解過Redis。但是不用擔(dān)心,今天小編會以最簡單的描述來講解Redis的原理。
所謂的持久化就是保持我們的數(shù)據(jù)不丟失,將數(shù)據(jù)通常保存在我們的硬盤中。在Redis中持久化的方式有兩種,一種是快照持久化,一種是AOF持久化,各有各的優(yōu)缺點,在項目中我們得根據(jù)實際的情況來選擇具體的持久化方式。
快照持久化(RDB)
也叫RDB持久化方式,就是通過拍攝快照的方式實現(xiàn)持久化,將某個時間的內(nèi)存數(shù)據(jù)存儲在一個rdb文件中,在redis服務(wù)重新啟動的時候加載文件中的數(shù)據(jù)
配置持久化快照
redis中的快照持久化默認(rèn)是開啟的,在redis.conf配置文件中有相關(guān)的配置選項
################################ 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 #900秒內(nèi)至少有1個key被更改就執(zhí)行快照 save 300 10 #300內(nèi)描述至少有10個key被更改就執(zhí)行快照 save 60 10000 #60秒內(nèi)至少有10000個key被更改就執(zhí)行快照 # 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 #拍攝快照失敗是否繼續(xù)執(zhí)行寫命令 # 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 #是否進行數(shù)據(jù)校驗 # 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 /opt/redis-5.0.3#快照文件存儲的位置
驗證效果
1、進入安裝目錄,如果有dump.db文件就刪除
2、啟動redis,然后添加幾個數(shù)據(jù),然后關(guān)閉redis退出
[root@root redis-5.0.3]# src/redis-cli 127.0.0.1:6379> ping PONG 127.0.0.1:6379> set name aaa OK 127.0.0.1:6379> set age 18 OK 127.0.0.1:6379> incr age (integer) 19 127.0.0.1:6379> shutdown not connected> exit
3、在我們的安裝的目錄下就生成一個dump.rdb文件,這個就是我們的快照備份文件
4、再次啟動redis,進入發(fā)現(xiàn)原來的數(shù)據(jù)還在,這是因為redis啟動的時候加載了備份文件中的數(shù)據(jù)。
[root@root redis-5.0.3]# src/redis-server redis.conf 1211:C 13 Feb 2019 01:27:22.668 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 1211:C 13 Feb 2019 01:27:22.668 # Redis version=5.0.3, bits=64, commit=00000000, modified=0, pid=1211, just started 1211:C 13 Feb 2019 01:27:22.668 # Configuration loaded [root@root redis-5.0.3]# src/redis-cli 127.0.0.1:6379> ping PONG 127.0.0.1:6379> get name "aaa" 127.0.0.1:6379> get age "19" 127.0.0.1:6379> keys * 1) "name" 2) "age"
5、關(guān)閉退出
關(guān)閉退出后刪除dump.rdb文件,啟動后發(fā)現(xiàn)數(shù)據(jù)沒有了
[root@root redis-5.0.3]# src/redis-server redis.conf 1218:C 13 Feb 2019 01:29:01.336 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 1218:C 13 Feb 2019 01:29:01.336 # Redis version=5.0.3, bits=64, commit=00000000, modified=0, pid=1218, just started 1218:C 13 Feb 2019 01:29:01.336 # Configuration loaded [root@root redis-5.0.3]# src/redis-cli 127.0.0.1:6379> ping PONG 127.0.0.1:6379> keys * (empty list or set)
快照持久化原理
save命令:
在redis運行中,我們可以顯示的發(fā)送一條save命令來拍攝快照。save命令是阻塞命令,也就是當(dāng)服務(wù)器接收了一條save命令之后就會開始拍攝快照,在此期間不會再去處理其他的請求,其他請求會被掛起直到備份結(jié)束
bgsave命令
bgsave命令也是立即拍攝快照,有別于save命令,bgsave并不是一條阻塞命令,而是fork一個子線程,然后這個子線程負(fù)責(zé)備份操作。而父進程繼續(xù)處理客戶端的請求,這樣就不會造成阻塞了。
127.0.0.1:6379> ping PONG 127.0.0.1:6379> keys * (empty list or set) 127.0.0.1:6379> set name zhangsan OK 127.0.0.1:6379> set age 20 OK 127.0.0.1:6379> bgsave Background saving started
4、shutdown命令
當(dāng)我們只想shutdown命令的時候。服務(wù)器會自動發(fā)送一條save命令來完成快照操作。并在完成備份操作后關(guān)閉服務(wù)器。所以我們當(dāng)我們的操作不滿足前面三種情況的時候關(guān)閉服務(wù)器后,再次打開我們的數(shù)據(jù)也不會丟失。
5、sync命令
當(dāng)在主從環(huán)境中,從節(jié)點要同步主節(jié)點的數(shù)據(jù)的時候會發(fā)送一條sync命令來開發(fā)一次復(fù)制。此時主節(jié)點會發(fā)送一條bgsave命令來fork一個新的線程來完成快照并在bgsave命令操作結(jié)束后將快照文件發(fā)送給從節(jié)點來完成主從節(jié)點的數(shù)據(jù)的同步。
優(yōu)缺點
優(yōu)點
RDB文件保存了某個時間點的redis數(shù)據(jù),適合備份,你可以設(shè)定一個時間點對RDB文件進行歸檔,這樣就能在需要的時候很輕易的把數(shù)據(jù)恢復(fù)到不同的版本。 RDB很適合用于災(zāi)備。單文件很方便就能傳輸?shù)竭h(yuǎn)程的服務(wù)器上。在數(shù)據(jù)量比較打的情況下,RDB啟動速度快.
缺點
RDB容易造成數(shù)據(jù)丟失,如果設(shè)置3分鐘保存一次,如果redis因為一些原因不能正常工作,那么從上次產(chǎn)生快照到Redis出現(xiàn)問題這段時間的數(shù)據(jù)就會丟失了。
如何禁用快照持久化
1、在redis.conf配置文件中注釋掉所有的save配置 2.在最后一條save配置追加吃命令
save ""
AOF持久化
與快照持久化通過直接保存 Redis 的鍵值對數(shù)據(jù)不同,AOF 持久化是通過保存 Redis 執(zhí)行的寫命令來記錄 Redis 的內(nèi)存數(shù)據(jù)。理論上說,只要我們保存了所有可能修改 Redis 內(nèi)存數(shù)據(jù)的命令(也就是寫命令),那么根據(jù)這些保存的寫命令,我們可以重新恢復(fù) Redis 的內(nèi)存狀態(tài)。AOF 持久化正是利用這個原理來實現(xiàn)數(shù)據(jù)的持久化與數(shù)據(jù)的恢復(fù)的
開啟AOF
在redis中aof默認(rèn)是關(guān)閉的,我們需要修改配置文件開啟aof
appendonly yes appendfilename "appendonly.aof" # If unsure, use "everysec". # appendfsync always appendfsync everysec # appendfsync no no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb
關(guān)閉快照持久化
save "" #save 900 1 #save 300 10 #save 60 10000
驗證,重啟服務(wù)
執(zhí)行簡單的命令操作我們可以看到append.aof文件中存儲的內(nèi)容就是我們執(zhí)行的命令
AOF持久化備份的注意點
1.appendfsync的取值有三個,分別是everysec,always和no,在這里我們推薦使用everysec,不推薦使用always。因為always會嚴(yán)重影響服務(wù)器的性能 2.everysec最壞的情況也就只會丟失1秒的數(shù)據(jù),影響在可控范圍之內(nèi)。
優(yōu)缺點
優(yōu)點
AOF 持久化的方法提供了多種的同步頻率,即使使用默認(rèn)的同步頻率每秒同步一次,Redis 最多也就丟失 1 秒的數(shù)據(jù)而已。 AOF 文件的格式可讀性較強,這也為使用者提供了更靈活的處理方式。例如,如果我們不小心錯用了 FLUSHALL 命令,在重寫還沒進行時,我們可以手工將最后的 FLUSHALL 命令去掉,然后再使用 AOF 來恢復(fù)數(shù)據(jù)。
缺點
雖然 AOF 提供了多種同步的頻率,默認(rèn)情況下,每秒同步一次的頻率也具有較高的性能。但在 Redis 的負(fù)載較高時,RDB 比 AOF 具好更好的性能保證。 RDB 使用快照的形式來持久化整個 Redis 數(shù)據(jù),而 AOF 只是將每次執(zhí)行的命令追加到 AOF 文件中,因此從理論上說,RDB 比 AOF 方式更健壯
持久化的一些使用建議
1、如果redis僅僅是用來做為緩存服務(wù)器的話,我們可以不使用任何的持久化。
2、一般情況下我們會將兩種持久化的方式都開啟。redis優(yōu)先加載AOF文件來回復(fù)數(shù)據(jù)。RDB的好處是快速。
3、在主從節(jié)點中,RDB作為我們的備份數(shù)據(jù),只在salve(從節(jié)點)上啟動,同步時間可以設(shè)置的長一點,只留(save 900 1)這條規(guī)則就可以了。
以上就是關(guān)于Redis持久化快照的方法與原理介紹的詳細(xì)內(nèi)容了,看完之后是否有所收獲呢?如果如果想了解更多,歡迎來創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計公司行業(yè)資訊!
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
網(wǎng)頁題目:關(guān)于Redis持久化快照的方法與原理介紹-創(chuàng)新互聯(lián)
文章地址:http://jinyejixie.com/article4/psgie.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、品牌網(wǎng)站建設(shè)、小程序開發(fā)、App開發(fā)、服務(wù)器托管、Google
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容