System Design

A Cache Is a Consistency Trade, and the Trade Should Be Deliberate

Key takeaway: A cache improves performance by serving data that may be stale, and the trade-off between freshness and speed is inherent to caching — the trade should be a deliberate decision based on what the data needs, not an accident of how the cache happens to be configured.

Why Caching Is a Consistency Trade

A cache stores a copy of data so that reads can be served quickly without going to the source of truth. The cost is that the cached copy can become stale — the source of truth changes, but the cache still serves the old value until it is updated or expires. This is the fundamental trade of caching: speed in exchange for possible staleness.

The trade is not optional — every cache has it, because the whole point of a cache is to avoid going to the source of truth, which is what makes it fast and what makes it potentially stale. The question is not whether to make the trade, but how to make it deliberately.

The Freshness Options

Option How it works Trade-off
Time-based expiry Data expires after a set time Simple, but can serve stale data
Invalidation The cache is updated when data changes Fresh, but complex
Write-through Writes go to the cache and the source Consistent, but slower writes
Read-through Reads populate the cache Simple, but can serve stale data

The freshness options range from simple time-based expiry, which is easy but can serve stale data, to invalidation, which keeps the cache fresh but adds complexity. The right option depends on how stale the data can be before it matters.

Choosing the Trade Deliberately

The trade should be chosen deliberately based on the data’s needs. For data where staleness is acceptable — a value that does not change often, or where a slightly old value is fine — a simple time-based expiry is appropriate. For data where staleness is not acceptable — a value that must be current, or where serving stale data has real consequences — invalidation or a shorter expiry is necessary.

The key is to decide the trade based on what the data needs, rather than letting it be an accident of how the cache happens to be configured, since an accidental trade can serve stale data where it matters, or add complexity where it is not needed.

The Cost of Getting It Wrong

Getting the trade wrong has real costs — serving stale data where it matters can cause incorrect behaviour or user-facing errors, while over-invalidating or using too-short expiries can make the cache ineffective, defeating its purpose. Both are avoidable by choosing the trade deliberately based on the data’s needs.

The Bottom Line

Treat caching as a deliberate consistency trade, since a cache improves performance by serving data that may be stale, and the trade between freshness and speed is inherent. Choose the freshness strategy based on what the data needs — simple time-based expiry for data where staleness is acceptable, and invalidation or shorter expiries where it is not — rather than letting the trade be an accident of configuration, since getting it wrong either serves stale data where it matters or makes the cache ineffective.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button