Introducing Dragonfly Cloud! Learn More

Question: How does a Redis cluster work?


A Redis cluster is a distributed implementation of the Redis database that provides a way to run an automatically sharding, highly available Redis installation.

When you create a Redis Cluster, you start multiple Redis instances that each act as a node. To form a cluster, these nodes use a gossip protocol to communicate with each other and automatically distribute data between them. The distribution uses consistent hashing on the keys to divide the total key space (16384 slots by default) among all available nodes. Each member of the cluster handles a subset of the hashed keys.

Data in a Redis Cluster is partitioned across the nodes. Each master node is responsible for a portion of the data, and replica nodes duplicate this data for redundancy and failover protection. If a master node fails, one of its replicas can be promoted to take its place.

In terms of command execution, most Redis commands are redirected to the appropriate node automatically. But some commands, like multi-key operations, need all keys involved to be on the same node.

Here is a basic example of starting a new Redis Cluster with 3 master nodes and 3 slave nodes using redis-cli:

# Start instance 1: redis-server --port 6379 --cluster-enabled yes --cluster-config-file nodes_6379.conf --cluster-node-timeout 5000 --appendonly yes --appendfilename node_6379.aof --dbfilename node_6379.rdb --logfile node_6379.log --daemonize yes # Start instance 2: redis-server --port 6380 --cluster-enabled yes --cluster-config-file nodes_6380.conf --cluster-node-timeout 5000 --appendonly yes --appendfilename node_6380.aof --dbfilename node_6380.rdb --logfile node_6380.log --daemonize yes # Start instance 3: redis-server --port 6381 --cluster-enabled yes --cluster-config-file nodes_6381.conf --cluster-node-timeout 5000 --appendonly yes --appendfilename node_6381.aof --dbfilename node_6381.rdb --logfile node_6381.log --daemonize yes

After starting the instances, you can use redis-cli to create a cluster:

redis-cli --cluster create

Remember that a Redis Cluster provides automatic failover and high availability, but it does not provide strong consistency. Updates may take some time to propagate from master nodes to slave nodes. If a client reads from a slave node immediately after writing to the master, it might get an outdated value.

Was this content helpful?

White Paper

Free System Design on AWS E-Book

Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.

Free System Design on AWS E-Book

Start building today 

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.