Everyone who wants to feel safe about his data wants to have some sort of backup :). Redis have a support for replication. And it is very easy to setup.
Setup!
To setup replica node all you have to do is to add one line slaveOf
in config @_@ of your new Redis instance.
Sounds easy :). Lets think about most basic scenario.
Two nodes, master node and slave node. For purpose of this example you can just start redis using redis-server
command without by default he will start on port 6379
and this is all we need to know to setup replication.
Configuration of replica node
To configure replica node all we need to do is to create place to store the db eg. mkdir replica_db
and choose port eg. 7789
. Last thing to do is to create config and point this node to the master. For me it looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
|
Here the important thing really is slaveof 127.0.0.1 6379
where we set where is our master. port 7789
important if we are using few redis instances on one box and dir ./replica_dir
be sure to not point this to master node db path if you do… you will suffer eternal flame.
Now just start the node redis-server redis-replica.conf
and he will start syncing.
Checking if everything works
So by now we should have master running on default port and replica connected to it. Lets connect using redis-cli
to master and set some keys eg. set name kuba
. Now lets connect to our replica. If you followed the same configuration then me you can simply do ./redis-cli -p 7789
this will prompt you with regular command line interface. Now jsut type get name
1 2 |
|
Bang works!
RO
Important information is that one master can have many replicas and each replica is read only! So you can connect to it and read from it if you want / need.
1 2 3 4 5 6 7 8 9 |
|
Summary
I never had deadly important data in redis :) But still its worth knowing how to setup this just in case something goes wrong you may want to have replica ready :).
On official site http://redis.io/topics/replication you can learn more about replication in redis.
Cheers!