# Caching

> Keeping a copy of an answer close by so you do not have to work it out again.

- Level: Intermediate
- Topics: Software
- URL: https://tidbit.legios.com.ar/caching/
- In Spanish: [Caché](https://tidbit.legios.com.ar/es/caching/)

**Caching** means saving the result of something slow or expensive, a database query, a web page, a photo, a calculation, in a place that is fast to read, so the next time someone asks for it you hand over the saved copy. That fast place is the **cache**. When the answer is there, it is a **cache hit**; when it is not, it is a **cache miss**, and you do the slow work and usually store the result for next time.

Caches are everywhere in a single click. Your browser keeps images and files from sites you visit. A CDN keeps copies of pages in cities near you. The application keeps popular database results in memory, often in a tool like Redis. The database keeps hot data in RAM instead of on disk. Even the processor has its own CPU cache. Each layer saves a trip to something slower.

The hard part is knowing when a saved copy is out of date. If a price changes but the cache still holds the old one, users see the wrong price. Common answers are giving each item an expiry time (a TTL, time to live) or deleting it from the cache whenever the original changes, which is called **cache invalidation**. Getting it wrong is a classic source of confusing bugs, which is why programmers joke it is one of the two hard problems in computer science.

## An analogy

Writing a friend's phone number on a sticky note on the fridge instead of searching your contacts every time. It is much faster, until your friend changes numbers and the note is still there.

## Keep in mind

- A cache stores copies of slow-to-get results in a fast place.
- A hit is served from the cache; a miss does the slow work and saves the result.
- Browsers, CDNs, apps, databases and processors all cache.
- The hard part is invalidation: not serving stale data.

## Go deeper

- [Cache (computing) on Wikipedia](https://en.wikipedia.org/wiki/Cache_(computing))
- [HTTP caching (MDN)](https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/Caching)

## Related terms

- [Database](https://tidbit.legios.com.ar/database/): An organized store of data that programs can search and update safely.
- [Website](https://tidbit.legios.com.ar/website/): A set of web pages under one address, stored on a server and opened in a browser.
- [Web browser](https://tidbit.legios.com.ar/web-browser/): The app that fetches web pages from the internet and draws them on your screen.
- [CDN](https://tidbit.legios.com.ar/cdn/): Copies of a website kept on servers around the world, close to whoever asks.
- [RAM](https://tidbit.legios.com.ar/ram/): The computer's short-term memory: fast, small and wiped when you turn it off.
- [CPU](https://tidbit.legios.com.ar/cpu/): The chip that runs your programs, one instruction after another, very fast.
- [CPU cache](https://tidbit.legios.com.ar/cpu-cache/): A tiny, very fast memory inside the processor that keeps the data it is about to need.
- [Bug](https://tidbit.legios.com.ar/bug/): A mistake in software that makes it behave differently from what was intended.
