Monday, July 2, 2018

Redis Replication


Introduction
------------

- Operates in master-slave setup
- Ensures that slaves are exact copy of the master
- Slaves always attempt to sync with master even after network disconnection
- Redis uses asynchronous replication (parallel operation)
- Slaves acknowledges the amount of data receveid from master
- Can be cascaded: master -> slave1 -> slave2 ..
- Master continues to receveive request even slaves are synchronizing

How replication works?
----------------------

Master replicates by sending incremental changes to slaves. It is like a source
control that keeps tracks of changes via "Replication ID" + "Offset".

    | master | AAAAAA, 1 --> | slaves |
    | master | BBBBBB, 2 --> | slaves |
    ...
    | master | XXXXXX, N --> | slaves |

When slave reconnects with master, they sends their old replication ID and
offset. That will be used by master to determine what dataset is missing on the
slaves and will send only what's needed. This is partial synchronization.

When partial sync is not possible, not enough backlog or replication ID no
longer exists on the master, full sync is executed. Master starts a background
job to save dataset to an RDB file while still receiving requests from clients.
The requests actively receive is buffered. Once the background job is finised,
master will send that to the slaves then the buffered datasets will be sent
next.

Diskless Replication
--------------------

- Enabled in version 2.8.18
- RDB is sent directly over the wire to the slaves
- Can be used to eliminate using slow disks on large datasets
- Configured using "repl-diskless-sync" on redis.conf

Slave Configuration
-------------------

- Add the following in redis.conf of each slave and restart redis service
    slaveof
- You can also execute "SLAVEOF" command inside redis-cli will which take
  effect immediately

Read-only Slave
---------------

- Prevents writing data to slaves
- Default mode starting in Redis 2.6
- Configured using "slave-read-only" option
- Reasons to disable read-only on slaves
    a. Storing local keys on slaves during slow operations

Setting authentication
----------------------

- By default, slave doesn't require a password to sync with master
- To configure it:
    a. On master: config set masteraut

Activate master if N slaves are connected
-----------------------------------------

- Allow writes on master if N slaves are connected with a lag of M seconds
- Started in Redis 2.8
- Configuration:
  min-slaves-to-write
  min-slaves-max-lag

How Redis replicate expire keys
-------------------------------

- Slaves don't expire keys; they wait for master to expire it
- Slaves uses logical clock to report that a key doesn't exist
- No keys are expired during LUA script execution

Replication in Docker and NAT
-----------------------------

IP and port for might be different on NATTed setups so use the following config
to solve that issue:

slave-announce-ip 5.5.5.5
slave-announce-port 1234




No comments:

Post a Comment