Cache Coherence in Distributed Systems
You'll learn how multiple cache instances can maintain consistency and the tradeoffs involved when data is cached in different locations.
The Core Challenge
When you scale horizontally with multiple servers, each server often has its own cache instance (say, local in-memory caches). The problem: if Server A updates a cached value, how do Servers B, C, and D know their copies are now stale? This is the cache coherence problem — keeping multiple caches synchronized so users don't see outdated or contradictory data.
Why It Matters
Imagine an e-commerce site where Product X's price is cached on three servers. Server A updates the price from 80. If Servers B and C still serve the old $100 price from their caches, customers see inconsistent prices depending on which server handles their request. That's cache incoherence.
Approaches and Tradeoffs
Strong Consistency: Invalidate or update all cache instances immediately when data changes. This requires coordination (network calls, message queues, or distributed cache protocols), adding latency and complexity.
Eventual Consistency: Accept that caches may be briefly out of sync. Use short TTLs, periodic refreshes, or asynchronous invalidation events. Faster and more scalable, but users might see stale data temporarily.
Single Shared Cache: Use a distributed cache like Redis shared by all servers. Ensures coherence but creates a dependency and potential bottleneck.
Real-World Reality
Most systems choose eventual consistency with short TTLs (seconds to minutes) for non-critical data, and strong consistency or no caching for critical operations like payments or inventory counts.
Key Takeaway:
Cache coherence in distributed systems requires balancing consistency guarantees with performance — most real-world systems accept brief staleness (eventual consistency) to avoid coordination overhead.