Redis基礎知識(學習筆記16--持久化 (2))

来源:https://www.cnblogs.com/xuliuzai/p/18295153
-Advertisement-
Play Games

亞信科技在Apache SeaTunnel的實踐分享 自我介紹 各位同學好,很榮幸通過Apache SeaTunnel社區和大家進行分享交流。我是來自亞信科技的潘志巨集,主要負責公司內部數據中台產品的開發。 本次分享的主題是Apache SeaTunnel在亞信科技的集成實踐,具體講我們的數據中台是如 ...


三. AOF持久化

AOF,Append Only File,是指Redis將每一次的寫操作(命令)都以日誌的形式記錄到一個AOF文件中的持久化技術。當需要恢復記憶體數據時,將這些寫操作重新執行一次,便會恢復到之前的記憶體數據狀態。

3.1 AOF基礎配置

(1)AOF的開啟

直接修改配置文件的方式

# Please check https://redis.io/docs/latest/operate/oss_and_stack/management/persistence/ for more information.

appendonly no

預設情況下AOF持久化是沒有開啟的,通過修改配置文件中的appendonly的屬性為yes。這種方式需要重啟生效。

 命令的查看修改方式

狀態查看的,可通過下麵的命令:

> config get appendonly

修改的話

>config set appendonly yes

註意,這個命令只是修改了記憶體中的配置。這種命令修改的方式,不需要重啟生效。

如果要刷新到配置文件中

>config rewrite

 (2)文件名的配置

# - appendonly.aof.1.base.rdb as a base file.
# - appendonly.aof.1.incr.aof, appendonly.aof.2.incr.aof as incremental files.
# - appendonly.aof.manifest as a manifest file.

appendfilename "appendonly.aof"  ###註意:這隻是定義了這類文件的首碼

Redis 7 在這裡發生了重大變化。原來只有一個appendonly.aof文件,現在具有了三類多個文件:

*** 基礎文件:可以是RDF格式也可以是AOF格式。其存放的內容是由RDB轉為AOF當時記憶體的快照數據。該文件可以有多個。

*** 增量文件:以操作日誌形式記錄轉為AOF後的寫入操作。該文件可以有多個。

*** 清單文件:用於維護AOF文件的創建順序,保障激活時的應用順序,該文件只有一個。

(3)混合式持久化開啟

# Redis can create append-only base files in either RDB or AOF formats. Using
# the RDB format is always faster and more efficient, and disabling it is only
# supported for backward compatibility purposes.
aof-use-rdb-preamble yes

對於基本文件(例如:appendonly.aof.1.base.rdb as a base file) 可以是RDF格式也可以是AOF格式,通過aof-use-rdb-preamble 屬性可以選擇。其預設值為yes,即預設AOF持久化的基本文件為rdb格式文件,也就是預設採用混合式持久化。

(4)AOF完整配置參數

############################## 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).###概況說RDB存在的風險
#
# 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開啟帶來的好處
#
# 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.
#
# Note that changing this value in a config file of an existing database and
# restarting the server can lead to data loss. A conversion needs to be done
# by setting it via CONFIG command on a live server first.
#
# Please check https://redis.io/docs/latest/operate/oss_and_stack/management/persistence/ for more information.

appendonly no

# The base name of the append only file.
#
# Redis 7 and newer use a set of append-only files to persist the dataset
# and changes applied to it. There are two basic types of files in use:
#
# - Base files, which are a snapshot representing the complete state of the
#   dataset at the time the file was created. Base files can be either in
#   the form of RDB (binary serialized) or AOF (textual commands).
# - Incremental files, which contain additional commands that were applied
#   to the dataset following the previous file.
#
# In addition, manifest【貨單;清單】 files are used to track the files and the order in
# which they were created and should be applied.
#
# Append-only file names are created by Redis following a specific pattern.
# The file name's prefix is based on the 'appendfilename' configuration
# parameter, followed by additional information about the sequence【序號】 and type【類型】.
#
# For example, if appendfilename is set to appendonly.aof, the following file
# names could be derived【產生;獲得】:
#
# - appendonly.aof.1.base.rdb as a base file.【大多數情況下,這個類型的文件就一個;如果記憶體裡面的數據量很大時,也可能會不止一個】
# - appendonly.aof.1.incr.aof, appendonly.aof.2.incr.aof as incremental files.【這個類型的文件會同時有多個】
# - appendonly.aof.manifest as a manifest file.

appendfilename "appendonly.aof"

# For convenience, Redis stores all persistent append-only files in a dedicated【專門的;特定的】
# directory. The name of the directory is determined by the appenddirname
# configuration parameter.

appenddirname "appendonlydir"

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

# Redis can create append-only base files in either RDB or AOF formats. Using
# the RDB format is always faster and more efficient, and disabling it is only
# supported for backward compatibility purposes.
aof-use-rdb-preamble yes

# Redis supports recording timestamp annotations in the AOF to support restoring
# the data from a specific point-in-time. However, using this capability changes
# the AOF format in a way that may not be compatible with existing AOF parsers.
aof-timestamp-enabled no

################################ SHUTDOWN #####################################

 3.2 AOF 文件格式

AOF文件包含三類文件:基本文件、增量文件與清單文件。其中基本文件一般為rdb格式,不再贅述,下麵主要看下增量文件和清單文件的內容格式。

(1)Redis 協議

 增量文件擴展名為.aof,採用AOF格式。AOF格式其實就是redis通訊協議格式,AOF持久化文件的本質就是基於redis通訊協議的文本,將命令以純文本的方式寫入到文件中。

redis協議規定,redis文本是以行來劃分,每行以\r\n行結束。每一行都有一個消息頭,以表示消息類型。消息頭由六種不同的符號表示,其意義如下:

  • (+)表示一個正確的狀態消息;
  • (-)表示一個錯誤的消息;
  • (*)表示消息體總共有多少行,不包括當前行;
  • ($)表示下一行消息數據的長度,不包括換行符長度\r\n;
  • (空)表示一個消息數據;
  • (:)表示返回一個數值。

(2)查看AOF文件

打開appendonly.aof.1.incr.aof文件,可以看到格式如下:

(3)清單文件

打開appendonly.aof.manifest文件,其格式如下:

 tpye有兩種類型:b(base file) 和 i(incremental file)。

該文件首先會按照seq序號列舉出所有基本文件,基本文件type類型為b,然後再按照seq序號再列舉出所有增量文件,增量文件type為i。

對redis啟動時的數據恢復,也會按照該文件從上到下依次載入他們中的數據。

3.3 Rewrite 機制

隨著使用時間的推移,AOF文件會越來越大。為了防止AOF文件由於太大而占用大量的磁碟空間,降低性能,redis引入了rewrite機制來對AOF文件進行壓縮。

(1) rewrite的定義

所謂rewrite其實就是對AOF文件進行重寫整理。當rewrite開啟後,主進程redis-server創建出一個子進程bgrewriteaof,由該子進程完成rewrite過程。其首先對現有aof文件進行rewrite計算,將計算結果寫入到一個臨時文件,寫入完畢後,再rename該臨時文件為原aof文件名,覆蓋原有文件。

(2)rewrite計算

rewrite計算也稱為rewrite策略。rewrite計算遵循以下策略:

  • 讀操作命令不寫入文件;
  • 無效命令不寫入文件;
  • 過期數據不寫入文件;
  • 多條命令合併寫入文件。

(3)手動開啟rewrite

rewrite過程的執行有兩種方式。一種是通過bgrewriteaof命令手動開啟,一種是通過設置條件自動開啟。

手動啟動(觸發)的命令:

> bgrewriteaof

該命令會使主進程redis-server創建出一個子進程bgrewriteaof,由該子進程完成rewrite過程。而在rewrite期間,redis-server仍是可以對外提供讀寫服務的 。

(4)自動開啟rewrite 

其實就是配置文件中的參數,如下:

# 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

學習參閱特別聲明

【Redis視頻從入門到高級】

【https://www.bilibili.com/video/BV1U24y1y7jF?p=11&vd_source=0e347fbc6c2b049143afaa5a15abfc1c】

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 本篇文章就是一個過渡學習的,先入門shell腳本,由於作者有編程基礎,所以有些解釋的比較少。由於現在還在努力學習中,以後等本散修進階了之後再寫進階的、與網路安全更加貼合的shell編程 ...
  • 寫在前面 筆者不才,過去一年中一半的時間在準備考研,博客園無心打理,顯得荒蕪了。到如今臨近畢業,找的工作實事求是的講也只是專業相關,並不完全對口,估計一段時間之內都沒法親自做開發了。雖然去的也是大公司,培養和各方面的保障都不錯,但是對於學了四年技術(慚愧地說學的不算精深)的筆者來說,畢業了做的不是技 ...
  • 2024/07/08 一、JDK下載 二、安裝與JDK開發環境配置(Windows) 三、安裝與JDK開發環境配置(Linux) 一、JDK下載與安裝 網址: https://www.oracle.com/java/technologies/downloads/#java8-windows Linu ...
  • 2024/07/15 1.問題描述 2.問題處理 3.其他問題 1.問題描述 昨天伺服器突然斷電,今天重啟後,網路出了些問題,具體情況如下: 能ping通本機IP ping不通網關 ping不通本網段其他IP地址 ping不通其他網段地址 2.問題處理 vi /etc/sysconfig/netwo ...
  • 近日,天翼雲TeleDB資料庫在中國信通院“可信資料庫”系列測試的賽道上,一次性跨越“分散式事務型資料庫基礎能力測試”與“性能測試”的雙重大關,以雲服務國家隊的卓越實力為資料庫領域樹立了新標桿。 ...
  • 主機配置說明: 192.168.136.101 mysql01 centos7.9 2C4G192.168.136.102 mysql02 centos7.9 2C4G防火牆主機互相放行 firewall-cmd --zone=public --permanent --add-rich-rule=' ...
  • FILE+POS 方式 GreatSQL 主從複製架構給主節點磁碟擴容 一、前提 在一套非常老的系統上,有一套GreatSQL主從集群(1主1從),主從複製採用的是FILE+POS方式複製,磁碟使用緊張需要擴容,只能在該台機器上添加更大的磁碟,將原數據盤替換,也沒有其他的機器資源替換。這套系統沒有V ...
  • 本文介紹了索引合併(Index Merge)包含的三種類型,即交集(intersection)、並集(union)和排序並集(sort-union),以及索引合併的實現原理、場景約束與通過案例驗證的優缺點。 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...