ENES

Caching

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

Requests go to a fast cache first; a hit answers right away, a miss goes on to the slow database and the answer is saved in the cache for next time

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.

Keep in mind

Go deeper

Related terms

More in Software