0Pricing
Caching Strategies: Redis + CDN + Edge Computing · Lesson

Redis Cluster and Sharding

Learn how Redis Cluster scales a cache horizontally across many nodes using hash slots, how keys are routed, and how to keep related keys together with hash tags.

Redis Cluster and Sharding is a free Caching Strategies: Redis + CDN + Edge Computing lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Caching Strategies: Redis + CDN + Edge Computing learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why Scale Beyond One Node?

A single Redis instance is limited by one server's memory and throughput. Redis Cluster spreads data across many nodes so the cache can grow far beyond a single machine.

Hash Slots

Redis Cluster divides the keyspace into 16384 hash slots. Each node owns a range of slots. A key belongs to exactly one slot, determined by hashing the key.

TOTAL_SLOTS = 16384

def slot_for(crc):
    return crc % TOTAL_SLOTS

print('Slot:', slot_for(123456789))

CRC16 Routing

The slot is computed as CRC16(key) mod 16384. The client (or cluster) uses this to know which node holds the key, so reads and writes go directly to the right place.

Slot Distribution

With three nodes, slots split roughly into thirds. Adding a node means moving some slots to it; removing one redistributes its slots. This is how the cluster rebalances.

nodes = 3
per_node = 16384 // nodes
print('Approx slots per node:', per_node)

MOVED and ASK Redirects

If a client contacts the wrong node, the node replies with a MOVED redirect pointing to the correct node. During slot migration, a temporary ASK redirect is used. Smart clients cache the slot map.

The Multi-Key Limitation

Operations across multiple keys (like MGET or transactions) only work if all keys live in the same slot. Otherwise the cluster rejects the command, because the keys may be on different nodes.

Hash Tags

Hash tags solve this. Only the part inside curly braces is hashed, so keys sharing a tag land in the same slot. user:{42}:profile and user:{42}:cart are co-located.

def hash_part(key):
    if '{' in key and '}' in key:
        start = key.index('{') + 1
        end = key.index('}')
        return key[start:end]
    return key

print(hash_part('user:{42}:cart'))

Replicas for Availability

Each master node can have replicas. If a master fails, a replica is promoted automatically, so the slot range stays available. This combines sharding with high availability.

Cluster vs Client-Side Sharding

Before Redis Cluster, apps sharded manually by hashing keys to instances in client code. Cluster mode moves that logic into Redis itself, handling rebalancing and failover automatically.

Trade-offs of Clustering

Clustering adds power but also constraints: cross-slot operations are limited, multi-key Lua scripts need same-slot keys, and operations are more complex. Use it when a single node truly is not enough.

When to Cluster

Reach for Redis Cluster when memory or throughput exceeds one node, when you need automatic failover at scale, or when a region demands many nodes. For smaller caches, a single primary with a replica is simpler.

Quick Check

You need MGET to fetch two related keys atomically in a Redis Cluster. How do you guarantee they live on the same node?

Recap

You learned Redis Cluster and sharding:

  • The keyspace splits into 16384 hash slots owned by nodes.
  • CRC16 routing plus MOVED/ASK redirects locate keys.
  • Multi-key ops need same-slot keys; hash tags co-locate them.
  • Replicas add automatic failover.

Clustering scales a cache horizontally when one node is not enough.

Frequently asked questions

Is the “Redis Cluster and Sharding” lesson free?

Yes — the full text of “Redis Cluster and Sharding” is free to read here on the web, and the Caching Strategies: Redis + CDN + Edge Computing course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Caching Strategies: Redis + CDN + Edge Computing course, upgrade to CoddyKit PRO.

What will I learn in “Redis Cluster and Sharding”?

Learn how Redis Cluster scales a cache horizontally across many nodes using hash slots, how keys are routed, and how to keep related keys together with hash tags. You practise Caching Strategies: Redis + CDN + Edge Computing with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Caching Strategies: Redis + CDN + Edge Computing?

No prior experience is required. Caching Strategies: Redis + CDN + Edge Computing on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Redis Cluster and Sharding” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Caching Strategies: Redis + CDN + Edge Computing lesson?

Yes. Every Caching Strategies: Redis + CDN + Edge Computing lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Redis Persistence & HA
  2. Distributed Caching with Redis
  3. Redis Pub/Sub for Invalidation
  4. Redis Cluster and Sharding
← Back to Caching Strategies: Redis + CDN + Edge Computing