Redis provides two different mechanisms for handling distributed storage: Master-Replica setup and Clustering.
A Master-Replica setup involves a single "master" Redis instance and one or more "replica" instances. The replicas mirror the data of the master. The purpose of this setup is mostly to scale reads and provide redundancy. If the master goes down, one of the replicas can be promoted to master.
Here's how you can set it up:
The first command starts a Redis server acting as the master on port 6379 and the second command starts another Redis server acting as a replica to the master, running on port 6380.
On the other hand, Redis Cluster is a distributed implementation of Redis with two main goals: providing a degree of write scalability and providing high availability. Write commands are divided among multiple nodes so that each node only holds a subset of the total dataset. Additionally, the cluster maintains a certain number of replicas for redundancy and failover purposes.
Setting up a cluster involves several steps but here's a simple example using the
create-cluster script provided by Redis:
This command sets up a six-node Redis Cluster, with three master nodes and three corresponding replica nodes.
The choice between Master-Replica setup and Clustering depends on the use case. Master-Replica setups are simpler to manage and are ideal when read scalability or data redundancy is required, whereas Redis Clusters provide more write scalability and higher availability through automatic partitioning of data.