Skip to content

Memory, Disk, and Network: Why Caches Exist

6 min reading

Outcome: Explain locality and cache hit ratio, calculate a weighted latency estimate and identify when storing another copy is useful.

Page details

System Design foundations · Chapter 5 · Sources

Published · Last edited: · ~6 min reading · ~17 min with practice

20px

Know first: Servers, Threads, and Waiting: Concurrency and Little’s Law

System Design foundations glossary

Hypothetical scenario. Northwind's price page repeatedly asks Postgres for the same catalogue entries. Dev considers a Redis cache. Before choosing its size, the useful question is whether requests reuse the same information and how long that information remains valid.

Copies help when work is reused

A cache keeps a reusable representation where it is cheaper or faster to access. A CPU cache keeps copies close to computation; an operating-system page cache keeps file data in memory; an application cache can keep an answer closer than the database that produced it. These are related ideas at different boundaries, with different rules for updating and evicting entries.

Latency is the time for an individual operation. Bandwidth is the amount transferred per unit time. Memory, storage and network operations have different combinations of these properties. A device can transfer a large sequential stream quickly while still imposing noticeable overhead on each small random request. Saying only “the disk is fast” hides the access pattern that determines the cost.

Locality describes useful reuse. Temporal locality means recently accessed information is likely to be accessed again. Spatial locality means nearby information may be useful together. A cache uses these patterns to avoid repeating expensive work. If every request asks for a new item that will never be used again, storing each result does not create useful future hits.

Cache lookupkey + validity ruleHit: reuse the acceptable representationChosen total hit latency: 1 msMiss: obtain the answer from the sourceChosen total miss latency: 20 ms
Hit and miss are alternative paths. The weighted mean uses the total latency assigned to each path.

Calculate the average from two paths

A cache hit finds the required usable entry. A miss must obtain the answer elsewhere. The hit ratio is hits divided by lookups, within a declared workload and interval. A stale entry that violates the application's freshness requirement is not a successful reusable answer merely because its key exists.

Illustrative calculation. Choose a 90% hit ratio, a one-millisecond hit path and a twenty-millisecond total miss path. The miss number already includes the lookup and source work; it is not an additional twenty milliseconds to add after another complete response. The weighted average is 0.9 × 1 + 0.1 × 20 = 0.9 + 2 = 2.9 milliseconds.

Over one hundred requests, that means ninety hit paths contribute ninety milliseconds of summed response time, and ten miss paths contribute two hundred milliseconds. The total is 290 milliseconds across one hundred requests, giving the same 2.9-millisecond mean. This sum describes independent toy response times; it does not say a concurrent server needs 290 milliseconds of wall-clock time.

If the hit ratio falls to 50% with the path costs unchanged, the estimate becomes 0.5 × 1 + 0.5 × 20 = 10.5 milliseconds. Improving the one-millisecond hit path cannot compensate for an arbitrary increase in misses. The fraction of expensive paths matters as much as the speed of the cheap path.

Size for the active set

The working set is the distinct information touched over the time window relevant to reuse. It is not necessarily the entire catalogue. For a toy workload, choose ten thousand equally sized product entries and suppose two thousand receive eighty percent of reads. Holding those two thousand entries could cover that read share under the chosen stable access pattern. The statement concerns twenty percent of items by count, not twenty percent of read volume.

Real items have different sizes and update rates. Space occupied by one large entry may displace several small popular entries. A hot key can also overload the cache node serving it even when total memory is ample. Capacity planning therefore needs both the size of reusable data and the rate at which requests access it.

A copy needs a validity rule

Redis does not automatically know that a price in Postgres changed. The application needs a freshness policy: invalidate, refresh, check a revision or accept a declared period of staleness. A time-to-live limits an entry's lifetime under a chosen policy; it does not prove that the source never changed before expiry.

In the wild. Redis's eviction documentation describes policies for deciding which keys leave memory when limits are reached. Eviction is a capacity decision. It is different from invalidating a price because the authoritative value changed. Keeping those decisions separate helps explain why a larger cache can still serve an unacceptable answer.

When more memory stops helping

Misconception: a bigger cache always improves the system. If the useful working set already fits, additional memory may add few hits. If the cached representation is incorrectly scoped or stale, more memory can retain more incorrect answers. If the workload rarely repeats information, extra space cannot create repetition.

Use a cache when measured repeated work justifies maintaining another representation and its validity rules. Account for the empty-cache recovery path as well as the warm path. A source that handles ten misses in the toy hundred-request workload may not handle one hundred simultaneous misses after a restart.

The mean calculation is intentionally narrow. It excludes tail latency, correlated misses, refresh contention, network failure and changing popularity. The later caching lesson introduces request coalescing, early refresh and stampedes. This foundation establishes the earlier decision: what work can legitimately be reused, and how much of the workload does that reuse actually remove?

Check yourself

Does a 90% hit ratio guarantee the 99th percentile is near the hit latency?

No. Ten percent of requests follow the slower chosen miss path, and the weighted mean does not describe percentile behavior or correlated stalls.

An entry remains in Redis after its Postgres price changed. Is this an eviction problem?

It is primarily a validity/invalidation problem. Eviction chooses what leaves because of space policy; freshness determines whether an existing entry is acceptable.

The useful working set already fits. Must doubling memory double performance?

No. Additional space may create few extra hits. Measure reuse, entry size, update cost and bottlenecks before increasing capacity.

Go deeper