hub

Scaling Distributed Caching

check_circle The Short Answer

To scale distributed caching, you primarily use Horizontal Scaling (Sharding) to increase storage capacity and Replication to increase read throughput and availability.

grid_view

1. The Problem: Naive Sharding Bad for Scaling

Using hash(key) % N distributes data evenly, but resizing the cluster is disastrous. Change the number of nodes below and watch how many keys (dots) change color (move servers).

NODES (N):
3

Impact of Resizing

0% of cache keys moved

System stable. No nodes added or removed yet.

warning

Cache Stampede Risk: In a real system, moving 70%+ of keys means your database gets hit by 70% of traffic instantly. This brings sites down.

donut_large

2. The Solution: Consistent Hashing Scalable

Nodes and Keys are placed on a "Ring". A key belongs to the first node found clockwise. Add a node, and it only "steals" keys from its immediate neighbor.

Key (Data)
Server Node

Rebalancing Impact

0% of keys moved

vs. ~67% with Modulo

Simulation Log

> System initialized with 3 nodes.
pie_chart

Partitioning (Sharding)

Splits data into subsets. Each node owns a slice of the data.

  • check Scales RAM (Total cache size = Sum of nodes)
  • check Scales Writes (Parallel writing)
  • close Complexity in rebalancing (solved by Consistent Hashing)
Used by: Redis Cluster, MongoDB, Cassandra
content_copy

Replication

Copies the same data to multiple nodes (Master-Replica).

  • check Scales Reads (Read from any replica)
  • check High Availability (Failover if master dies)
  • close Writes are limited to Master capacity
Used by: Redis Sentinel, Postgres Streaming
donut_small

Virtual Nodes

A refinement of Consistent Hashing. Each physical server appears multiple times on the ring.

  • check Prevents "Hot Spots" (uneven data distribution)
  • check Better load balancing when nodes have different capacities
Concept: Node A maps to tokens A1, A2, A3... on the ring.

Real-World Implementation: Redis Cluster

Hybrid Approach

Redis Cluster combines Sharding and Replication. It does not use a pure Consistent Hashing ring (0-360°). Instead, it uses Hash Slots.

  • • There are fixed 16,384 hash slots.
  • • Key location = CRC16(key) % 16384.
  • • Every node in the cluster is responsible for a subset of these slots.
  • • Moving a slot is easier than recalculating a full ring, allowing for cleaner resharding.
REDIS TOPOLOGY EXAMPLE
M1
S1
Slots 0-5500
M2
S2
Slots 5501-11000
M3
S3
Slots 11001-16383
M = Master (Write/Read), S = Slave (Replica/Failover)