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

Cache Partitioning via Consistent Hashing

You will minimize data re-shuffling when resizing your cache cluster.

In this lesson, you will learn how to use Consistent Hashing to distribute cached data across multiple nodes so that adding or removing cache servers doesn't force you to relocate your entire dataset.

The Problem with Modulo Sharding

When sharding cache keys, a naive approach is to use hash(key) % node_count. While simple, this creates a major headache: if you add a new cache node, node_count changes. Consequently, nearly every single key hashes to a different index than it did before. Your entire cache is effectively invalidated, leading to a "cache miss storm" that could crash your underlying database.

The Consistent Hashing Solution

Consistent Hashing solves this by mapping both your cache keys and your cache nodes onto a large, circular "hash ring" (typically a range from 0 to 232−12^{32}-1).

  1. Placement: You hash each node identifier (e.g., node-1, node-2) to determine its position on the ring.
  2. Mapping: To find a key, you hash the key to land on the ring. You then move clockwise until you hit the first cache node. That node owns the key.
  3. Resizing: When you add a new node, it only "steals" keys from its immediate clockwise neighbor. The vast majority of keys remain mapped to the same node as before.

Implementation Concept

In a Node.js environment, you don't typically implement the ring from scratch; you use a library that maintains the ring structure. Here is the conceptual flow:

// Conceptual logic for finding a node
const ring = new ConsistentHashRing(['node-1', 'node-2', 'node-3']);

function getCacheNode(key) {
  // Returns the nearest node on the ring
  return ring.getNode(key); 
}

const node = getCacheNode('user:123:profile');
// Proceed to perform cache operation on 'node'

By using this approach, your system maintains high cache hit rates even when you scale your infrastructure up or down. You minimize data re-shuffling because only a fraction of keys need to be remapped when the cluster topology changes, keeping your database load stable during horizontal scaling events.

Key Takeaway: Consistent Hashing assigns cache keys to a circular hash space, ensuring that adding or removing nodes only affects a small subset of keys rather than the entire cache cluster.