Replication in Redis
In previous article we seen that how redis is working or redis’s system design, in this article we will see how replication is done in redis. Redis is following master-slave architecture. it allows replica Redis instances to be exact copies of master instances. data is stored on more then one server and That way, if a server goes down, data is not lost since it is available on the other server. and another advantage of replica is we can use as a load balancer.
Redis follow master-slave architecture, so that whenever write happen then slave also fetch that data from master and update data. for write operation must need master but for read operation we can use slave also and due to this we can reduce load on master server but may happen that before update data on slave if read of that data is occurred then old value we can get so, also need to take care of that.
If a slave gets disconnected from the master, it automatically reconnects and tries to be the exact copy of the master.There are two approaches that a slave takes to get in sync with the master.
Partial Synchronization: As per name suggest it take only partial data which slave don’t have. suppose due to any reason slave goes down and when it back there is some data it missing for gating data slave send point to master that i don’t have data after this point and master send data.
Full Synchronization: In some cases partial syncronization not possible then slave follow full syncronization in this approach master send all add to slave and slave restore the data. in this slave can have in disk or disk-less method. means first master send rdb file to slave and slave save rdb file into disk and then restore. and in disk-less master send data to direct slave’s memory and restore it.
Configuring replication in Redis
There are two ways to configure a Redis server as a slave.
- Provide the
slaveof
property in theredis.conf
file. It take two argument host and port number of the master. When this Redis instance is started, it will automatically connect to the master server.
slaveof
127.0.0.1 6379
2. We can use the REPLICAOF
command to convert a Redis instance into a slave.
REPLICAOF
127.0.0.1 6379
Authenticating a replica with the master
- If the slave is already running, the following command can be used to authorise it with the server.
config set masterauth <password>
2. We can also provide the master password in the redis.conf file of slave using the masterauth
property, as shown below.
masterauth <password>