Redis 數據的持久化 RDB、AOF、RDB + AOF、No persistence 各自優缺點

文章目錄

    • 一、RDB (Redis Database)
      • 1.1 RDB 優勢
      • 1.2 RDB 缺點
      • 1.3 RDB 如何工作
      • 1.4 RDB配置
      • 1.5 開啟/關閉,RDB快照策略,save指令
      • 1.6 持久化硬盤文件,dbfilename指令
      • 1.7 持久化硬盤文件的存儲地址,dir指令
    • 二、AOF (Append Only File)
      • 2.1 AOF 優勢
      • 2.2 AOF 缺點
      • 2.3 AOF 如何工作
      • 2.4 AOF配置
      • 2.5 開啟/關閉,appendonly指令
      • 2.6 AOF文件,appendfilename指令
      • 2.7 AOF文件目錄,dir指令
      • 2.8 AOF追加策略,appendfsync指令
      • 2.9 AOF寫入被意外中斷,數據也能恢復到上一句命令,可靠性高
    • 三、No persistence 禁用持久化
    • 四、RDB 和 AOF 能否同時開啟?
    • 五、如何選擇 RDB 和 AOF?
    • 六、官方文檔

持久化,是指將Redis內存中的數據寫入磁盤,當遇到重啟Redis或重啟服務器時,再將磁盤上的數據恢復到內存中

Redis持久化選項有4種
RDB (Redis Database) 以指定的時間間隔進行數據的快照備份。
AOF (Append Only File) 記錄更改數據的命令(例如SET), 然后在重啟時再次執行這些命令,從而恢復數據。
RDB + AOF 組合使用
No persistence 禁用持久化


一、RDB (Redis Database)

RDB開啟后,Redis 可以自動創建某個時間點的快照,以便重啟的時候,將快照中的數據恢復在內存中。

1.1 RDB 優勢

  • 以快照的形式備份,非常適合災難后進行數據的恢復;大數據時,更快地重新啟動。
  • 因為是以時間節點的形式備份,節省了資源,也最大限度地提高了Redis的性能。

1.2 RDB 缺點

  • 因為是以快照的形式備份,會阻塞線程,來保證數據完整性,當遇到大數據時,阻塞時間會被放大。
  • 以時間節點的形式處理,服務停止工作時,可能還沒到達時間點,會導致部分數據丟失,只能恢復到上一個時間節點上。
  • Redis運行時,手動復制RDB文件是安全的,那怕Redis正在寫入臨時RDB文件,原始的RDB文件也是完整的。
  • 每個時刻,RDB文件會有一份,可以創建cron任務,備份RDB文件。記錄不同時刻的RDB文件。

1.3 RDB 如何工作

達到配置中save指令條件時,子進程將內存中的數據集寫入臨時RDB文件,再進行原子替換操作,替換舊的RDB文件。 此時RDB文件只會有一份

1.4 RDB配置

以windows版為例, 在redis.conf中 (redis.windows-service.conf 和 redis.windows.conf)

redis.windows-service.conf 旨在作為服務/守護進程運行。這意味著它應該在后臺運行并由操作系統管理(重啟時啟動,崩潰時重新啟動等)。

redis.windows.conf 旨在從命令行或腳本運行并在用戶空間中進行管理。

兩個配置文件基本相同,更細的區別,不再贅述。

windows服務管理中啟用、停用redis服務,僅修改redis.windows-service.conf配置即可

################################ 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
save 300 10
save 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 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.5 開啟/關閉,RDB快照策略,save指令

# 900秒(15分鐘)后,如果至少有一個按鍵發生變化
save 900 1  
# 300秒(5分鐘)后,如果至少有10個按鍵發生變化  
save 300 10 
# 60秒后,如果至少有10000個密鑰發生更改   
save 60 10000  
  • 注釋或刪除掉所有策略,就可以禁用RDB模式
  • 添加自定義策略,只需要滿足save指令規則即可

1.6 持久化硬盤文件,dbfilename指令

dbfilename dump.rdb

指定了文件的名稱、擴展名;dump.rdb是一個的二進制文件


1.7 持久化硬盤文件的存儲地址,dir指令

dir ./

默認是:當前安裝目錄下

AOF和RDB模式,使用了這個公共配置項,如果修改RDB和AOF都受影響


二、AOF (Append Only File)

2.1 AOF 優勢

  • 持久化的同步有不同的策略,fsync策略,參照后面文章,可以自行靈活配置
  • 即使由于某種原因(磁盤已滿或其他原因),AOF文件中存在不完整命令,redis check aof工具也能很容易地修復它。
  • AOF文件過大時,文件會被拆分和重寫,用當前數據集所需的最小操作命令集生成一個全新的AOF文件,再進行原子替換操作,切換新舊AOF文件

2.2 AOF 缺點

  • AOF文件通常比RDB文件大;因為RDB是時間點的數據快照,AOF是整個數據變動過程的完整命令。
  • AOF重寫過程時, 可能會占用更多內存, 先緩沖在內存中,并在最后寫入新的AOF

2.3 AOF 如何工作

Redis 收到更改數據集的命令(例如SET)時,將該命令寫入到緩沖區,最后依據fsync策略,從緩沖區再追加AOF文件中。需要恢復時,執行中AOF文件中命令即可。

Redis 7.0.0以后,AOF文件過大時,原始的單個AOF文件被拆分為基本文件(最多一個)和增量文件(可能有多個)

2.4 AOF配置

以windows版為例, 在redis.conf中 (redis.windows-service.conf 和 redis.windows.conf)

redis.windows-service.conf 旨在作為服務/守護進程運行。這意味著它應該在后臺運行并由操作系統管理(重啟時啟動,崩潰時重新啟動等)。

redis.windows.conf 旨在從命令行或腳本運行并在用戶空間中進行管理。

兩個配置文件基本相同,更細的區別,不再贅述。

windows服務管理中啟用、停用redis服務,僅修改redis.windows-service.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 always
appendfsync 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
# is reached but it is still pretty small.
#
# Specify a percentage of zero in order to disable the automatic AOF
# rewrite feature.auto-aof-rewrite-percentage 100
auto-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

2.5 開啟/關閉,appendonly指令

默認情況下 Redis 沒有開啟 AOF(Redis 6.0 之后已經默認是開啟了)
通過 appendonly 參數開啟:yes/no 開啟/關閉

appendonly yes

2.6 AOF文件,appendfilename指令

appendfilename "appendonly.aof"

2.7 AOF文件目錄,dir指令

dir ./

默認是:當前安裝目錄下

AOF和RDB模式,使用了這個公共配置項,如果修改RDB和AOF都受影響


2.8 AOF追加策略,appendfsync指令

appendfsync everysec  
# appendfsync always    
# appendfsync no        
  • everysec 默認值, 后臺線程每秒主動同步一次,將緩沖區數據寫入到aof文件;性能和數據一致性的折中方案。

  • always 每次緩沖區有更改數據集的命令時,直接寫入AOF文件;性能差一點,但數據一致性更高。

  • no 不主動進行寫入到aof文件,等待操作系統刷新數據;數據一致比較差,性能更好;Linux 下一般為 30 秒一次。


2.9 AOF寫入被意外中斷,數據也能恢復到上一句命令,可靠性高

當寫入AOF文件時,redis或服務器發生故障,可能導致命令寫入了一半(命令不完整),當服務恢復時,redis能自己檢測出來異常的命令,將其截斷,然后將數據恢復到上一個命令記錄的時刻,可能最多損失1秒的數據(配置是 appendfsync everysec)

* Reading RDB preamble from AOF file...
* Reading the remaining AOF tail...
# !!! Warning: short read while loading the AOF file !!!
# !!! Truncating the AOF at offset 439 !!!
# AOF loaded anyway because aof-load-truncated is enabled

三、No persistence 禁用持久化

在上面文章中,已經講述到了RDB、AOF的開啟關閉, 將RDB、AOF都關閉,其實就是不使用持久化。

關閉后,redis對于數據的操作,不會被同步到RDB、AOF文件;
關閉后,重啟服務后,也不會從RDB、AOF文件恢復數據,所以重啟服務后,redis沒有數據。
關閉后,向redis寫入key-val數據,如果重啟服務,redis就回丟失數據

如果再次開啟RDB、AOF模式,重啟服務后,會再次加載RDB、AOF文件中的數據。


四、RDB 和 AOF 能否同時開啟?

AOF和RDB可以同時啟用

AOF文件將優先使用,因為它保證是最完整的


五、如何選擇 RDB 和 AOF?

個人理解,RDB是必須要開啟的,Redis默認也是開啟的, 簡單的快照是一個有效的恢復手段,哪怕中間有小部分數據丟失。

普通場景, 能承受一定的數據丟失,可能只開啟RDB即可。
數據安全性要求高的場景,可能需要RDB和AOF同時開啟,

任何時候,都應該再借助數據庫(mqsql、 sql server)進行持久化,做最后一道防線。


六、官方文檔

https://redis.io/docs/management/persistence/

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/211929.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/211929.shtml
英文地址,請注明出處:http://en.pswp.cn/news/211929.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

leetcode:643. 子數組最大平均數 I(滑動窗口)

一、題目 鏈接&#xff1a;643. 子數組最大平均數 I - 力扣&#xff08;LeetCode&#xff09; 函數原型&#xff1a; double findMaxAverage(int* nums, int numsSize, int k) 二、思路 滑動窗口&#xff1a; 先計算數組前k個元素總和&#xff0c;作為第一個窗口&#xff0c;默…

vlog如何降低重復率

大家好&#xff0c;今天來聊聊vlog如何降低重復率&#xff0c;希望能給大家提供一點參考。 以下是針對論文重復率高的情況&#xff0c;提供一些修改建議和技巧&#xff1a; vlog如何降低重復率 Vlog作為一種流行的視頻日志形式&#xff0c;常常被人們用于記錄日常生活、分享經…

pta模擬題——7-34 刮刮彩票

“刮刮彩票”是一款網絡游戲里面的一個小游戲。如圖所示&#xff1a; 每次游戲玩家會拿到一張彩票&#xff0c;上面會有 9 個數字&#xff0c;分別為數字 1 到數字 9&#xff0c;數字各不重復&#xff0c;并以 33 的“九宮格”形式排布在彩票上。 在游戲開始時能看見一個位置上…

Lambda表達式規則,用法

Lambda表達式是JDK8新增的一種語法格式 1.作用 簡化匿名內部類的代碼寫法 Lambad用法前提&#xff1a;只能簡化函數式接口&#xff08;一般加有Funcationallnterface&#xff09;&#xff08;有且僅有一個抽象方法&#xff09;的匿名內部類 匿名內部類&#xff1a;(本質是對…

url轉pdf或者html轉pdf工具 — iText實現url轉pdf

url轉pdf或者html轉pdf工具 — iText實現url轉pdf 參考資料&#xff1a; https://kb.itextpdf.com/itext/can-i-generate-a-pdf-from-a-url-instead-of-from-a- http://www.micmiu.com/opensource/expdoc/itext-pdf-demo/ https://blog.51cto.com/u_16237557/7263784 iText&…

sensitive-word 敏感詞/臟詞開源工具-v.0.10.0-臟詞分類標簽支持

sensitive-word sensitive-word 基于 DFA 算法實現的高性能敏感詞工具。 創作目的 實現一款好用敏感詞工具。 基于 DFA 算法實現&#xff0c;目前敏感詞庫內容收錄 6W&#xff08;源文件 18W&#xff0c;經過一次刪減&#xff09;。 后期將進行持續優化和補充敏感詞庫&…

幾種常用的壓力測試工具

1. JMeter 官網: Apache JMeter簡介: Apache JMeter 是一個開源軟件&#xff0c;主要用于性能測試和壓力測試。它可以用來測試靜態和動態資源&#xff0c;如文件、Web服務、REST API等。下載與使用: 訪問官網下載安裝包。解壓安裝包并運行 JMeter。通過創建測試計劃來設置壓力…

2023年終總結-輕舟已過萬重山

自我介紹 高考大省的讀書人 白&#xff0c;隴西布衣&#xff0c;流落楚、漢。-與韓荊州書 我來自孔孟故里山東濟寧&#xff0c;也許是小學時的某一天&#xff0c;我第一次接觸到了電腦&#xff0c;從此對它產生了強烈的興趣&#xff0c;高中我有一個愿望&#xff1a;成為一名計…

設計模式再探——裝飾模式

目錄 一、背景介紹二、思路&方案三、過程1.裝飾模式簡介2.裝飾模式的類圖3.裝飾模式代碼4.裝飾模式&#xff0c;職責父類拆分的奧義5.裝飾模式&#xff0c;部件抽象類的無中生有 四、總結五、升華 一、背景介紹 最近公司在做架構模型的時候&#xff0c;涉及到裝飾模式的研…

html網頁設計 01marquee標簽廣告滾動(1)

<!DOCTYPE html> <html><head><meta charset"utf-8"><title></title></head><body><!-- scrollamount:數字越大&#xff0c;滾動越快direction:滾動方向滾動的類型behaior"slide",文字滾動到邊界后就會…

Python中的lambda匿名函數詳解以及三種經典使用場景

lambda匿名函數 匿名函數&#xff0c;顧名思義就是不需要具體定義函數名的函數。我們首先拋開復雜的定義&#xff0c;看兩個具體例子。 先看一個無參數函數的例子。假設我們需要一個return 1的函數&#xff0c;如果使用普通的函數定義方式&#xff0c;其代碼為&#xff1a; …

vuepress-----20、全文搜索

默認主題自帶的搜索, 只會為頁面的標題、h2、h3 以及 tags構建搜索索引。所以盡量將圍繞知識點的關鍵字體現到標題上。而 tags 更為靈活&#xff0c;可以把相關的能想到的關鍵字都配置到 tags 中&#xff0c;以方便搜索。 默認插件介紹 (opens new window) 默認主體配置 (ope…

電子秤ADC芯片CS1237技術資料問題合集

問題11&#xff1a;實際應用中&#xff0c;多個稱重傳感器應該怎么與ADC連接&#xff1f; 解答&#xff1a;如果傳感器是測量同一物體&#xff08;例如&#xff1a;廚房垃圾處理器&#xff09;&#xff0c;一般建議使用并聯的方式。則相同類型的信號線連接在一起。對于傳感器的…

C語言指針基礎題(一)

目錄 例題一題目解析答案 例題二題目解析答案 例題三題目解析答案 例題四題目解析答案 例題五題目解析答案 例題六題目解析答案 例題七題目解析答案 感謝各位大佬對我的支持,如果我的文章對你有用,歡迎點擊以下鏈接 &#x1f412;&#x1f412;&#x1f412; 個人主頁 &#x…

C++ 教程 - 01 基礎篇

文章目錄 C介紹環境配置第一個cpp程序案例練習 變量常量關系運算符邏輯運算符條件運算符位運算符類型轉換分支循環程序調用綜合案例 C介紹 基于C語言&#xff0c;繼承了C的所有語法&#xff1b; 靜態類型語言&#xff0c;需要先編譯&#xff0c;再執行&#xff1b; 貼近底層硬…

雪花算法:分布式系統的關鍵藝術

導言 在探索分布式系統的奧秘時&#xff0c;我們經常遇到一個看似簡單卻極其關鍵的挑戰&#xff1a;如何高效、可靠地生成唯一的標識符&#xff08;ID&#xff09;。這不僅是技術的問題&#xff0c;更是一種藝術。讓我們深入探討雪花算法&#xff08;Snowflake Algorithm&…

windows下分卷解壓文件

我的文件是這樣的&#xff1a; 存放路徑為&#xff1a;C:\Users\Luli_study\MICCAI_MMAC\fudanuniversity\DDR dataset 首先要進入分卷文件的目錄cd&#xff1a; 第一步&#xff1a;cd /path/o/分卷問文件目錄 第二步&#xff1a; 執行之后的結果(紅色框出來的)&#xff1a; …

?functools --- 高階函數和可調用對象上的操作?

源代碼: Lib/functools.py functools 模塊應用于高階函數&#xff0c;即參數或&#xff08;和&#xff09;返回值為其他函數的函數。 通常來說&#xff0c;此模塊的功能適用于所有可調用對象。 functools 模塊定義了以下函數: functools.cache(user_function) 簡單輕量級未綁…

Vellum —— Fluid

目錄 Vellum fluids setups Fluid tips and troubleshooting Fluid phases Vellum fluids and soft bodies Vellum fluid vs FLIP fluid Vellum fluids setups Vellum fluid solver是基于粒子流體的解算框架&#xff0c;被完全集成到了Vellum動力學系統&#xff08;可與gr…

王道數據結構課后代碼題 p149 第3—— 7(c語言代碼實現)

目錄 3.編寫后序遍歷二叉樹的非遞歸算法 4.試給出二叉樹的自下而上、自右到左的層次遍歷算法 &#xff08;有圖解代碼詳解&#xff09;c語言代碼實現 5.假設二叉樹采用二叉鏈表存儲結構&#xff0c;設計一個非遞歸算法求二叉樹的高度。 ?編輯 6.設一棵二叉樹中各結點的值互不…