Redis Sentinel vs Redis Cluster: Key Differences and Usage Guide
Sentinel provides high availability by monitoring and automatic failover of Redis masters but does not support data sharding. Redis Cluster offers both high availability and automatic data sharding across multiple nodes for scalability.Quick Comparison
This table summarizes the main differences between Redis Sentinel and Redis Cluster across key factors.
| Factor | Redis Sentinel | Redis Cluster |
|---|---|---|
| Purpose | High availability and failover | High availability, failover, and data sharding |
| Data Sharding | No, single dataset per master | Yes, automatic partitioning across nodes |
| Failover | Automatic master failover with Sentinel monitoring | Automatic failover with cluster management |
| Complexity | Simpler setup, fewer nodes | More complex setup, multiple shards and replicas |
| Use Case | Single dataset with high availability | Large datasets needing horizontal scaling |
| Client Support | Standard Redis clients with Sentinel support | Clients must support cluster protocol |
Key Differences
Redis Sentinel is designed to provide high availability by monitoring Redis master and replica nodes. It detects failures and promotes a replica to master automatically, ensuring minimal downtime. However, Sentinel does not split data; it manages a single Redis dataset replicated across nodes.
Redis Cluster extends this by partitioning data across multiple nodes using hash slots. It provides both failover and horizontal scaling by distributing data shards. Clients communicate with the cluster to access the correct shard transparently.
Sentinel setups are simpler and suitable for applications with a single dataset that needs high availability. Cluster setups are more complex but essential when data size or throughput requires scaling beyond a single Redis instance.
Code Comparison
Example of connecting to Redis using Sentinel for automatic failover handling.
import redis from redis.sentinel import Sentinel # Connect to Redis Sentinel sentinel = Sentinel([('localhost', 26379)], socket_timeout=0.1) # Get master from Sentinel master = sentinel.master_for('mymaster', socket_timeout=0.1) # Set and get a key master.set('key', 'value') value = master.get('key') print(value.decode())
Redis Cluster Equivalent
Example of connecting to Redis Cluster and performing a set/get operation.
from rediscluster import RedisCluster # Define startup nodes startup_nodes = [{'host': '127.0.0.1', 'port': 7000}] # Connect to Redis Cluster rc = RedisCluster(startup_nodes=startup_nodes, decode_responses=True) # Set and get a key rc.set('key', 'value') value = rc.get('key') print(value)
When to Use Which
Choose Redis Sentinel when you need high availability for a single Redis dataset without the complexity of sharding. It is ideal for smaller applications or when scaling is not a priority.
Choose Redis Cluster when your dataset is large or your application requires horizontal scaling and automatic data partitioning. It is suited for high-throughput, large-scale Redis deployments.