Friday, May 17, 2019

How to Create High Available Multi Nodes Redis Cluster (Beginner's Guide)

Redis is an open source (BSD licensed), in-memory data structure store, used as a database, cache and message broker. It supports data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs, geospatial indexes with radius queries and streams.

This tutorial tries to provide information about the availability and consistency characteristics of Redis Cluster from the point of view of the final user, stated in a simple to understand way







Architectures





The architecture of redis cluster


 In practical terms, Redis Cluster provides the ability to
  - Automatically split your dataset among multiple nodes.
  - Continue operations when a subset of the nodes are experiencing failures or are unable to communicate with the rest of the cluster.


The Mechanism
When client API writes to the Master A then Master A replies OK to client API. The Master A propagates the write to its Slaves A, B and C.
When Master A fails then Slave A will be promoted as new master.




Installation
I assume that you already have redis binaries available otherwise you can download from https://redis.io/download


$ wget http://download.redis.io/releases/redis-5.0.4.tar.gz
$ tar xzf redis-5.0.4.tar.gz
$ cd redis-5.0.4
$ make


The binaries that are now compiled are available in the src directory.




Creating Configuration Files
To create redis cluster, basically we need to create minimum redis cluster configuration file:

filename: redis-node1.conf

port 7001                                    
cluster-enabled yes                          
cluster-config-file rediscluster-node-1.conf      
cluster-node-timeout 5000                    
appendonly yes                               
appendfilename node-1.aof                    
dbfilename dump-1.rdb    


filename: redis-node2.conf

port 7002                                    
cluster-enabled yes                          
cluster-config-file rediscluster-node-2.conf      
cluster-node-timeout 5000                    
appendonly yes                               
appendfilename node-2.aof                    
dbfilename dump-2.rdb        


Please continue by creating other node configuration file, just change the port and the number according to the sequence of the port for easy setup. Then copy the configuration file to each node.


Implementing in 1 node (localhost)
If you run only 1 node then you have to create 6 folder and putting redis code in each folder then compile the code. In my case, the folder will look like this:

/home/api/redis1/redis-5.0.4/src
/home/api/redis2/redis-5.0.4/src
/home/api/redis3/redis-5.0.4/src
/home/api/redis4/redis-5.0.4/src
/home/api/redis5/redis-5.0.4/src
/home/api/redis6/redis-5.0.4/src

Don't forget to copy the configuration file into each folder.


Run the Redis server in each folder in different terminal tab:

   $ /home/api/redis1/redis-5.0.4/src/redis-server  redis-node1.conf
   $ /home/api/redis2/redis-5.0.4/src/redis-server  redis-node2.conf
   $ /home/api/redis3/redis-5.0.4/src/redis-server  redis-node3.conf
   $ /home/api/redis4/redis-5.0.4/src/redis-server  redis-node4.conf
   $ /home/api/redis5/redis-5.0.4/src/redis-server  redis-node5.conf
   $ /home/api/redis6/redis-5.0.4/src/redis-server  redis-node6.conf

Then create the cluster by using tools in redis utils in folder /home/api/redis6/redis-5.0.4/src/utils:

    $ /home/api/redis/redis-5.0.4/utils/create-cluster/create-cluster start
    $ /home/api/redis/redis-5.0.4/utils/create-cluster/create-cluster create

To stiop redis cluster:
   $ /home/api/redis/redis-5.0.4/utils/create-cluster/create-cluster stop


create-cluster command

Or you can create redis cluster by running this command:

$ /home/api/redis6/redis-5.0.4/src/redis-cli --cluster create 127.0.0.1:7001 \
127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 127.0.0.1:7006 \
--cluster-replicas 1
 
 
Since redis cluster run in 1 node, make sure you use different port for each redis-server and  granted each port.


Implementing in multiple node
If you run in multiple nodes, just run this command and make sure the IP address each node is correct:

$ /home/api/redis6/redis-5.0.4/src/redis-cli --cluster create 10.10.16.124:7001 10.10.16.125:7001 10.10.16.126:7001 10.10.16.127:7001 10.10.16.128:7001 10.10.16.129:7001 --cluster-replicas 1



Since redis cluster run in multiple node, you can use same port number for redis-server and  granted the port. 




Testing Fail Over
The easy way to test redis cluster by open new terminal and run redis-cli 


$ redis-cli -c -p 7001
redis 127.0.0.1:7001> set foo bar
-> Redirected to slot [12182] located at 127.0.0.1:7002
OK
redis 127.0.0.1:7002> set hello world
-> Redirected to slot [866] located at 127.0.0.1:7001
OK
redis 127.0.0.1:7001> get foo
-> Redirected to slot [12182] located at 127.0.0.1:7002
"bar"
redis 127.0.0.1:7001> get hello
-> Redirected to slot [866] located at 127.0.0.1:7000
"world"






To check the cluster node, run this command: CLUSTER NODES


$ redis-cli -c -p 7001
redis 127.0.0.1:7001> cluster nodes
 
  
To fail over the node, run this command: DEBUG SEGFAULT

Thank you