Wednesday, December 12, 2018

Redis clustering for beginners


If you build an enterprise level application, caching is one of the most important areas that you need to consider according to your application rending data volume and transactions per time period. You must be aware of which data to be cached and which data should not be cached.

In this post, I am going to explain, how to create a Redis cluster environment.


Install Redis

You can get the installation details from here https://redis.io/topics/quickstart


wget http://download.redis.io/redis-stable.tar.gz
tar xvzf redis-stable.tar.gz
cd redis-stable
make

Configure Redis cluster configuration files

Redis cluster requires 3 master nodes. Then at least you have to create 6 nodes to have 3 master nodes and 3 cluster nodes, one replica per one master node. If you need 2 replicas for one master node, then you have to create 9 nodes. It means 3 master nodes and 2 replicas per master node. Here is one configuration file that can be used in Redis server.


port 7001
cluster-enabled yes
cluster-config-file cluster-node1.config
cluster-node-timeout 5000
appendonly yes
appendfilename node-1.config

dbfilename dump1.rdb


Here I mentioned only the most important configuration details, not all configurations. If you are going to create 6 nodes, you will have to create 6 config files with different portscluster-config-file, appendfilename and dbfilename

Now you have Redis server and 6 configuration files. Look at the following image.



Now you can start Redis servers one by one in different terminals as follows.

redis-server node1.config
redis-server node2.config
redis-server node3.config
....
....
redis-server node6.config




As you can see, I started node1 which is configured in port 7001. Now it is running.


Start Redis clusters

Now we can use Redis-cli commands to start and configure Redis server in cluster mode.

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

According to this configuration it will create 3 master nodes and 3 replicas. It means one replica per one master node. Once you apply this command, it will show the configuration details and you have to enter "yes" to apply this configuration and start Redis servers. You can login to the node by using the port as redis-cli -c -p 7001 then you can get the node information by using "info". Please look the image below.






There are a set of redis-cli commands. You can find all details about Redis commands from https://redis.io/commands