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 |
| Fibonacci cache |
HTTP caching timeline
1996 — HTTP/1.0 (RFC 1945): Expires, Pragma, Last-Modified, If-Modified-Since.
1999 — HTTP/1.1 (RFC 2616): Cache-Control, ETag, If-None-Match, Vary.
2014 — RFC 7234: refines HTTP caching semantics without changing the model.
2022 — RFC 9111: current authoritative specification for HTTP caching.
How to use a cache well
Using a cache without breaking correctness
Define the TTL before the cache
A cache without a TTL is a stale-data generator waiting for an incident.
Decide how to invalidate
TTL, write-through, write-behind, or explicit invalidation. Pick one; document it.
Measure hit rate
A cache with a 5% hit rate is an expensive way to slow down requests.
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
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
MDN Web Docs (2024). HTTP caching. https://developer.mozilla.org/en-US/docs/Web/HTTP/Caching
Fielding, R. et al. (2022). RFC 9111: HTTP Caching. IETF. https://www.rfc-editor.org/rfc/rfc9111.html
Wikipedia (2024). Cache (computing). https://en.wikipedia.org/wiki/Cache_(computing)
Etymology
Origin: borrowed
cache(noun): from French cache, "hidden"; a hidden store of provisions
Semantic network
● broader ● narrower ● related
Used in the garden
- Mastering Content Curation: Build Your Own RSS Aggregator with PHP & SimplePie (4×)
- The DBA-Free Playbook: Unlocking MySQL's Hidden Power for Your SaaS (3×)
- The Zero-Downtime WordPress Migration Blueprint for High-Stakes Sites (3×)
- Changelog (2×)
- Nginx Mastery: Host Multiple Apps with One Server, Conquer LatAm Tech Costs (2×)
- Unlock Real-Time Power: Vanilla JS & SSE for Elite Dashboards (2×)
- Core Web Vitals Optimization: The Complete Technical Guide (2×)
- Serverless Savings: Cut Cloud Costs by 40% with Elegant Patterns (1×)
- Rsync Over SSH: The Ultimate Incremental Backup Strategy for Solopreneurs (1×)
- Architecting API Dominance: Design a REST API That Scales From Day One (1×)
- Technical SEO, Core Web Vitals, and Mobile SEO: The Complete Guide (1×)
- Advanced SEO, Local, E-commerce, and Future Trends: The Complete Guide (1×)
- Stremio + Torrentio: The Ultimate Free Streaming Setup (1×)
- E-commerce SEO: Optimizing Your Online Store (1×)
Sources
MDN Web Docs (2024). HTTP caching. https://developer.mozilla.org/en-US/docs/Web/HTTP/Caching
{"Fielding, R. et al. (2022). RFC 9111":"HTTP Caching. https:\/\/www.rfc-editor.org\/rfc\/rfc9111.html"}
Wikipedia (2024). Cache (computing). https://en.wikipedia.org/wiki/Cache_(computing)