Course contentsShow
← Node.js
Lesson 1336 of 1,98012. Caching Strategies and Application SpeedFree lesson

Cache Partitioning and Sharding by Key

You will distribute cache load across multiple instances to overcome vertical scaling limitations.

Cache Partitioning and Sharding by Key

In this lesson, you will learn how to scale your cache throughput beyond the limits of a single Redis instance by distributing keys across a cluster using partitioning.

As your Node.js application grows, a single cache instance can become a bottleneck due to CPU limits or memory capacity. When you hit this ceiling, vertical scaling (buying a bigger server) becomes inefficient or impossible. Sharding solves this by splitting your data across multiple cache nodes.

How Key Partitioning Works

Instead of sending all requests to one primary Redis instance, you implement a logic layer—either within your application or via a cluster-aware client—to route specific keys to specific nodes. The most common approach is Consistent Hashing.

When your application needs to store or retrieve data, it performs a calculation on the key:

  1. Hash the key: Convert the key string into a numeric value (e.g., using an algorithm like CRC32).
  2. Modulo operation: Use the hash value to determine the node index (hash(key) % total_nodes).
  3. Route: Direct the request to the target node.

A Simplified Illustration

Imagine you have three Redis nodes. If you store a user session with the key user:101, the logic maps it to node-1. A session with user:102 might map to node-3.

const Redis = require('ioredis');

// A simple approach using a cluster-aware client
// This handles the partitioning logic automatically
const cluster = new Redis.Cluster([
  { port: 6379, host: '10.0.0.1' },
  { port: 6379, host: '10.0.0.2' },
  { port: 6379, host: '10.0.0.3' }
]);

async function getCachedUser(userId) {
  // The client automatically shards this key based on the cluster map
  return await cluster.get(`user:${userId}`);
}

By partitioning, you gain linear scalability. If you need more capacity, you add more nodes to the cluster and rebalance the keys. This effectively turns a single, massive cache burden into multiple manageable, high-performance slices. It is crucial to remember that once you shard by key, you cannot easily perform cross-key operations (like MGET or set intersections) if the keys reside on different shards, so plan your key namespacing accordingly.

Key Takeaway: Sharding breaks your cache into smaller, independent partitions based on key hashes, allowing you to bypass hardware limits and achieve massive horizontal scalability.