Caching LoadBalancing
Caching LoadBalancing
[!NOTE] This module explores the core principles of Caching LoadBalancing, deriving solutions from first principles and hardware constraints to build world-class, production-ready expertise.
1. Consistent Hashing (The Scale Problem)
Problem: You have N cache servers. Simple load balancing uses key % N.
Disaster: If 1 server crashes, N becomes N-1. key % (N-1) changes the mapping for almost every single key. Your database melts down.
Solution: The Ring.
- Map servers to a circle (0 to 232-1).
- Map keys to the same circle.
- Assign key to the first server clockwise.
- Result: If server X crashes, only its keys move to the next neighbor. 90% of cache remains valid.
2. Bloom Filters (The “Maybe” Set)
Problem: Checking if a username exists in a database of 1 billion users. Disk is slow. RAM is expensive. Solution: A probabilistic data structure.
- Query: “Is ‘user123’ in the set?”
- Answer:
- No: Definitely not. (100% confidence).
- Yes: Maybe. (Small probability of False Positive).
Mechanism:
- Bit array of size M.
- K hash functions.
- To add: Hash item K times, set bits to 1.
- To check: Hash item K times. If any bit is 0, it’s definitely missing.
3. Mathematical Anchor: False Positive Rate
The probability of a false positive is approximately: P \approx (1 - e-kn/m)k
- m: Bit array size.
- n: Number of items.
- k: Number of hash functions.
Tradeoff: More memory (m) = Fewer false positives.
4. Deep Dive Strategy Lab: Caching Loadbalancing
Intuition Through Analogy
Think of this chapter like running a high-traffic consumer app. The goal is not to memorize a fixed trick, but to repeatedly answer:
- What is the bottleneck?
- Which constraint dominates (time, memory, latency, correctness)?
- Which representation makes the bottleneck easier to eliminate?