Skip to content
Caching

Lesson 9 of 14 · 24 min

x
9/14

Lesson position in the course — not completion. Use Mark Complete to track finished lessons (saved in this browser).

Distributed Caching: Memcached, Hashing & Hot Keys

When one Redis instance is not enough, you scale horizontally. Consistent hashing maps cache keys to nodes in a ring — when you add or remove a node, only a fraction of keys need to move, not the entire dataset. Redis Cluster implements this natively with 16,384 hash slots distributed across nodes.

Memcached is Redis's simpler cousin: pure key-value, no persistence, no data structures, multi-threaded. Choose Memcached when you need a dumb, fast, large cache with no durability requirements. Choose Redis when you need data structures, persistence, pub/sub, or atomic operations.

The hot key problem occurs when a single key (celebrity profile, breaking news article) receives disproportionate traffic, overwhelming one cache node. Solutions: local in-process caching of hot keys (L1), read replicas for that key, or splitting the key into multiple sub-keys (hot:article:123:shard-1 through shard-N) and aggregating on read.

Before
Single Redis node — hot key bottleneck
1// 100k req/sec all hit one key on one node2const article = await redis.get('article:breaking-news');3// Node CPU at 100%, other nodes idle
After
L1 local cache + Redis L2 for hot keys
1const localCache = new LRUCache({ max: 100 });2 3async function getArticle(id) {4  // L1: in-process (nanoseconds)5  const local = localCache.get(id);6  if (local) return local;7 8  // L2: Redis (milliseconds)9  const cached = await redis.get(`article:${id}`);10  if (cached) {11    const article = JSON.parse(cached);12    localCache.set(id, article);13    return article;14  }15 16  const article = await db.articles.findById(id);17  await redis.setex(`article:${id}`, 60, JSON.stringify(article));18  localCache.set(id, article);19  return article;20}

Check your understanding

  • What does consistent hashing reduce?Show answer

    Answer

    Key remapping when nodes are added/removed — only a fraction of keys move.
  • Memcached vs Redis in one line?Show answer

    Answer

    Memcached: simple multi-threaded memory cache. Redis: richer structures, persistence options, pub/sub, atomics.
  • How do you relieve a hot key?Show answer

    Answer

    L1 local cache, replicas, or key sharding/aggregation patterns — measure before complexity.
Previous

Progress is saved in this browser.

Next Lesson