A cache is a digital, in­ter­me­di­ate storage that retains already accessed data for sub­se­quent reuse. The following queries can then be answered directly from the cache, without having to contact the actual ap­pli­ca­tion. A typical use case is web browsers: Each browser has its own cache that’s able to tem­porar­i­ly store certain website contents. When this website is visited again at a later time, it can be loaded faster, since the retained data is requested directly from the cache, rather than the server.

De­f­i­n­i­tion

Cache: A cache (pro­nounced “cash”) is an in­ter­me­di­ate storage that retains data for repeat access. It reduces the time needed to access the data again. Caches represent a trans­par­ent layer between the user and the actual source of the data. The process for saving data in a cache is called “caching.”

To il­lus­trate the concept of caches, let’s consider the following analog example from medicine:

Imagine a dental treatment or surgical operation. The doctor asks the assistant for a utensil, like a scalpel, dis­in­fec­tant, or bandage. If the utensil has already been laid out, the assistant can im­me­di­ate­ly respond to the request and hand it over to the doctor. Otherwise, the assistant will need to locate the utensil in the medical cabinet and pass it to the doctor. After use, the assistant keeps the utensil close to hand for quick, sub­se­quent reuse.

The in­di­vid­ual utensils are not com­plete­ly unrelated in terms of their use: For instance, if the doctor asks for dis­in­fec­tant, they will likely also need a swab; a needle is also useless without sutures. The assistant will keep the as­so­ci­at­ed resources available to minimize the time required to retrieve them.

As you can see, keeping resources on hand that are often needed or used together is a very useful, com­mon­place practice. In the digital world, these processes all come under the term “caching.”

What is the purpose of a cache?

The primary purpose of a cache is to reduce the access time for important data. “Important” data means:

  1. Data that is often required: In this case, it would be in­ef­fi­cient to keep accessing the data from the slower storage un­der­ly­ing the cache. Instead, the data is supplied from the cache with quicker access times.
  2. Data that requires a time-consuming process to generate: Some data is the result of com­pu­ta­tion­al­ly intensive pro­cess­ing, or the data has to be compiled from different parts. Here, it makes sense to store the finished data in a cache for sub­se­quent reuse.
  3. Pieces of data that are needed together: It would be in­ef­fi­cient to load related pieces of data only after you have requested them. Instead, it is much more effective to hold all data available together in the cache.

How a cache works

Now let’s look more closely at how caches work. We’ll also cover how in­ter­me­di­ate storage generally operates and where it is used.

Un­der­ly­ing cache process

  1. A query for a resource is sent to the system or software that has a cache.
  2. If the resource is already contained in the cache, it is provided from the cache. This is referred to as a “cache hit.
  3. If the resource is not found in the cache, it’s first loaded from the un­der­ly­ing system into the in­ter­me­di­ate storage and then provided. This is referred to as a “cache miss.
  4. If the same resource is requested again in the future, it can likewise be supplied from the cache as a cache hit.

The diagram il­lus­trates the un­der­ly­ing process: A client sends a query for a resource to the server (1). In case of a “cache hit,” the resource is already contained in the cache and is provided to the client straight away. In case of a “cache miss,” the requested resource is not contained in the cache and is, therefore, retrieved from the system un­der­ly­ing the server (a database in this example) (2). As soon as the resource is available (3), it is provided to the client (4) and saved in the cache for reuse.

Where is a cache located?

A basic char­ac­ter­is­tic of a cache is that it is “hidden in the back­ground.” This is even reflected by the origin of the word: The word “cache” comes from French and means “hiding place.”

The cache is located in front of the actual data storage medium, invisible to the user. This means that, as a user, you don’t need to know anything about the internal prop­er­ties of the cache. You send queries to the data storage without realizing that they are actually being answered by the cache.

To delete your cache in Firefox, follow the tutorial below:

tGQ4F2tXZUc.jpg To display this video, third-party cookies are required. You can access and change your cookie settings here.

How many caches are there typically?

Generally, a range of caches are active within a digital system.

Consider accessing a website: Your browser com­mu­ni­cates with a server and retrieves a range of resources. Before the website contents are displayed to you in the browser, some of this process is likely handled by the following caches: processor cache, system cache, and browser cache on your device, as well as CDN (Content Delivery Network) and web cache on the server-side.

Ad­van­tages and dis­ad­van­tages of a cache

Whether or not an ap­pli­ca­tion has its own cache generally depends on the developer’s dis­cre­tion. We’ve sum­ma­rized the ad­van­tages and dis­ad­van­tages of the in­ter­me­di­ate storage solution below.

Advantage: Huge increase in speed

The use of a cache can provide a sig­nif­i­cant speed boost as an advantage. A speed increase by a factor of one hundred is not uncommon. However, this speed increase is only realized when the same data is accessed re­peat­ed­ly. How great this advantage actually is, therefore, varies sig­nif­i­cant­ly depending on the ap­pli­ca­tion.

Advantage: Load reduction for the system behind the cache

Since a cache delivers data extremely quickly, the load on the un­der­ly­ing system is reduced con­sid­er­ably.

Imagine the example of a dynamic HTML page generated from a PHP template: A database is accessed to generate the page. This access is rel­a­tive­ly time-consuming. Moreover, scaling database servers is not trivial – which is why database access can act as a bot­tle­neck for the overall through­put of a system. In this case, it is ben­e­fi­cial to save the generated HTML page in a web cache in order to free up the capacity of the database server for other tasks.

Dis­ad­van­tage: Cache in­val­i­da­tion is difficult

Quote

“There are only two hard things in Computer Science: cache in­val­i­da­tion and naming things.”

Phil Karlton, source: https://www.mar­t­in­fowler.com/bliki/TwoHardThings.html

The term cache in­val­i­da­tion refers to the decision of when saved data is no longer up-to-date and has to be renewed. Recall the analog example we gave earlier: The assistant acts as the cache for the doctor by keeping already used resources available for reuse. However, since there is only limited space available on the utensil tray, the assistant con­tin­u­ous­ly tidies up during the operation. Already used utensils have to be removed, and new ones added. In some cir­cum­stances, the assistant will remove a utensil that the doctor might need again. Related back to the digital world, this results in a cache miss. The assistant will then have to spend time looking for the necessary utensil again.

Since a cache miss is time-consuming, the optimal caching strategy aims to avoid them wherever possible. On the other hand, this can mean the cache supplies data that is no longer up-to-date. This problem is ex­ac­er­bat­ed when multiple caches arranged in a hierarchy are active. It can then be difficult to determine when which data in the cache should be marked as no longer up-to-date.

If a cache provides data that is no longer current, this can often result in strange issues on the provider’s side: The visited website may exhibit display errors or fragments from the past may be provided for a data request. It can sometimes be difficult to ascertain the exact origin of the issues; so clearing the cache rep­re­sents the best solution to these problems.

What types of caches exist?

Caches comprise hardware or software com­po­nents. A hardware cache is a fast buffer storage that reduces the access times for the un­der­ly­ing data storage. In principle, hardware caches are always very small compared to the overall size of the ac­cel­er­at­ed data storage.

By contrast, caches im­ple­ment­ed in software can even exceed the size of the un­der­ly­ing resources. This is es­pe­cial­ly the case when multiple versions of a resource are found in the cache.

Here’s an overview of resources fre­quent­ly equipped with caches. It shows the size of the cache, the access time for the cache, as well as how much slower access the resource without in­ter­me­di­ate storage would be.

Resource Cache Cache size Access time with cache How much slower without cache
Main storage Level 1 cache (hardware) Dozens of kilobytes (KB) Less than a nanosec­ond (ns) 200 ×
Hard drive Hard drive cache (hardware) Dozens of megabytes (MB) Hundreds of nanosec­onds (ns) 100 ×
Browser Browser cache (software) Several gigabytes (GB) Dozens of mil­lisec­onds (ms) 10–100 ×
Websites CDNs, Google page cache, Wayback Machine (software) Thousands of terabytes (petabyte, PB) Few seconds (s) 2–5 ×

Hardware caches

Processor cache

A modern processor works in­cred­i­bly quickly. The processes on the chip only need fractions of nanosec­onds – that’s a billionth of a second! In contrast, accessing main storage is com­pa­ra­bly slow at hundreds of nanosec­onds. For this reason, modern proces­sors have a hierarchy of processor caches.

A cache hit on the quickest processor cache – known as a “level 1 cache” or “L1 cache” – is around 200 times faster than access to main storage.

To delete your cache in Chrome, follow the tutorial below:

tePOBMLnG7c.jpg To display this video, third-party cookies are required. You can access and change your cookie settings here.

Hard drive cache

A hard drive disk rotates at several thousand rev­o­lu­tions per minute. The write-read head races over the disks and reads out data se­quen­tial­ly. Since this is a physical process, access to a hard drive is rel­a­tive­ly slow.

Every hard drive, therefore, has its own small cache. This way, at least the most fre­quent­ly used data – such as parts of the operating system – don’t always have to be in­ef­fi­cient­ly read from the hard drive.

The hard drive cache allows essential data to be loaded around 100 times faster. For the user, this data seems to be provided “instantly.”

Software caches

Browser cache

When visiting a website, lots of web page data is tem­porar­i­ly stored on the visitor’s device. Besides the actual content, this includes various resources like images, stylesheets, and JavaScript files. Many of these resources are typically needed on multiple pages. To speed up page loading, it’s ad­van­ta­geous to save these fre­quent­ly required resources in the browser cache on the local device.

As practical as the browser cache is for surfing the internet, it can also cause problems. For instance, if the de­vel­op­ers have made changes to a website resource, but the browser cache still contains the outdated version of the resource. In this case, display errors can occur. A solution here is then to clear the browser cache.

Google page cache

Google’s “in cache” function keeps the pages of many websites ready for access. The pages can also then be accessed, even when the original website is offline. The state of the pages cor­re­sponds with the most recent date of indexing by the Googlebot.

DNS cache

The Domain Name System, or DNS for short, is a globally dis­trib­uted system for con­vert­ing internet domains into IP addresses (and vice versa). The DNS provides an IP address for a domain name. For example, the IP address 74.208.255.134 is returned for the domain ionos.com.

Already answered queries to the DNS are stored locally on the user’s device in the DNS cache. This way, each res­o­lu­tion is always just as fast.

But using the DNS cache can likewise lead to problems – such as if the IP address as­so­ci­at­ed with a domain has changed due to a server migration, but the local DNS cache contains the old address. In this case, the attempt to connect with the server will be un­suc­cess­ful. The solution here is to clear the DNS cache.

Content Delivery Network (CDN)

Globally dis­trib­uted Content Delivery Networks provide a majority of the data of popular websites on what’s known as “edge nodes.” These edge nodes represent the data on the “edge” of the internet. The nodes are located near to the user and are tech­ni­cal­ly designed to provide data as fast as possible. A CDN works like a cache for the data of the websites it contains. This reduces access times, es­pe­cial­ly for streaming services and websites.

Web cache

A web cache provides web documents like HTML pages, images, stylesheets, or JavaScript files for reuse. Modern web caches such as Varnish and Redis store fre­quent­ly used data in the working memory and thereby aim to achieve par­tic­u­lar­ly short response times.

When the data is requested again, the response only requires extremely quick memory access. Response times are, therefore, decreased enor­mous­ly and the load for the systems behind the cache, like the web server and database, is also reduced. Other well-known web caches include OPcache and the al­ter­na­tive PHP Cache (APC).

Go to Main Menu