Redis主從複製-Replication

來源:互聯網
上載者:User

標籤:disable   locking   分享   height   ade   ror   對象   setting   writable   

官網介紹看這裡 http://redis.io/topics/replication

 主從複製:就是主機資料更新後根據配置和策略,自動同步到備機的master/slaver機制,Master以寫為主,Slave以讀為主

Redis replication is a very simple to use and configure master-slave replication that allows slave Redis servers to be exact copies of master servers.

  • 用處:讀寫分離,效能擴充; 容災快速回複

特點:

  • Redis uses asynchronous replication  非同步複製
  • A master can have multiple slaves  一主可以多從 並聯關係
  • Slaves are able to accept connections from other slaves   串聯關係
  • Redis replication is non-blocking on the master side    複製是非阻塞模式
  • Replication is also non-blocking on the slave side
  • avoid the cost of having the master write the full dataset to disk  防止資料溢出寫入硬碟

一、配從(伺服器)不配主(伺服器)

配置三個伺服器6379,6380,6381

需要複製三份設定檔,並分別更改連接埠號碼,pid檔案名稱字,dump.rdb名字,

Appendonly關掉;

 info replication查詢主從複製的資訊,初始時都是master

 

salveof <ip> <port> 設定主僕關係,

①           並聯關係:80和81都是79的slave

②           串聯關係: 79是80的master,80是81的master

 

 

先來並聯關係:

 

①           並聯關係:80和81都是79的slave

 

1 切入點問題?slave1、slave2是從頭開始複製還是從切入點開始複製?比如從k4進來,那之前的123是否也可以複製 .

2 從機是否可以寫?set可否?

127.0.0.1:6380> set k8 v8

(error) READONLY You can‘t write against a read only slave.

Read-only slave

Since Redis 2.6, slaves support a read-only mode that is enabled by default. This behavior is controlled by the slave-read-only option in the redis.conf file, and can be enabled and disabled at runtime using CONFIG SET.

Read-only slaves will reject all write commands, so that it is not possible to write to a slave because of a mistake. This does not mean that the feature is intended to expose a slave instance to the internet or more generally to a network where untrusted clients exist, because administrative commands like DEBUG or CONFIG are still enabled. However, security of read-only instances can be improved by disabling commands in redis.conf using the rename-commanddirective.

You may wonder why it is possible to revert the read-only setting and have slave instances that can be target of write operations. While those writes will be discarded if the slave and the master resynchronize or if the slave is restarted, there are a few legitimate use case for storing ephemeral data in writable slaves. However in the future it is possible that this feature will be dropped.

3 主機shutdown後情況如何?從機是上位還是原地待命

4 主機又回來了後,主機新增記錄,從機還能否順利複製?

5 其中一台從機down後情況如何?依照原有它能跟上大部隊嗎?

設定檔

串聯關係: 79是80的master,80是81的master

  • 薪火相傳
  • 上一個slave可以是下一個slave的Master,slave同樣可以接收其他slaves的串連和同步請求,那麼該slave作為了鏈條中下一個的master, 可以有效減輕master的寫壓力,去中心化降低風險。
  • 用 slaveof  <ip>  <port>
  • 中途變更轉向:會清除之前的資料,重建立立拷貝最新的
  • 風險是一旦某個slave宕機,後面的slave都沒法備份
  • 反客為主 
  • 當一個master宕機後,後面的slave可以立刻升為master,其後面的slave不用做任何修改。
  • 用 slaveof  no one  將從機變為主機。

 

主從複製的原理:

每次從機聯通後,都會給主機發送sync指令,主機立刻進行存檔操作,發送RDB檔案給從機 ,

從機收到RDB檔案後,進行全盤載入,之後每次主機的寫操作,都會立刻發送給從機,從機執行相同的命令

 

How Redis replication works

If you set up a slave, upon connection it sends a PSYNC command.

If this is a reconnection and the master has enough backlog, only the difference (what the slave missed) is sent. Otherwise what is called a full resynchronization is triggered.觸發再同步

When a full resynchronization is triggered, the master starts a background saving process in order to produce an RDB file. At the same time it starts to buffer all new write commands received from the clients. When the background saving is complete, the master transfers the database file to the slave, which saves it on disk, and then loads it into memory. The master will then send all buffered commands to the slave. This is done as a stream of commands and is in the same format of the Redis protocol itself.

You can try it yourself via telnet. Connect to the Redis port while the server is doing some work and issue the SYNCcommand. You‘ll see a bulk transfer and then every command received by the master will be re-issued in the telnet session.

Slaves are able to automatically reconnect when the master-slave link goes down for some reason. If the master receives multiple concurrent slave synchronization requests, it performs a single background save in order to serve all of them.

 

二、哨兵模式sentinal

  • 反客為主的自動版,能夠後台監控主機是否故障,如果故障了根據投票數自動將從庫轉換為主庫.

(1)、建立哨兵設定檔

  • 自訂的/myredis目錄下建立sentinel.conf檔案,名字絕不能錯
  • 在設定檔中填寫內容:

        sentinel  monitor  mymaster  127.0.0.1  6379  1

  • 其中mymaster為監控對象起的伺服器名稱, 1 為 至少有多少個哨兵同意遷移的數量。
  • 執行redis-sentinel  /myredis/sentinel.conf  啟動哨兵模式
  • 可以看到Running in sentinel mode ,和顯示79master 80,81slave的資訊

 

故障恢複

主機79shutdown

可以看到new epoch-選舉leader—elect leader -----switch master 成81,

 

 

Redis主從複製-Replication

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.