← All writing

The Economics of Software

Caching Is Not an Architecture

Caching can hide a slow system. It can also add a second system whose correctness now depends on invalidation, freshness and coordination.

By Neeraj KoulNovember 25, 20267 min read

Caching is one of the first tools engineers reach for when a system becomes slow. It is also one of the easiest ways to make a system harder to reason about.

The pitch is irresistible: keep frequently used data closer to the caller, avoid repeating expensive work, and reduce pressure on the source of truth. In the happy path, it works exactly as advertised.

The trouble starts when the source changes.

A cache is another copy of state

The moment a value is cached, the system has more than one representation of that value. There is the authoritative copy and there is the cached copy, which may be stale, partially populated, evicted, unavailable or inconsistent with another cache.

That is not inherently bad. Replication is a legitimate engineering technique. But a cache is not free speed. It is a consistency decision with a performance benefit.

The design needs to answer a few unglamorous questions:

  • What event makes an entry stale?
  • Who is responsible for invalidating it?
  • What happens if invalidation is lost?
  • Can readers tolerate stale data, and for how long?
  • What happens during a cache outage or a cold start?
  • Does the cache key represent every input that affects the result?

If these questions have no clear answers, the cache is not an optimisation. It is an unowned correctness problem.

Invalidation is where the architecture shows up

Time-to-live is simple because it moves the problem into a bounded-staleness contract. It does not make the data fresh; it says how stale the system is willing to be.

Event-driven invalidation can reduce that window, but it introduces delivery semantics, ordering, retries and failure handling. Write-through and write-behind approaches make different trade-offs around latency, durability and consistency. None removes the need to define what readers are allowed to observe.

The right mechanism depends on the data and the product contract. A cached product description and a cached account balance do not have the same correctness requirements.

Fix the expensive work first

Before adding a cache, identify what is expensive. Is the query missing an index? Is the data model forcing repeated joins? Is the service doing duplicate work? Is network distance dominating? Is the algorithm doing more work than the problem requires?

A cache can reduce repeated work, but it can also conceal the reason that work is expensive. The original inefficiency remains, and now there is a cache lifecycle to operate as well.

There are systems where caching is absolutely the right answer. Read-heavy workloads, expensive derived data and carefully bounded staleness are all legitimate reasons. The point is not to avoid caches. It is to make the consistency contract explicit before celebrating the hit ratio.

A cache is a copy of state. Treat it with the same architectural seriousness as any other copy of state.