Course contentsShow
← Software Engineering
Lesson 973 of 1,93330. Caching StrategiesFree lesson

Distributed Cache Consistency Patterns

Coordinate cache updates across multiple nodes using publish-subscribe or cache invalidation messages to maintain consistency.

Distributed Cache Consistency Patterns

You'll learn how to keep multiple cache nodes synchronized so all servers see the same data.

When you deploy caches across multiple servers (a distributed cache), you face a challenge: what happens when data changes? If Server A updates a cached value, how do Servers B, C, and D know about it? Without coordination, they'll serve stale data.

The Core Problem

Imagine three libraries sharing a popular book catalog. When one library updates a book's status to "checked out," the other two need to know—otherwise they'll tell patrons the book is available when it's not. Distributed caches face this exact synchronization challenge.

Common Consistency Patterns

Publish-Subscribe Invalidation

When one cache node updates or deletes data, it publishes an invalidation message to all other nodes. Each subscriber receives the message and removes (invalidates) that key from its local cache. The next read will fetch fresh data.

Think: "Hey everyone, the value for user:12345 is now stale—delete it from your cache."

Active Cache Update Broadcasting

Instead of just invalidating, some systems broadcast the new value itself. All nodes update their local cache simultaneously. This keeps caches "hot" but requires more network bandwidth and assumes all nodes want the same data.

Time-Based Expiration

Set aggressive TTL (time-to-live) values across all nodes. Data expires quickly everywhere, forcing periodic reloads. Simple but may cause unnecessary database hits.

Cache Versioning

Attach version numbers to cached values. Nodes can detect when their version is outdated and refresh accordingly, similar to optimistic locking patterns.

Key Takeaway: Distributed cache consistency requires explicit coordination—typically through invalidation messages or pub-sub systems—to ensure all nodes evict or update stale entries when data changes.