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 微服務框架,幫助我們輕鬆構建和管理微服務應用。 本框架不僅支持 Consul 服務註 ...
  • 先看一下效果吧: 如果不會寫動畫或者懶得寫動畫,就直接交給Blend來做吧; 其實Blend操作起來很簡單,有點類似於在操作PS,我們只需要設置關鍵幀,滑鼠點來點去就可以了,Blend會自動幫我們生成我們想要的動畫效果. 第一步:要創建一個空的WPF項目 第二步:右鍵我們的項目,在最下方有一個,在B ...
  • Prism:框架介紹與安裝 什麼是Prism? Prism是一個用於在 WPF、Xamarin Form、Uno 平臺和 WinUI 中構建鬆散耦合、可維護和可測試的 XAML 應用程式框架 Github https://github.com/PrismLibrary/Prism NuGet htt ...
  • 在WPF中,屏幕上的所有內容,都是通過畫筆(Brush)畫上去的。如按鈕的背景色,邊框,文本框的前景和形狀填充。藉助畫筆,可以繪製頁面上的所有UI對象。不同畫筆具有不同類型的輸出( 如:某些畫筆使用純色繪製區域,其他畫筆使用漸變、圖案、圖像或繪圖)。 ...
  • 前言 嗨,大家好!推薦一個基於 .NET 8 的高併發微服務電商系統,涵蓋了商品、訂單、會員、服務、財務等50多種實用功能。 項目不僅使用了 .NET 8 的最新特性,還集成了AutoFac、DotLiquid、HangFire、Nlog、Jwt、LayUIAdmin、SqlSugar、MySQL、 ...
  • 本文主要介紹攝像頭(相機)如何採集數據,用於類似攝像頭本地顯示軟體,以及流媒體數據傳輸場景如傳屏、視訊會議等。 攝像頭採集有多種方案,如AForge.NET、WPFMediaKit、OpenCvSharp、EmguCv、DirectShow.NET、MediaCaptre(UWP),網上一些文章以及 ...
  • 前言 Seal-Report 是一款.NET 開源報表工具,擁有 1.4K Star。它提供了一個完整的框架,使用 C# 編寫,最新的版本採用的是 .NET 8.0 。 它能夠高效地從各種資料庫或 NoSQL 數據源生成日常報表,並支持執行複雜的報表任務。 其簡單易用的安裝過程和直觀的設計界面,我們 ...
  • 背景需求: 系統需要對接到XXX官方的API,但因此官方對接以及管理都十分嚴格。而本人部門的系統中包含諸多子系統,系統間為了穩定,程式間多數固定Token+特殊驗證進行調用,且後期還要提供給其他兄弟部門系統共同調用。 原則上:每套系統都必須單獨接入到官方,但官方的接入複雜,還要官方指定機構認證的證書 ...
  • 本文介紹下電腦設備關機的情況下如何通過網路喚醒設備,之前電源S狀態 電腦Power電源狀態- 唐宋元明清2188 - 博客園 (cnblogs.com) 有介紹過遠程喚醒設備,後面這倆天瞭解多了點所以單獨加個隨筆 設備關機的情況下,使用網路喚醒的前提條件: 1. 被喚醒設備需要支持這WakeOnL ...
  • 前言 大家好,推薦一個.NET 8.0 為核心,結合前端 Vue 框架,實現了前後端完全分離的設計理念。它不僅提供了強大的基礎功能支持,如許可權管理、代碼生成器等,還通過採用主流技術和最佳實踐,顯著降低了開發難度,加快了項目交付速度。 如果你需要一個高效的開發解決方案,本框架能幫助大家輕鬆應對挑戰,實 ...