Redis持久化快照(RDB)

      網友投稿 690 2022-05-29

      通過前面文章的介紹,大家對于redis的基本操作應該比較了解了。本文主要介紹下redis持久化方式中的快照持久化

      Redis教程10(事務)

      Redis教程09(發布和訂閱)

      Redis持久化

      所謂的持久化就是保持我們的數據不丟失,將數據通常保存在我們的硬盤中。在Redis中持久化的方式有兩種,一種是快照持久化,一種是AOF持久化,各有各的優缺點,在項目中我們得根據實際的情況來選擇具體的持久化方式。本文主要介紹快照持久化,下篇文章介紹AOF持久化。

      快照持久化

      也叫RDB持久化方式。就是通過拍攝快照的方式來實現持久化,將某個時間的內存數據存儲在一個rdb文件中。在redis服務重新啟動的時候會加載rdb文件中的數據。

      配置快照持久化

      redis中的快照持久化默認是開啟的,在redis.conf配置文件中有相關的配置選項

      ################################ SNAPSHOTTING ################################ # # Save the DB on disk: # # save # # 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秒內至少有1個key被更改就執行快照 save 300 10 #300內描述至少有10個key被更改就執行快照 save 60 10000 #60秒內至少有10000個key被更改就執行快照 # 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 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 ./ #快照文件存儲的位置

      1

      2

      3

      4

      5

      6

      7

      Redis持久化之快照(RDB)

      8

      9

      10

      11

      12

      13

      14

      15

      16

      17

      18

      19

      20

      21

      22

      23

      24

      25

      26

      27

      28

      29

      30

      31

      32

      33

      34

      35

      36

      37

      38

      39

      40

      41

      42

      43

      44

      45

      46

      47

      48

      49

      50

      51

      52

      53

      54

      55

      56

      57

      58

      59

      60

      61

      62

      63

      64

      65

      66

      67

      驗證快照效果

      1.進入安裝目錄,如果有dump.rdb文件就刪除。

      2.啟動redis,然后添加幾個數據,然后關閉redis并退出。如下

      [root@hadoop-node01 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

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      3.在我們的安裝目錄下有生成了一個dump.rdb文件,這個就是我們的快照備份文件。

      4.再次啟動redis,進入發現原來的數據還在,這是因為redis啟動的時候加載了備份文件中的數據。

      [root@hadoop-node01 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@hadoop-node01 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"

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      5.關閉退出

      關閉退出后刪除dump.rdb文件,啟動后發現數據沒有了

      [root@hadoop-node01 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@hadoop-node01 redis-5.0.3]# src/redis-cli 127.0.0.1:6379> ping PONG 127.0.0.1:6379> keys * (empty list or set)

      1

      2

      3

      4

      5

      6

      7

      8

      9

      快照持久化原理

      1.save命令

      在redis運行中,我們可以顯示的發送一條save命令來拍攝快照。save命令是阻塞命令,也就是當服務器接收了一條save命令之后就會開始拍攝快照,在此期間不會再去處理其他的請求,其他請求會被掛起直到備份結束

      2.bgsave命令

      bgsave命令也是立即拍攝快照,有別于save命令,bgsave并不是一條阻塞命令,而是fork一個子線程,然后這個子線程負責備份操作。而父進程繼續處理客戶端的請求,這樣就不會造成阻塞了。

      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

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      3.根據配置文件默認快照

      save 900 1 #900秒內至少有1個key被更改就執行快照 save 300 10 #300內描述至少有10個key被更改就執行快照 save 60 10000 #60秒內至少有10000個key被更改就執行快照

      1

      2

      3

      4.shutdown命令

      當我們只想shutdown命令的時候。服務器會自動發送一條save命令來完成快照操作。并在完成備份操作后關閉服務器。所以我們當我們的操作不滿足前面三種情況的時候關閉服務器后,再次打開我們的數據也不會丟失。

      5.sync命令

      當在主從環境中,從節點要同步主節點的數據的時候會發送一條sync命令來開發一次復制。此時主節點會發送一條bgsave命令來fork一個新的線程來完成快照并在bgsave命令操作結束后將快照文件發送給從節點來完成主從節點的數據的同步。

      優缺點

      優點

      RDB文件是一個很簡潔的單文件,它保存了某個時間點的Redis數據,很適合用于做備份。你可以設定一個時間點對RDB文件進行歸檔,這樣就能在需要的時候很輕易的把數據恢復到不同的版本。

      RDB很適合用于災備。單文件很方便就能傳輸到遠程的服務器上。

      RDB的性能很好,需要進行持久化時,主進程會fork一個子進程出來,然后把持久化的工作交給子進程,自己不會有相關的I/O操作。

      比起AOF,在數據量比較大的情況下,RDB的啟動速度更快。

      缺點

      RDB容易造成數據的丟失。假設每5分鐘保存一次快照,如果Redis因為某些原因不能正常工作,那么從上次產生快照到Redis出現問題這段時間的數據就會丟失了。

      RDB使用fork()產生子進程進行數據的持久化,如果數據比較大的話可能就會花費點時間,造成Redis停止服務幾毫秒。如果數據量很大且CPU性能不是很好的時候,停止服務的時間甚至會到1秒。

      如何禁用快照持久化

      1.在redis.conf配置文件中注釋掉所有的save配置

      2.在最后一條save配置追加吃命令

      save ""

      1

      ~好了本文到此為止,下篇文章介紹AOF持久化

      更多資料歡迎參考官網手冊

      Redis 任務調度

      版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。

      上一篇:RocketMQ實戰:生產環境中,autoCreateTopicEnable為什么不能設置為true
      下一篇:支付寶移動端動態化方案實踐
      相關文章
      久久青青成人亚洲精品| 国产亚洲精品精品国产亚洲综合| 亚洲日韩涩涩成人午夜私人影院| 国产精品亚洲四区在线观看| 亚洲熟妇色自偷自拍另类| 亚洲视频精品在线观看| 亚洲AV无码一区二区三区DV| 国产亚洲精品国产| 亚洲午夜国产精品无码| 中文字幕不卡亚洲| 亚洲欧洲精品无码AV| 亚洲午夜国产精品无码老牛影视| 77777亚洲午夜久久多人| 国产AV无码专区亚洲AV手机麻豆| 国产精品xxxx国产喷水亚洲国产精品无码久久一区 | 亚洲国产精品久久| 婷婷亚洲久悠悠色悠在线播放| 久久夜色精品国产嚕嚕亚洲av| 久久久影院亚洲精品| 亚洲国产高清人在线| 久久亚洲AV成人无码| 亚洲成人免费网址| 7777久久亚洲中文字幕| 亚洲日韩精品无码专区加勒比☆| 中文字幕在线观看亚洲日韩| 亚洲色大成WWW亚洲女子| 亚洲av无码偷拍在线观看| 亚洲aⅴ天堂av天堂无码麻豆| 国产精品亚洲一区二区无码| 亚洲国产成人a精品不卡在线| 亚洲乱码日产精品a级毛片久久| 国产精品亚洲w码日韩中文| 亚洲精品乱码久久久久久久久久久久 | 国产成人精品日本亚洲专| 亚洲人成自拍网站在线观看| 亚洲爆乳成av人在线视菜奈实| 国产亚洲综合视频| 国产精品亚洲精品日韩已方| 亚洲精品乱码久久久久66| 久久亚洲精品中文字幕| 亚洲一区免费在线观看|