Stage 4 · Provision
Cache Design Patterns
Hot Key Mitigation
Key salting, local caches, replication, and load-aware routing for skewed access patterns.
What Are Hot Keys?
Hot keys are cache keys that receive disproportionately high traffic. A trending product, a celebrity user, or a viral post can overwhelm a single Redis node. Hot keys create uneven load distribution and can cause cascading failures.
Key Salting
Key salting distributes a hot key across multiple cache keys by appending a random suffix. Instead of one key receiving 100K QPS, 10 salted keys each receive 10K QPS. This distributes load across multiple Redis nodes.
import random
NUM_SLOTS = 10
def get_hot_key(key: str):
# Randomly select a slot
slot = random.randint(0, NUM_SLOTS - 1)
salted_key = f"{key}:salt:{slot}"
cached = redis.get(salted_key)
if cached:
return json.loads(cached)
# Cache miss — load from DB
data = load_from_db(key)
redis.setex(salted_key, 300, json.dumps(data))
return data
def invalidate_hot_key(key: str):
# Must invalidate ALL salted slots
for slot in range(NUM_SLOTS):
redis.delete(f"{key}:salt:{slot}")Key salting distributes load but complicates invalidation — you must invalidate all salted slots. Use it for read-heavy hot keys where invalidation is infrequent.
Local Caching for Hot Keys
Cache hot keys in-process (Caffeine, Guava) to avoid network hops entirely. Every instance has a local copy of the hot key. This eliminates the network bottleneck and provides nanosecond access times.
Cache<String, Product> hotKeyCache = Caffeine.newBuilder()
.maximumSize(1000)
.expireAfterWrite(Duration.ofSeconds(30))
.build(key -> loadProduct(key));
Product getProduct(String productId) {
// Check local cache first (nanoseconds)
Product cached = hotKeyCache.getIfPresent(productId);
if (cached != null) return cached;
// Fall back to Redis
return redis.get(productId);
}Local caches absorb hot key traffic. Each instance serves from its local cache, distributing the load across all instances. The short TTL ensures freshness.
Replicating Hot Keys
Redis Cluster distributes keys across nodes using hash slots. A hot key always lands on the same node. To distribute the load, replicate the hot key to every node. Redis 6+ supports READONLY replicas that can serve read traffic.
Load-Aware Routing
Load-aware routing directs traffic based on Redis node load. If one node is overloaded with hot keys, route reads to replicas. Envoy and Redis Cluster proxy support load-aware routing.
Preventing Hot Keys
- Design review — Identify keys likely to be hot during design phase.
- Traffic monitoring — Alert on keys with disproportionate QPS.
- Pre-warming — Cache hot keys before traffic spikes.
- Read replicas — Distribute hot key reads across replicas.
- Application caching — Layer local caches for hot data.
Hot keys are usually predictable: trending products, celebrity profiles, flash sales. Identify them proactively and apply mitigation before the traffic spike hits.
Mark this lesson complete to store local progress and unlock a cleaner resume path the next time you visit.