redis 持久化機制及配置
阿新 • • 發佈:2021-08-19
本文為博主原創,未經允許不得轉載:
目錄:
1. RDB
2. AOF(append-only file)
3. RDB 和 AOF特性比對
4. 混合持久化
redis資料持久化共有兩種方式:一種是RDB,另一個是AOF
1. RDB:
預設情況下,redis將記憶體資料庫快照儲存在名字為dump.rdb的二進位制檔案中。
redis.conf配置中對 rdb方式 的配置如下:
################################ SNAPSHOTTING ################################ # # Save the DB on disk: # # save對 Redis 進行設定, 讓它在“ N 秒內資料集至少有 M 個改動”這一條件被滿足時, 自動儲存一次資料集到rdb檔案中。 redis在資料進行rdb持久化時,支援 Redis 藉助作業系統提供的寫時複製技術(Copy-On-Write, COW),在生成快照的同時,依然可以正常處理寫命令。簡單來說,bgsave 子程序是由主執行緒 fork 生成的,可以共享主執行緒的所有記憶體資料。bgsave 子程序執行後,開始讀取主執行緒的記憶體資料,並把它們寫入 RDB 檔案。此時,如果主執行緒對這些資料也都是讀操作,那麼,主執行緒和 bgsave 子程序相互不影響。但是,如果主執行緒要修改一塊資料,那麼,這塊資料就會被複制一份,生成該資料的副本。然後,bgsave 子程序會把這個副本資料寫入 RDB 檔案,而在這個過程中,主執行緒仍然可以直接修改原來的資料。 其配置如下:<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 # after60 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
2. AOF(append-only file):
RDB 快照功能並不是非常耐久, 如果 Redis 因為某些原因而造成故障停機, 那麼伺服器將丟失最近寫入、且仍未儲存到快照中的那些資料。 Redis 增加了一種完全耐久的持久化方式: AOF 持久化,將修改的每一條指令記錄進檔案 appendonly.aof 中.(先寫入os cache,每隔一段時間fsync到磁碟)############################## 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"
AOF也可配置redis多久將資料 fsync同步到磁碟一次。
appendfsync always:每次有新命令追加到 AOF 檔案時就執行一次 fsync ,非常慢,也非常安全。
appendfsync everysec:每秒 fsync 一次,足夠快,並且在故障時只會丟失 1 秒鐘的資料。推薦(並且也是預設)
appendfsync no:從不 fsync ,將資料交給作業系統來處理。更快,也更不安全的選擇。
3. RDB 和 AOF特性比對
命令 | RDB | AOF |
啟動優先順序 | 低 | 高 |
體積 | 小 | 大 |
恢復速度 | 快 | 慢 |
資料安全性 | 容易丟資料 | 根據策略決定 |