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

tar xvzf redis-stable.tar.gz
cd redis-stable

If you are getting errors while make Redis, please try to install build-essential tcl.

sudo apt-get install build-essential tcl 

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 –-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