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
- Document database↗ Data engineeringA document database often keeps its most-read documents in memory, so the app gets them without reading the disk.
- Key-value store↗ Data engineeringA key-value store keeps its data in memory, so a payment system can read a record in under a millisecond.
- Website caching↗ Digital publishingWebsite caching keeps ready-made pages in memory, so the server does not build each page again for every reader.