What are Redis Cluster and How to setup Redis Cluster locally ?

Rajat Pachauri
6 min readMay 23, 2023

--

What is Redis Cluster ?

Redis Cluster is a distributed implementation of Redis that allows you to scale your Redis infrastructure horizontally across multiple nodes. It provides high availability and automatic partitioning of data across the cluster, ensuring fault tolerance and improved performance.

In a Redis Cluster, data is automatically sharded and distributed among multiple master nodes, with each master node having one or more replicas for data replication and redundancy. The cluster ensures that data is evenly distributed across the nodes using a process called consistent hashing.

Redis Cluster offers several benefits:

  1. Scalability: By adding more nodes to the cluster, you can increase the overall capacity and throughput of your Redis system. It allows you to handle larger datasets and handle more concurrent requests.
  2. High Availability: Redis Cluster provides fault tolerance by automatically redistributing data and maintaining replicas. If a master node fails, the cluster promotes one of the replicas to a master, ensuring uninterrupted access to data.
  3. Performance: With data distributed across multiple nodes, Redis Cluster can handle parallel processing and concurrent access, resulting in improved performance. It allows you to leverage the collective resources of all nodes in the cluster.
  4. Simplicity: Redis Cluster abstracts the complexity of data sharding and distribution, providing a unified interface to access and manage data across the cluster. Clients can connect to any node in the cluster, and the cluster handles the redirection to the appropriate node transparently.

Redis Cluster is particularly useful in scenarios where high availability, scalability, and fault tolerance are critical requirements. It is commonly used in applications that deal with large datasets and require fast access to distributed data, such as real-time analytics, messaging systems, caching layers, and more.

How does redis cluster works ?

Redis Cluster works by distributing data across multiple nodes while ensuring high availability and fault tolerance. Here’s a simplified overview of how Redis Cluster operates:

  1. Node Discovery: Redis Cluster uses a concept called “gossip protocol” for node discovery. Each node maintains a list of other nodes in the cluster and periodically shares information about the cluster’s state with a few randomly selected nodes. This helps nodes discover and join the cluster.
  2. Data Sharding: Redis Cluster employs consistent hashing to divide the data into multiple hash slots. The cluster supports 16384 hash slots, and each key is mapped to a specific slot using a hash function. The hash slots are distributed across the nodes in the cluster.
  3. Master-Replica Replication: Redis Cluster follows a master-replica replication model. Each hash slot is associated with a specific master node, and the data in that slot is replicated to one or more replica nodes. The replicas provide redundancy and act as failover nodes in case the master node fails.
  4. Client Interaction: Clients can connect to any node in the Redis Cluster. When a client sends a command related to a specific key, the cluster uses the hash slot of that key to determine the node responsible for that slot. The client is then redirected to the appropriate node transparently using a MOVED or ASK redirection response.
  5. Failover and High Availability: Redis Cluster supports automatic failover. When a master node fails, one of its replicas is automatically promoted to a master, ensuring continuous availability of the data. The cluster uses a consensus protocol to elect the new master. Clients are updated with the new master information through redirection responses.
  6. Cluster Configuration: Redis Cluster maintains a cluster configuration that holds information about the nodes, hash slots, and replicas. This configuration is dynamically updated as nodes join or leave the cluster. Cluster nodes exchange messages to reach a consensus on the cluster configuration and ensure its consistency.

By distributing data and load across multiple nodes, Redis Cluster enables horizontal scalability and better performance. It provides fault tolerance, as replicas can take over the role of failed master nodes. The cluster also balances the distribution of hash slots and ensures that each node is responsible for a subset of the slots, allowing the cluster to scale efficiently.

In an oversimplified example of a 4-node cluster, we’d have the following layout:

Note: In Redis Enterprise, there is a further division into shards on each node. It is done the same way, but instead of nodes, it’s divided into shards.

As an example, if you have a key that you know is in slot 2,000, then you know the data resides on Node #0. If the key is in slot 9,000, then it’s on Node #2. In reality, it’s much more complex than this (with slots moving and re-balancing all the time), but for the purposes of understanding transactions and keys, this simplified conceptual understanding of clustering will do.

How to setup up redis cluster locally ?

You can check out the following to install redis on you local machine :-

https://medium.com/@rajatpachauri12345/9a19d47c5454

Once you have installed the redis locally and everything is tested you can start creating nodes for your cluster, first you can test if cluster support is enabled or not for your redis via running following command :-

> redis-cli
127.0.0.1:6379> cluster nodes
ERR This instance has cluster support disabled
127.0.0.1:6379>

You can edit the redis.conf file in your local via uncommenting following fields :-

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

Redis config file path :-

on linux :- /etc/redis/redis.conf
on mac (via brew) :- /opt/homebrew/etc/redis.conf

Or you can pass creating new config file anywhere in system and passing config file path as Redis Server arguments while starting it :-

> redis-server ./redis1.conf

Now let’s start creating the Nodes for the Redis Cluster :-

Create new directory for testing cluster

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

Now let’s create config file in each directory

> cd 7000
> touch redis.conf

Paste the following code in redis.conf

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

Now up each node via following commands

> redis-server ./redis.conf

Repeat the same steps for 7001, 7002, 7003, 7004, 7005 updating ports to corresponding folder name.

Once all your nodes are up and running you can create cluster with following command :-

> redis-cli --cluster create 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 \
--cluster-replicas 1

Now your cluster is created, you can start redis-cli with port and once you run cluster nodes you will be able to see nodes :-

> redis-cli -c -p 7000

127.0.0.1:7000> cluster nodes

134f520f700a64b9217ade24e65b4c09853976f1 127.0.0.1:7005@17005 slave cb324962150e95840c8e20fefe79c4a36112a136 0 1684502951537 2 connected
1a2fe158828a259db576c7345be487e7e3863cd7 127.0.0.1:7004@17004 slave 366b5042d0a213483481cefc63ed743c8b10936f 0 1684502951537 8 connected
cb324962150e95840c8e20fefe79c4a36112a136 127.0.0.1:7001@17001 master - 0 1684502952043 2 connected 5461-10922
cf4c53c1701ff1f2071f9f4c87ead666df47154a 127.0.0.1:7003@17003 slave 8661a584fe4d71017d0c94e58b72c5e74fcd4d5e 0 1684502951435 3 connected
8661a584fe4d71017d0c94e58b72c5e74fcd4d5e 127.0.0.1:7002@17002 master - 0 1684502952448 3 connected 10923-16383
366b5042d0a213483481cefc63ed743c8b10936f 127.0.0.1:7000@17000 myself,master - 0 1684502950000 8 connected 0-5460

Other ways to setup :-

  1. Using utils/create-cluster inside redis folder

If you don’t want to create a Redis Cluster by configuring and executing individual instances manually as explained above, there is a much simpler system.

Find the utils/create-cluster directory in the Redis distribution. There is a script called create-cluster inside (same name as the directory it is contained into), it's a simple bash script. In order to start a 6 nodes cluster with 3 masters and 3 replicas just type the following commands:

create-cluster start

create-cluster create

Reply to yes in step 2 when the redis-cli utility wants you to accept the cluster layout.

You can now interact with the cluster, the first node will start at port 30001 by default. When you are done, stop the cluster with:

create-cluster stop

2. Via Docker

Creating docker bridge network for redis-cluster

docker network create redis-cluster

Six docker container for forming redis-cluster

docker run -e ALLOW_EMPTY_PASSWORD=yes -e REDIS_NODES=redis1,redis2,redis3,redis4,redis5,redis6 - name redis1 - network redis-cluster -p 6379:6379 -d bitnami/redis-cluster:latest
docker run -e ALLOW_EMPTY_PASSWORD=yes -e REDIS_NODES=redis1,redis2,redis3,redis4,redis5,redis6 - name redis2 - network redis-cluster -p 6380:6379 -d bitnami/redis-cluster:latest
docker run -e ALLOW_EMPTY_PASSWORD=yes -e REDIS_NODES=redis1,redis2,redis3,redis4,redis5,redis6 - name redis3 - network redis-cluster -p 6381:6379 -d bitnami/redis-cluster:latest
docker run -e ALLOW_EMPTY_PASSWORD=yes -e REDIS_NODES=redis1,redis2,redis3,redis4,redis5,redis6 - name redis4 - network redis-cluster -p 6382:6379 -d bitnami/redis-cluster:latest
docker run -e ALLOW_EMPTY_PASSWORD=yes -e REDIS_NODES=redis1,redis2,redis3,redis4,redis5,redis6 - name redis5 - network redis-cluster -p 6383:6379 -d bitnami/redis-cluster:latest
docker run -e ALLOW_EMPTY_PASSWORD=yes -e REDIS_NODES=redis1,redis2,redis3,redis4,redis5,redis6 - name redis6 - network redis-cluster -p 6384:6379 -d bitnami/redis-cluster:latest

Commands for forming redis-cluster

docker exec -it redis1 redis-cli - cluster create redis1:6379 redis2:6379 redis3:6379 redis4:6379 redis5:6379 redis6:6379 - cluster-replicas 1 - cluster-yes

If you see some redirect message and than connection timeout message

> Redirected to slot [12182] located at 172.16.0.4:6379

try binding the ip in redis.conf

bind 127.0.0.1

further you can check out the following for issue :-

https://stackoverflow.com/questions/37341646/redis-cli-redirected-to-127-0-0-1

References :

https://redis.io/docs/management/scaling/

https://architecturenotes.co/redis/

--

--