redis的高可用方案

1. Redis持久化

redis能夠將數據寫入到磁盤中,在停機或宕機後,再次啓動redis時,將磁盤中的備份數據加載到內存中恢復使用。這是redis的持久化。持久化有兩種機制:RDB 快照持久化AOF 追加文件持久化node

1.1 RDB 快照持久化

redis能夠將內存中的數據寫入磁盤進行持久化。在進行持久化時,redis會建立子進程來執行。python

redis默認開啓了快照持久化機制。redis

進行快照持久化的時機以下:算法

  • 按期觸發bash

redis的配置文件redis.conf相關內容以下:服務器

# 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秒(15分鐘)以後,若是至少有1個key發生變化,Redis就會自動觸發BGSAVE命令建立快照。
  save 300 10  #在300秒(5分鐘)以後,若是至少有10個key發生變化,Redis就會自動觸發BGSAVE命令建立快照。
  save 60 10000 #在60秒(1分鐘)以後,若是至少有10000個key發生變化,Redis就會自動觸發BGSAVE命令建立快照。複製代碼
  • BGSAVEapp

    執行BGSAVE命令,手動觸發RDB持久化ide

  • SHUTDOWNui

    關閉redis時觸發spa

1.2 AOF 追加文件持久化

redis能夠將執行的全部指令追加記錄到文件中持久化存儲,這是redis的另外一種持久化機制。

redis默認未開啓AOF機制。

redis能夠經過修改配置以下項開啓AOF機制

appendonly yes  # 是否開啓AOF
appendfilename "appendonly.aof"  # AOF文件複製代碼

AOF機制記錄操做的時機

# appendfsync always # 每一個操做都寫到磁盤中
appendfsync everysec  # 每秒寫一次磁盤,默認
# appendfsync no # 由操做系統決定寫入磁盤的時機複製代碼

使用AOF機制的缺點是隨着時間的流逝,AOF文件會變得很大。但redis能夠壓縮AOF文件。

1.3 結合使用

redis容許咱們同時使用兩種機制,一般狀況下咱們會設置AOF機制爲everysec 每秒寫入,則最壞僅會丟失一秒內的數據。

2.Redis高可用

爲了保證redis最大程度上可以使用,redis提供了主從同步+Sentinel哨兵機制。

2.1 Sentinel 哨兵

redis.io/topics/sent…

redis提供的哨兵是用來看護redis實例進程的,能夠自動進行故障轉移,其功能以下:

  • Monitoring. Sentinel constantly checks if your master and slave instances are working as expected.

  • Notification. Sentinel can notify the system administrator, another computer programs, via an API, that something is wrong with one of the monitored Redis instances.

  • Automatic failover. If a master is not working as expected, Sentinel can start a failover process where a slave is promoted to master, the other additional slaves are reconfigured to use the new master, and the applications using the Redis server informed about the new address to use when connecting.

  • Configuration provider. Sentinel acts as a source of authority for clients service discovery: clients connect to Sentinels in order to ask for the address of the current Redis master responsible for a given service. If a failover occurs, Sentinels will report the new address

簡單理解就是哨兵帶有一個心跳機制,會一直去check主服務器是否發生故障,一旦發生故障,就會當即取代原來的主服務器,變成新的主服務器,之前的主服務器就算恢復了也自動變成了從服務器。

在redis安裝後,會自帶sentinel哨兵程序,修改sentinel.conf配置文件

bind 172.16.33.128
port 26380
daemonize yes
logfile /var/log/redis-sentinel.log
sentinel monitor mymaster 172.16.33.127 6379 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000  # 超時複製代碼
  • sentinel monitor mymaster 172.16.33.127 6379 2說明

    • mymaster 爲sentinel監護的redis主從集羣起名

    • 172.16.33.127 6379 爲主從中任一臺機器地址

    • 2 表示有兩臺以上的sentinel認爲某一臺redis宕機後,纔會進行自動故障轉移。

啓動方式:

redis-sentinel sentinel.conf複製代碼

注意事項

  • 至少三個sentinel以上

  • sentinel要分散運行在不一樣的機器上

2.2 Python客戶端使用

安裝

pip install redis複製代碼

配置

在Flask中配置內容以下:

# redis 哨兵
REDIS_SENTINELS = [
    ('172.16.33.128', '26379'),
    ('172.16.33.129', '26379'),
    ('172.16.33.130', '26379'),
]
REDIS_SENTINEL_SERVICE_NAME = 'mymaster'

from redis.sentinel import Sentinel
_sentinel = Sentinel(REDIS_SENTINELS)
redis_master = _sentinel.master_for(REDIS_SENTINEL_SERVICE_NAME)
redis_slave = _sentinel.slave_for(REDIS_SENTINEL_SERVICE_NAME)複製代碼

使用示例

# 讀數據,master讀不到數據去slave讀
try:
    real_code = redis_master.get(key)
except ConnectionError as e:
    real_code = redis_slave.get(key)

# 寫數據,只能在master裏寫
try:
    current_app.redis_master.delete(key)
except ConnectionError as e:
    logger.error(e)複製代碼

3. Redis集羣

redis.io/topics/part…

Reids Cluster集羣方案,內部已經集成了sentinel機制來作到高可用。

Python客戶端須要安裝redis-py-cluster.

pip install redis-py-cluster複製代碼

使用

# redis 集羣
# 構建全部的節點,Redis會使⽤CRC16算法,將鍵和值寫到某個節點上
REDIS_CLUSTER = [
    {'host': '172.16.33.125', 'port': '6379'},
    {'host': '172.16.33.126', 'port': '6379'},
    {'host': '172.16.33.127', 'port': '6379'},
]

from rediscluster import StrictRedisCluster
redis_cluster = StrictRedisCluster(startup_nodes=REDIS_CLUSTER)

# 能夠將redis_cluster就看成普通的redis客戶端使用
redis_master.set('name','python')複製代碼

注意:

  • redis cluster 不支持事務

  • redis cluster 不支持多鍵操做,如mset

相關連接:

相關文章
相關標籤/搜索