Skip to content

Cache

A cache is a small, fast storage layer that holds copies of data so future requests for that data are served faster; the term comes from the French cache, "hidden", and its software sense was formalised in the 1960s with memoization and CPU caches.

Definition

What it is

A cache trades space for time. It keeps a copy of something expensive — a function's result, a database row, an HTTP response, a DNS answer — in a place where reading is cheaper than recomputing. The trade only pays off when the same data is requested more than once, and when the cost of a stale copy is less than the cost of recomputing.

There are at least six cache families, and they share the same vocabulary: hit (found in the cache), miss (not found, must recompute), eviction (removing an entry to make space), TTL (time to live), invalidation (making an entry no longer valid) and coherence (all layers agree on the value).

The cache hierarchy

graph TD
  CPU[CPU] --> L1[L1 cache: ~32 KB, nanoseconds]
  L1 --> L2[L2 cache: ~256 KB, ~10 ns]
  L2 --> L3[L3 cache: ~8 MB, ~40 ns]
  L3 --> RAM[RAM: GBs, ~100 ns]
  RAM --> DISK[Disk: TBs, ~100 µs]
  DISK --> APP[Application cache: Redis, Memcached]
  APP --> HTTP[HTTP cache: browser, proxy]
  HTTP --> CDN[CDN edge cache]
  CDN --> DNS[DNS cache]
  style L1 fill:#E8F5E9
  style CDN fill:#E8F5E9

Cache vs buffer vs memoization

Aspect

Cache

Buffer

Memoization

Purpose

Faster reads

Smooth throughput

Faster repeated calls

Unit

Key/value

Bytes stream

Function + arguments

Lives in

Every layer

I/O boundary

Function scope

Eviction

LRU, TTL, FIFO

Implicit (drain)

Usually unbounded

Classic example

Redis, CDN

stdio buffer

Fibonacci cache

HTTP caching timeline

  1. 1996 — HTTP/1.0 (RFC 1945): Expires, Pragma, Last-Modified, If-Modified-Since.

  2. 1999 — HTTP/1.1 (RFC 2616): Cache-Control, ETag, If-None-Match, Vary.

  3. 2014 — RFC 7234: refines HTTP caching semantics without changing the model.

  4. 2022 — RFC 9111: current authoritative specification for HTTP caching.

How to use a cache well

Using a cache without breaking correctness

  1. Define the TTL before the cache

    A cache without a TTL is a stale-data generator waiting for an incident.

  2. Decide how to invalidate

    TTL, write-through, write-behind, or explicit invalidation. Pick one; document it.

  3. Measure hit rate

    A cache with a 5% hit rate is an expensive way to slow down requests.

  4. Guard against stampedes

    When a popular key expires, N requests recompute it at once. Use locks, single-flight or jitter.

Common failures

Origin

In French, cache means "hidden". In 19th-century English, a cache was a hidden store of provisions, ammunition or valuables — a place where something is concealed for later use. That physical sense survived into computing: a cache is a hidden store that holds copies of data for later retrieval. The IBM System/360 Model 85 (1968) shipped with a cache memory between the CPU and main memory, and the term became standard. Memoization in Lisp (1960s) applied the same idea to function results. HTTP caching arrived with the web in the 1990s and was formalised in the RFC series.

There are only two hard things in Computer Science: cache invalidation and naming things.

— Phil Karlton, attributed, 1996
1968 · IBM System/360 Model 85 introduce "cache memory"
1996 · HTTP/1.0 (RFC 1945) formaliza Expires y Last-Modified
1999 · HTTP/1.1 (RFC 2616) introduce Cache-Control y ETag
2022 · RFC 9111 reespecifica el caching HTTP
5% · hit rate que vuelve un cache contraproducente

Evidence

FAQ

Cache vs buffer?

A cache stores values for reuse; a buffer smooths throughput between producer and consumer. They are different structures with different purposes.

When should I invalidate?

The instant the source changes, or at the TTL if the cost of staleness is low. Choose based on how bad stale data is.

Why does "clear your cache" work?

Because most UI inconsistencies are stale HTML, CSS or JS held by the browser cache. Clearing forces a fresh fetch.

Can caching be a bug?

Yes. Serving a cached response past its invalidation is a bug, not a feature.

Connections

Etymology

Origin: borrowed

  • cache (noun): from French cache, "hidden"; a hidden store of provisions

Semantic network

CacheperformancstorageMemoizatioBufferCDNRedisinvalidati

● broader ● narrower ● related

Broader

  • performance
  • storage

Related

Used in the garden

Sources