No F*cking Idea

Common answer to everything

Setting Up Replication With Redis

| Comments

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:

redis-replica.conf
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
daemonize no
timeout 0
loglevel notice
logfile stdout
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir ./replica_dir
slave-serve-stale-data yes
slave-read-only yes
appendonly no
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
lua-time-limit 5000

pidfile /var/run/redis-replica-7789.pid
port 7789
# replication config
slaveof 127.0.0.1 6379

slowlog-log-slower-than 10000
slowlog-max-len 1024
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-entries 512
list-max-ziplist-value 64
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60

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

redis-replica.conf
1
2
redis 127.0.0.1:7789> get name
"jakub"

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
 λ ./redis-cli -p 7789
redis 127.0.0.1:7789> keys *
 1) "age"
 2) "name"
redis 127.0.0.1:7789> get name
"jakub"
redis 127.0.0.1:7789> set name "not jakub"
(error) READONLY You can't write against a read only slave.
redis 127.0.0.1:7789>

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!

Comments