Tech, ExplainedData platforms and AI
Concept

Caching

Most-used data, kept in memory.

HOW IT WORKSThe app needs a pieceof dataIt looks in the cachein memory firstIf the copy is there,it is returned inunder a millisecondIf not, the appreads the databaseand keeps a copywith an expiry timeWhen the datachanges, the oldcopy is deletedMost requests ask for the same small set of data, so afast copy answers them without touching the database.HOW IT WORKSThe appneeds apiece of dataIt looks inthe cache inmemory firstIf the copyis there, itis returnedin undera millisecondIf not, theapp reads thedatabase andkeeps a copywith anexpiry timeWhen thedata changes,the old copyis deletedMost requests ask for the same small set of data, so afast copy answers them without touching the database.HOW IT WORKSThe app needs a piece of data↓It looks in the cache in memory first↓If the copy is there, it is returned inunder a millisecond↓If not, the app reads the database andkeeps a copy with an expiry time↓When the data changes,the old copy is deletedMost requests ask for the same small set of data, so afast copy answers them without touching the database.
The app looks for data in the cache first, uses the copy at once if it is there, and otherwise reads the database and keeps a copy for next time.
opens a page inside this one a page in the same scene a page in another scene a concept page a label only, with no page

Caching means keeping a copy of the most-used data in a fast place, usually the computer's memory, so that it can be read in under a millisecond instead of being fetched again from a disk or a database. The fast place, and the copy kept in it, are both called a cache. Memory, also called RAM, is many times faster to read than a disk. It is also costlier, and its contents are lost when the computer restarts. So a cache holds only the data that is asked for most often.

Here is how it works in general. When an app needs a piece of data, such as a product's price, it looks in the cache first. If the copy is there, which is called a cache hit, the app uses it at once. If it is not there, which is called a cache miss, the app reads the data from the database, which may take 10 to 50 milliseconds. It then stores a copy in the cache, with an expiry time, so that the next request is fast. When the real data changes, the app must delete or update the old copy. Otherwise readers see out-of-date data, which is called stale data. Getting this right is the hardest part of caching. Caches are often run on a key-value store, which finds each record by one key, very quickly.

Take, for example, a news website during breaking news. Website caching builds the story page once and serves the same copy to lakhs of readers, instead of building it again for each one.

Consider a shopping app during a festival sale. It keeps each product's price and stock count in a cache for a few seconds, so that its database is not asked the same question millions of times a minute.

For instance, your phone's browser keeps a website's logo and fonts after the first visit. The next time you open the site, it loads faster, because those files do not travel over the network again.

How is a cache different from a database? A database, such as a relational database, is the official record. It is kept safely on disk and must never lose data. A cache is a disposable copy. If it is lost, the app rebuilds it from the database, only more slowly for a moment. Some products combine the two: a document database may come with a cache built in. A content delivery network is a cache too, placed in many cities so that each copy sits near its readers.

Appears in

Data platforms and AI
Gaming, streaming and media
Tech, Explained is a part of ingrid.fyi. Every page explains one piece of technology in plain words.