Architecture and scale
What is a Cache?
Definition
A cache is a store that keeps a copy of a result so later requests for the same thing can be answered from it instead of being recomputed or refetched.
Also known as: Caching
Cache in more detail
Caching trades memory and freshness for speed. Any expensive result that is read far more often than it changes — a rendered page, a database query, a remote API response, a compiled asset — is a candidate. The measure that matters is the hit ratio: a cache that misses most of the time adds a lookup to every request and returns nothing for it.
A request usually passes through several caches, and knowing which one is lying to you is most of the debugging. There is the CPU cache, the operating system's page cache, an in-process memoisation, a shared cache such as Redis or Memcached, the database's own buffer pool, a CDN at the edge, and the browser's HTTP cache. Each has its own key and its own expiry.
Entries leave a cache in two ways. They expire, on a time-to-live short enough that stale data is tolerable, or they are invalidated explicitly when the underlying data changes. Invalidation is genuinely hard because the cache does not know what your write touched, so common strategies keep it simple: put a version or content hash in the key so new data means a new key, or accept a short TTL and serve stale content while revalidating in the background.
The failure mode worth designing against is the stampede. When a popular key expires, every concurrent request misses at once and hammers the origin together. The standard defences are a lock so only one request recomputes while the rest wait, and serving the stale value until the refresh completes.
Cache example
Read-through cache, in words
1. look up key "user:12" in Redis
2. HIT -> return it
3. MISS -> query the database, store the row with a TTL,
then return it
Cache keys should include everything that changes the answer
user:12:v3 bump v3 -> v4 to invalidate everything
articles?page=2&lang=en
HTTP caching, the same idea over the wire
Cache-Control: public, max-age=60, stale-while-revalidate=300
ETag: "9f2c1b"
-> next request sends If-None-Match: "9f2c1b"
-> server may answer 304 Not Modified with no bodyCache — common questions
- What is the difference between a cache and a database?
- A database is the source of truth and is expected to keep data durably. A cache is a disposable copy optimised for speed — losing it should cost latency, never data. If your system cannot survive the cache being emptied, it is being used as a database.
- How long should a cache TTL be?
- As long as you can tolerate the data being wrong. Immutable, content-hashed assets can be cached for a year; a dashboard figure might take sixty seconds; a bank balance probably should not be cached at all without explicit invalidation.
- What is cache invalidation and why is it considered hard?
- It is removing or updating cached entries when the underlying data changes. It is hard because the writing code rarely knows every derived entry its change affects, so systems tend to fall back on short TTLs or versioned keys rather than precise invalidation.
Keep reading
Every definition in the set is indexed A-Z in the developer glossary, and the cheat sheets cover the syntax once you know what you are looking for.
Browse the A-Z glossary