Redis Cluster
Redis Cluster shards data across masters using 16384 hash slots — CRC16(key) mod 16384 assigns each key to a slot owned by one master.
Introduction
Redis Cluster shards data across masters using 16384 hash slots — CRC16(key) mod 16384 assigns each key to a slot owned by one master. Horizontal scale when one node's RAM or QPS ceiling is hit.
Clients must be cluster-aware (MOVED/ASK redirects) or use a proxy. Multi-key operations require same slot — use hash tags {user}:session and {user}:cart to co-locate.
Minimum six nodes (3 masters + 3 replicas) recommended for production fault tolerance.
Understanding the topic
Key concepts
- 16384 slots partitioned among masters.
- MOVED redirect — permanent slot owner.
- ASK — importing slot during resharding.
- Hash tag {foo} forces same slot.
- Replica per master for HA within cluster.
- CLUSTER FAILOVER for manual promotion.
flowchart LRApp -->|CRC16 slot| NodeAApp -->|MOVED| NodeBNodeA --- NodeC
Step-by-step explanation
- Client computes slot from key.
- Routes command to node owning slot.
- Wrong node returns MOVED new host:port.
- Smart client caches slot map.
- Gossip propagates topology changes.
Syntax reference
Common commands
- -c enables cluster redirect follow.
- Hash tag {user} in key name.
- CROSSSLOT error — keys in different slots in one transaction.
CLUSTER INFOCLUSTER NODESCLUSTER KEYSLOT mykeyredis-cli -c -h node1 CLUSTER SLOTS
Informative example
Cluster-aware session + cart keys with hash tag:
# Both keys land in same slot due to {42}SET user:{42}:session "data" EX 3600HSET user:{42}:cart sku1 2redis-cli -c CLUSTER KEYSLOT user:{42}:session
Without hash tag, MULTI touching session and cart may CROSSSLOT fail.
Real-world use
Real-world use cases
- Dataset exceeds single node RAM.
- Write QPS beyond one event loop.
- Multi-tenant SaaS shard by tenantId tag.
- Global session store at Netflix scale.
- Replace client-side consistent hashing with native Cluster.
Best practices
- Use hash tags for related multi-key ops.
- Cluster-aware client (Lettuce cluster mode).
- Balance slots across masters — rebalance tool.
- Monitor cluster_state:ok in CLUSTER INFO.
- Avoid big multi-key transactions cross-slot.
- Test resharding in staging before prod.
Common mistakes
- Non-cluster client ignoring MOVED.
- Multi-key ops without hash tags.
- Too few nodes — no replica quorum.
- Hot slot from poor key design.
Advanced interview questions
Q1BeginnerHow many hash slots?
Q2BeginnerMOVED vs ASK?
Q3IntermediateHash tag purpose?
Q4IntermediateMinimum prod cluster?
Q5AdvancedHot slot mitigation?
Summary
Cluster = 16384 slots across masters.