How To Create Redis Cluster

Install Redis

Run below scripts to install Redis:

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

The Easy Way

  1. Go to redis folder and go into folder /utils/create-cluster/
  2. Run ./create-cluster start
  3. Run ./create-cluster create
  4. Type in Yes for the suggest cluster configuration.
  5. When you see [OK] All 16384 slots covered, it means your cluster created successfully.
  6. Run ./create-cluster stop to stop the cluster

Note: If you are trying to connect the redis cluster from remote client, and you used the “Easy Way” to start the cluster, remember to update create-cluster script to add below code into “start” code branch.

--protected-mode no --bind *.*.*.*

After bind is your local IP address.

The Hard Way

Which will help you understand the detailed operation

To create a cluster, the first thing we need is to have a few empty Redis instances running in cluster mode. This basically means that clusters are not created using normal Redis instances as a special mode needs to be configured so that the Redis instance will enable the Cluster specific features and commands.

The following is a minimal Redis cluster configuration file:

port 7000
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes

As you can see what enables the cluster mode is simply the cluster-enabled directive. Every instance also contains the path of a file where the configuration for this node is stored, which by default is nodes.conf. This file is never touched by humans; it is simply generated at startup by the Redis Cluster instances, and updated every time it is needed.

Note that the minimal cluster that works as expected requires to contain at least three master nodes. For your first tests it is strongly suggested to start a six nodes cluster with three masters and three slaves.

To do so, enter a new directory, and create the following directories named after the port number of the instance we’ll run inside any given directory.

mkdir cluster-test
cd cluster-test
mkdir 7000 7001 7002 7003 7004 7005

Create a redis.conf file inside each of the directories, from 7000 to 7005. As a template for your configuration file just use the small example above, but make sure to replace the port number 7000 with the right port number according to the directory name.

Now install redis in your cluster-test folder by executing below scripts:

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

Start each node like this:

cd 7000
../src/redis-server redis.conf

As you can see from the logs of every instance, since no nodes.conf file existed, every node assigns itself a new ID.

[82462] 26 Nov 11:56:55.329 * No cluster configuration found, I’m 97a3a64667477371c4479320d683e4c8db5858b1

This ID will be used forever by this specific instance in order for the instance to have a unique name in the context of the cluster. Every node remembers every other node using this IDs, and not by IP or port. IP addresses and ports may change, but the unique node identifier will never change for all the life of the node. We call this identifier simply Node ID.

Finally, we are going to start our cluster !! Now that we have a number of instances running, we need to create our cluster by writing some meaningful configuration to the nodes.

This is very easy to accomplish as we are helped by the Redis Cluster command line utility called redis-trib, a Ruby program executing special commands on instances in order to create new clusters, check or reshard an existing cluster, and so forth.

The redis-trib utility is in the src directory of the Redis source code distribution. You need to install redis gem to be able to run redis-trib.

gem install redis

To create your cluster simply type:

./src/redis-trib.rb create --replicas 1 127.0.0.1:7000 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

The command used here is create, since we want to create a new cluster. The option –replicas 1 means that we want a slave for every master created. The other arguments are the list of addresses of the instances I want to use to create the new cluster.

Obviously the only setup with our requirements is to create a cluster with 3 masters and 3 slaves.

Redis-trib will propose you a configuration. Accept the proposed configuration by typing yes. The cluster will be configured and joined, which means, instances will be bootstrapped into talking with each other. Finally, if everything went well, you’ll see a message like that:

[OK] All 16384 slots covered

This means that there is at least a master instance serving each of the 16384 slots available.

Play with your cluster

  • Ping your Cluster
redis-cli -c -p 7000 ping

You will get a “PONG” if cluster is working fine.

  • Insert Data
redis-cli -c -p 7000 set test "test us"

OK

  • Get Data
redis-cli -c -p 7000 get test

“Test US”

  • Insert A Bunch of Data with Bash Loop
for i in `seq 1 150`; 
do 
redis-cli -c -p 7000 set "Test$i" "Test US";
done

Many OKs