Caching

A temporary storage technique that stores frequently accessed data closer to the requestor to speed up retrieval times.

Caching operates at almost every level of a modern computing stack:
- Hardware Level: CPUs have L1, L2, and L3 caches built directly into the silicon to prevent the processor from waiting on slower RAM.
- OS Level: Operating systems cache file system reads in unused RAM.
- Application Level: Web applications use in-memory databases like Redis or Memcached to store frequent database queries.
- Network Level: Content Delivery Networks (CDNs) cache static assets (images, videos, HTML) on servers geographically close to the end-user to minimize network [latency](/en/terms/network-latency).
- Browser Level: Web browsers cache website assets locally on your hard drive so they don't have to be re-downloaded on every visit.

        graph LR
  Center["Caching"]:::main
  Rel_file_systems["file-systems"]:::related -.-> Center
  click Rel_file_systems "/terms/file-systems"
  Rel_cpu_cache["cpu-cache"]:::related -.-> Center
  click Rel_cpu_cache "/terms/cpu-cache"
  Rel_cache["cache"]:::related -.-> Center
  click Rel_cache "/terms/cache"
  classDef main fill:#7c3aed,stroke:#8b5cf6,stroke-width:2px,color:white,font-weight:bold,rx:5,ry:5;
  classDef pre fill:#0f172a,stroke:#3b82f6,color:#94a3b8,rx:5,ry:5;
  classDef child fill:#0f172a,stroke:#10b981,color:#94a3b8,rx:5,ry:5;
  classDef related fill:#0f172a,stroke:#8b5cf6,stroke-dasharray: 5 5,color:#94a3b8,rx:5,ry:5;
  linkStyle default stroke:#4b5563,stroke-width:2px;

      

🧠 Knowledge Check

1 / 5

🧒 Explain Like I'm 5

Imagine you have to look up a phone number in a giant, heavy phone book in another room every time you want to call your mom. It takes a long time. Caching is like writing her number on a sticky note and putting it right on your computer screen. The sticky note is small (it can't hold the whole phone book), but it holds the number you need most often so you can find it instantly.

🤓 Expert Deep Dive

Locality of Reference
Hardware caching (L1/L2/L3 CPU caches) relies on two principles: Temporal Locality (if data is accessed, it will likely be accessed again soon) and Spatial Locality (if data is accessed, data at nearby memory addresses will likely be accessed soon).

Eviction Policies
Because caches are inherently smaller than primary storage, they must evict old data to make room for new data. Common algorithms include Least Recently Used (LRU), Least Frequently Used (LFU), and First-In-First-Out (FIFO).

The Hard Problem: Invalidation
Software engineer Phil Karlton famously said, 'There are only two hard things in Computer Science: cache invalidation and naming things.' Cache invalidation is the complex process of ensuring that when primary data changes, the cached copy is updated or purged so users do not receive stale (outdated) data.

❓ Frequently Asked Questions

Why not just make the cache big enough to hold everything?

Cost and physics. Cache storage (like SRAM inside a CPU or RAM for a database) is extremely fast but very expensive per gigabyte. Primary storage (like hard drives or SSDs) is much cheaper but much slower. Caching provides the best of both worlds.

What does it mean to 'clear your cache'?

When you clear your web browser's cache, you are deleting the temporary files (images, scripts) it saved from websites you visited. This is often necessary if a website updates its design but your browser is stubbornly loading the old, cached version.

What is a 'cache miss'?

A cache miss occurs when a system looks for data in the cache but doesn't find it. It must then fetch the data from the slower primary storage, which takes longer, and then it typically copies that data into the cache for next time.

📚 Sources