關(guān)于Redis持久化快照的方法與原理介紹

今天小編分享的是關(guān)于redis持久化快照的方法與原理介紹,可能大家對(duì)Redis并不陌生,或者從來(lái)沒(méi)有了解過(guò)Redis。但是不用擔(dān)心,今天小編會(huì)以最簡(jiǎn)單的描述來(lái)講解Redis的原理。

十載的向陽(yáng)網(wǎng)站建設(shè)經(jīng)驗(yàn),針對(duì)設(shè)計(jì)、前端、開(kāi)發(fā)、售后、文案、推廣等六對(duì)一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。成都營(yíng)銷(xiāo)網(wǎng)站建設(shè)的優(yōu)勢(shì)是能夠根據(jù)用戶(hù)設(shè)備顯示端的尺寸不同,自動(dòng)調(diào)整向陽(yáng)建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無(wú)論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。成都創(chuàng)新互聯(lián)公司從事“向陽(yáng)網(wǎng)站設(shè)計(jì)”,“向陽(yáng)網(wǎng)站推廣”以來(lái),每個(gè)客戶(hù)項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。

關(guān)于Redis持久化快照的方法與原理介紹

所謂的持久化就是保持我們的數(shù)據(jù)不丟失,將數(shù)據(jù)通常保存在我們的硬盤(pán)中。在Redis中持久化的方式有兩種,一種是快照持久化,一種是AOF持久化,各有各的優(yōu)缺點(diǎn),在項(xiàng)目中我們得根據(jù)實(shí)際的情況來(lái)選擇具體的持久化方式。

快照持久化(RDB)

也叫RDB持久化方式,就是通過(guò)拍攝快照的方式實(shí)現(xiàn)持久化,將某個(gè)時(shí)間的內(nèi)存數(shù)據(jù)存儲(chǔ)在一個(gè)rdb文件中,在redis服務(wù)重新啟動(dòng)的時(shí)候加載文件中的數(shù)據(jù)

配置持久化快照

redis中的快照持久化默認(rèn)是開(kāi)啟的,在redis.conf配置文件中有相關(guān)的配置選項(xiàng)

################################ 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個(gè)key被更改就執(zhí)行快照
save 300 10  #300內(nèi)描述至少有10個(gè)key被更改就執(zhí)行快照
save 60 10000  #60秒內(nèi)至少有10000個(gè)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í)行寫(xiě)命令

# 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 #是否對(duì)快照文件進(jìn)行壓縮

# 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 #是否進(jìn)行數(shù)據(jù)校驗(yàn)

# The filename where to dump the DB
dbfilename dump.rdb #快照文件存儲(chǔ)的名稱(chēng)
# 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#快照文件存儲(chǔ)的位置

關(guān)于Redis持久化快照的方法與原理介紹

驗(yàn)證效果

1、進(jìn)入安裝目錄,如果有dump.db文件就刪除

2、啟動(dòng)redis,然后添加幾個(gè)數(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、在我們的安裝的目錄下就生成一個(gè)dump.rdb文件,這個(gè)就是我們的快照備份文件

關(guān)于Redis持久化快照的方法與原理介紹

4、再次啟動(dòng)redis,進(jìn)入發(fā)現(xiàn)原來(lái)的數(shù)據(jù)還在,這是因?yàn)閞edis啟動(dòng)的時(shí)候加載了備份文件中的數(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文件,啟動(dòng)后發(fā)現(xiàn)數(shù)據(jù)沒(méi)有了

[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運(yùn)行中,我們可以顯示的發(fā)送一條save命令來(lái)拍攝快照。save命令是阻塞命令,也就是當(dāng)服務(wù)器接收了一條save命令之后就會(huì)開(kāi)始拍攝快照,在此期間不會(huì)再去處理其他的請(qǐng)求,其他請(qǐng)求會(huì)被掛起直到備份結(jié)束

關(guān)于Redis持久化快照的方法與原理介紹

bgsave命令

bgsave命令也是立即拍攝快照,有別于save命令,bgsave并不是一條阻塞命令,而是fork一個(gè)子線程,然后這個(gè)子線程負(fù)責(zé)備份操作。而父進(jìn)程繼續(xù)處理客戶(hù)端的請(qǐng)求,這樣就不會(huì)造成阻塞了。

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命令的時(shí)候。服務(wù)器會(huì)自動(dòng)發(fā)送一條save命令來(lái)完成快照操作。并在完成備份操作后關(guān)閉服務(wù)器。所以我們當(dāng)我們的操作不滿(mǎn)足前面三種情況的時(shí)候關(guān)閉服務(wù)器后,再次打開(kāi)我們的數(shù)據(jù)也不會(huì)丟失。

5、sync命令

當(dāng)在主從環(huán)境中,從節(jié)點(diǎn)要同步主節(jié)點(diǎn)的數(shù)據(jù)的時(shí)候會(huì)發(fā)送一條sync命令來(lái)開(kāi)發(fā)一次復(fù)制。此時(shí)主節(jié)點(diǎn)會(huì)發(fā)送一條bgsave命令來(lái)fork一個(gè)新的線程來(lái)完成快照并在bgsave命令操作結(jié)束后將快照文件發(fā)送給從節(jié)點(diǎn)來(lái)完成主從節(jié)點(diǎn)的數(shù)據(jù)的同步。

優(yōu)缺點(diǎn)

優(yōu)點(diǎn)

RDB文件保存了某個(gè)時(shí)間點(diǎn)的redis數(shù)據(jù),適合備份,你可以設(shè)定一個(gè)時(shí)間點(diǎn)對(duì)RDB文件進(jìn)行歸檔,這樣就能在需要的時(shí)候很輕易的把數(shù)據(jù)恢復(fù)到不同的版本。 RDB很適合用于災(zāi)備。單文件很方便就能傳輸?shù)竭h(yuǎn)程的服務(wù)器上。在數(shù)據(jù)量比較打的情況下,RDB啟動(dòng)速度快.

缺點(diǎn)

RDB容易造成數(shù)據(jù)丟失,如果設(shè)置3分鐘保存一次,如果redis因?yàn)橐恍┰虿荒苷9ぷ?那么從上次產(chǎn)生快照到Redis出現(xiàn)問(wèn)題這段時(shí)間的數(shù)據(jù)就會(huì)丟失了。

如何禁用快照持久化

1、在redis.conf配置文件中注釋掉所有的save配置 2.在最后一條save配置追加吃命令

save ""

AOF持久化

與快照持久化通過(guò)直接保存 Redis 的鍵值對(duì)數(shù)據(jù)不同,AOF 持久化是通過(guò)保存 Redis 執(zhí)行的寫(xiě)命令來(lái)記錄 Redis 的內(nèi)存數(shù)據(jù)。理論上說(shuō),只要我們保存了所有可能修改 Redis 內(nèi)存數(shù)據(jù)的命令(也就是寫(xiě)命令),那么根據(jù)這些保存的寫(xiě)命令,我們可以重新恢復(fù) Redis 的內(nèi)存狀態(tài)。AOF 持久化正是利用這個(gè)原理來(lái)實(shí)現(xiàn)數(shù)據(jù)的持久化與數(shù)據(jù)的恢復(fù)的

開(kāi)啟AOF

在redis中aof默認(rèn)是關(guān)閉的,我們需要修改配置文件開(kāi)啟aof

關(guān)于Redis持久化快照的方法與原理介紹

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)于Redis持久化快照的方法與原理介紹

關(guān)閉快照持久化

save ""
#save 900 1
#save 300 10
#save 60 10000

驗(yàn)證,重啟服務(wù)

執(zhí)行簡(jiǎn)單的命令操作我們可以看到append.aof文件中存儲(chǔ)的內(nèi)容就是我們執(zhí)行的命令

關(guān)于Redis持久化快照的方法與原理介紹

關(guān)于Redis持久化快照的方法與原理介紹

關(guān)于Redis持久化快照的方法與原理介紹

AOF持久化備份的注意點(diǎn)

1.appendfsync的取值有三個(gè),分別是everysec,always和no,在這里我們推薦使用everysec,不推薦使用always。因?yàn)閍lways會(huì)嚴(yán)重影響服務(wù)器的性能 2.everysec最壞的情況也就只會(huì)丟失1秒的數(shù)據(jù),影響在可控范圍之內(nèi)。

優(yōu)缺點(diǎn)

優(yōu)點(diǎn)

AOF 持久化的方法提供了多種的同步頻率,即使使用默認(rèn)的同步頻率每秒同步一次,Redis 最多也就丟失 1 秒的數(shù)據(jù)而已。 AOF 文件的格式可讀性較強(qiáng),這也為使用者提供了更靈活的處理方式。例如,如果我們不小心錯(cuò)用了 FLUSHALL 命令,在重寫(xiě)還沒(méi)進(jìn)行時(shí),我們可以手工將最后的 FLUSHALL 命令去掉,然后再使用 AOF 來(lái)恢復(fù)數(shù)據(jù)。

缺點(diǎn)

雖然 AOF 提供了多種同步的頻率,默認(rèn)情況下,每秒同步一次的頻率也具有較高的性能。但在 Redis 的負(fù)載較高時(shí),RDB 比 AOF 具好更好的性能保證。 RDB 使用快照的形式來(lái)持久化整個(gè) Redis 數(shù)據(jù),而 AOF 只是將每次執(zhí)行的命令追加到 AOF 文件中,因此從理論上說(shuō),RDB 比 AOF 方式更健壯

持久化的一些使用建議

1、如果redis僅僅是用來(lái)做為緩存服務(wù)器的話,我們可以不使用任何的持久化。

2、一般情況下我們會(huì)將兩種持久化的方式都開(kāi)啟。redis優(yōu)先加載AOF文件來(lái)回復(fù)數(shù)據(jù)。RDB的好處是快速。

3、在主從節(jié)點(diǎn)中,RDB作為我們的備份數(shù)據(jù),只在salve(從節(jié)點(diǎn))上啟動(dòng),同步時(shí)間可以設(shè)置的長(zhǎng)一點(diǎn),只留(save 900 1)這條規(guī)則就可以了。

以上就是關(guān)于Redis持久化快照的方法與原理介紹的詳細(xì)內(nèi)容了,看完之后是否有所收獲呢?如果如果想了解更多,歡迎來(lái)創(chuàng)新互聯(lián)行業(yè)資訊!

名稱(chēng)欄目:關(guān)于Redis持久化快照的方法與原理介紹
網(wǎng)頁(yè)路徑:http://muchs.cn/article14/isphde.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機(jī)品牌網(wǎng)站制作、App開(kāi)發(fā)、ChatGPT、微信公眾號(hào)、全網(wǎng)營(yíng)銷(xiāo)推廣

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

小程序開(kāi)發(fā)