Redis Tutorial 0/42 lessons ~6 min read Lesson 28

    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.

    Course progress0%
    Focus
    10 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    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.
    text
    flowchart LR
    App -->|CRC16 slot| NodeA
    App -->|MOVED| NodeB
    NodeA --- NodeC

    Step-by-step explanation

    1. Client computes slot from key.
    2. Routes command to node owning slot.
    3. Wrong node returns MOVED new host:port.
    4. Smart client caches slot map.
    5. 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.
    bash
    CLUSTER INFO
    CLUSTER NODES
    CLUSTER KEYSLOT mykey
    redis-cli -c -h node1 CLUSTER SLOTS

    Informative example

    Cluster-aware session + cart keys with hash tag:

    bash
    # Both keys land in same slot due to {42}
    SET user:{42}:session "data" EX 3600
    HSET user:{42}:cart sku1 2
    redis-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?
    16384.
    Q2BeginnerMOVED vs ASK?
    MOVED permanent slot migration done; ASK temporary during import.
    Q3IntermediateHash tag purpose?
    Force keys sharing {tag} to same slot for multi-key ops.
    Q4IntermediateMinimum prod cluster?
    3 masters + 3 replicas across AZs typical.
    Q5AdvancedHot slot mitigation?
    Split key design, replica reads, local cache, or sub-shard pattern.

    Summary

    Cluster = 16384 slots across masters.

    Ready to mark this lesson complete?Track your journey across the entire course.