Redis replication and availability
Redis Replication and availability can be achieved by using sentinel servers. Sentinel is available in redis starting with 2.8 version.
Firs of all we need redis. You can download it from the official web-site – http://redis.io or install it from the repo in your OS. I will use Debian in this article.
1. Installation:
1 2 |
sudo apt-get update sudo apt-get install redis-server |
BE CAREFUL!!!
If your are using in your DEBAIN 8 non-default path to store your redis database (dump.rdb) you MUST add in /lib/systemd/system/redis-server.service next line(where path is your data-path)
1 |
ReadWriteDirectories=-/data/db/redis |
and than run:
1 |
systemctl reenable redis-server.service |
2. Server Configuration.
2.1. Master
After the installation is complete we need to edit configs: First, we will set up the master node.
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 41 42 43 44 45 46 47 48 |
daemonize yes pidfile "/var/run/redis.pid" port 6379 bind 10.0.0.1 127.0.0.1 # Very important the ip address order. First comes 'production' interface tcp-backlog 511 timeout 0 tcp-keepalive 0 loglevel debug logfile "/var/log/redis/redis-server.log" 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 "/data/db/redis" slave-serve-stale-data yes slave-read-only yes repl-diskless-sync no repl-diskless-sync-delay 5 repl-disable-tcp-nodelay no slave-priority 100 maxmemory 256mb appendonly no appendfilename "appendonly.aof" appendfsync everysec no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb aof-load-truncated yes lua-time-limit 5000 slowlog-log-slower-than 10000 latency-monitor-threshold 0 notify-keyspace-events "" 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 hll-sparse-max-bytes 3000 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 hz 10 aof-rewrite-incremental-fsync yes |
Starting redis-server:
1 |
service redis-server start |
2.2. Slave
To run slave, just append to the master config(do not forget to change the interface) line for replication:
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 41 42 43 44 45 46 47 48 49 |
daemonize yes pidfile "/var/run/redis.pid" port 6379 bind 10.0.0.2 127.0.0.1 # Very important the order. First comes 'production' interface tcp-backlog 511 timeout 0 tcp-keepalive 0 loglevel debug logfile "/var/log/redis/redis-server.log" 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 "/data/db/redis" slave-serve-stale-data yes slave-read-only yes repl-diskless-sync no repl-diskless-sync-delay 5 repl-disable-tcp-nodelay no slave-priority 100 maxmemory 256mb appendonly no appendfilename "appendonly.aof" appendfsync everysec no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb aof-load-truncated yes lua-time-limit 5000 slowlog-log-slower-than 10000 latency-monitor-threshold 0 notify-keyspace-events "" 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 hll-sparse-max-bytes 3000 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 hz 10 aof-rewrite-incremental-fsync yes slaveof 10.0.0.1 6379 |
WARNING:
It is very important to keep ip address order. First comes the ip used for replication/production. Otherwise, you will get an error:
Unable to connect to MASTER: Invalid argument
Starting redis-server:
1 |
service redis-server start |
So, we have master-slave set up. To provide failover redis uses Sentinel. Sentinel monitor the state of the redis. If the server goes down it switches the node. All the changes log to sentinel configuration file.
3. Sentinel Configuration:
Sentinel runs on 26379 port. Config file for redis-sentinel will be sentinel.conf:
1 2 3 4 5 6 7 8 9 10 |
daemonize yes loglevel notice logfile "/var/log/redis/redis-sentinel.log" bind 10.0.0.2 127.0.0.1 # 10.0.0.2 – current server ip port 26379 dir "/tmp" sentinel monitor mymaster 10.0.0.1 6379 2 # 10.0.0.1 – current master server ip sentinel down-after-milliseconds mymaster 3000 sentinel failover-timeout mymaster 9000 sentinel parallel-syncs mymaster 1 |
In this file, we specify the ip address of the master server sentinel to monitor.
Starting sentinel-server:
1 |
redis-sentinel /path/to/sentinel.conf |
or:
1 |
redis-server /path/to/sentinel.conf --sentinel |
Redis replication and availability: How it works.
To change the master:
1 |
127.0.0.1:26379> SENTINEL failover mymaster |
Sentinel keeps the information about the nodes being used in the cluster. So, if you want it to forget deleted nodes we need to run in the sentinel console(redis-cli -p 26379) on each node with interval at least 30 seconds:
1 |
127.0.0.1:26379> SENTINEL RESET mymaster |
BE CAREFUL!!!
If your are using in your DEBAIN 8 non-default path to store your redis database (dump.rdb) you MUST add in /lib/systemd/system/redis-server.service next line(where path is your data-path)
1 |
ReadWriteDirectories=-/data/db/redis |
and than run:
1 |
systemctl reenable redis-server.service |
This will fix next error:
1 2 3 4 5 6 7 8 |
15542:S 01 Mar 11:52:58.217 - Reading from client: Connection reset by peer 15542:S 01 Mar 11:52:58.318 - Accepted 10.145.1.97:34959 15542:S 01 Mar 11:52:59.129 * Connecting to MASTER 10.145.1.98:6379 15542:S 01 Mar 11:52:59.129 * MASTER SLAVE sync started 15542:S 01 Mar 11:52:59.129 * Non blocking connect for SYNC fired the event. 15542:S 01 Mar 11:52:59.130 # Error reply to PING from master: '-MISCONF Redis is configured to save R DB snapshots, but is currently not able to persist on disk. Commands that may modify the data set are disabled. Please check Redis logs for details about the error.' |
Links:
Thats all 🙂