+4 votes
181 views
in Technical issues by (242k points)
reopened
The cache: what it is and how it works

1 Answer

+5 votes
by (1.6m points)
edited
 
Best answer

What is a cache?
What is the purpose of a cache?
How does a cache work?
Advantages and disadvantages of caches
What types of caches are there?
Basic outline of a cache
Where is the cache located?
How many caches are there usually?
Advantage: huge increase in speed
Advantage: reduced load for the system behind the cache
Disadvantages: cache is difficult to invalidate
Hardware caches
Software caches

image

The cache: what it is and how it works

In digital systems, data is accessed continuously, for example from a database, a web server or a hard drive. Accessing the data always takes time, so it is advisable to reduce it as much as possible. This allows more accesses to be processed per unit of time. Also, a good user experience depends on how fast a server responds..

A cache significantly reduces the access time to data that must be accessed multiple times, which is why this buffer is used by almost all systems and applications. Here we explain what exactly this technology consists of, what advantages and disadvantages it has and what types of caches exist.

Index
  1. What is a cache?
  2. What is the purpose of a cache?
  3. How does a cache work?
    1. Basic outline of a cache
    2. Where is the cache located?
    3. How many caches are there usually?
  4. Advantages and disadvantages of caches
    1. Advantage: huge increase in speed
    2. Advantage: reduced load for the system behind the cache
    3. Disadvantages: cache is difficult to invalidate
  5. What types of caches are there?
    1. Hardware caches
      1. Processor cache
      2. Hard drive cache
    2. Software caches
      1. Browser cache
      2. Google Page cache
      3. DNS cache
      4. Content Delivery Network (CDN)
      5. Web cache

What is a cache?

A cache is a type of digital buffer that stores the data once retrieved for later access. When the data is requested again, the query can be answered directly from the cache, without having to contact the application itself. A typical use case is web browsers, which have their own cache to temporarily store some website content. If you visit the pages again later, they will load faster, as the data will be retrieved directly from the cache and not from the server..

Definition

Cache - Cache is a buffer that contains data that will be accessed multiple times and allows faster retrieval. Caches are a transparent layer between the user and the actual source of the data. The process of caching data is called caching .

To explain how a cache works, let's take an analogy from medicine: imagine that, during dental treatment or surgery, the dentist asks his assistant for a resource, such as a scalpel, a disinfectant or a bandage. If the resource is ready , the assistant will be able to respond to the request immediately. If you do not have it, the assistant should look in the medicine cabinet to deliver the resource to the doctor. After the clinician uses it , the aide will leave the resource ready for quicker access later.

Resources are not used completely independently: if the doctor asks for a disinfectant, he will certainly need gauze; to use a needle, you need a thread. The wizard will have the necessary resources ready to minimize access time during use ..

As you have seen, keeping frequently needed resources close at hand or grouping those that are used together is a very useful and extremely common procedure. In the digital world, all these related processes are summed up under the term caching .

What is the purpose of a cache?

The main purpose of the cache is to reduce the access time to important data. Among the data considered important are the following:

  1. Frequently used data - For these, it would be a waste of time to always have to load them from slower memory. Instead, they are delivered from the cache, which is faster.
  2. Complex data to generate : some data is the result of intensive computational processing or must be combined from different sources. In these cases, it is a good idea to cache the ready data for future use.
  3. Data to be used together : In this case, it would not be efficient to load the data only when requested. Instead, it makes sense to keep the data together in the cache.

How does a cache work?

Here we explain in detail how a cache works. Among other things, we describe the basic operation of the buffer and where it is located.

Basic outline of a cache

  1. A resource is requested from the system or program that has a cache memory.
  2. If the resource is already in it, it is delivered from here. This is known as a cache hit , that is, a cache hit .
  3. If the resource is not in it, it is first loaded from the underlying system and then delivered. This case is known as Miss cache , or I cache miss .
  4. If the same resource is requested again in the future, it is delivered from the cache as a cache hit.
image
The operation of a cache, illustrated with the example of a web cache

The image illustrates the basic scheme : a client requests a resource from the server (1). In case of cache hit, the resource is already in the cache and is immediately delivered to the client (2). In the case of cache miss , the requested resource is missing and therefore the underlying system is requested from the server (in this case, a database) (2). As soon as the resource is available (3), it is delivered to the client (4) and cached for later use.

Where is the cache located?

A basic property of caches is that they work in a hidden way . This fact is already reflected in the origin of the word: the word cache comes from French and means? Hidden ?.

Unlike actual data storage, the cache is invisible to the user. This means that you, the user, should not know anything about the internal properties of a cache. When you make requests to data memory , you don't realize that they actually come from the cache.

How many caches are there usually?

Typically, in a digital system, there are always multiple caches active.

Imagine that you access a website: the browser communicates with a server and accesses a series of resources. Before the content of the page is shown to you in your browser, parts of it will probably go through the following caches: the processor caches, the system caches, the browser caches on your device and the CDN (Content Delivery Network) and the web cache on the server side.

Advantages and disadvantages of caches

The fact that an application is equipped with a cache is, in principle, in the hands of the developer. Here we summarize the advantages and disadvantages of a cache solution .

Advantage: huge increase in speed

Using a cache has the potential advantage that speed is greatly increased. It is not unusual to achieve acceleration by a factor of one hundred. However, this acceleration only occurs when the data is accessed repeatedly, and the actual acceleration will vary greatly depending on the case.

Advantage: reduced load for the system behind the cache

Because the cache delivers data very quickly, the load on the underlying system is greatly reduced.

As an example, imagine a dynamic HTML page generated from a PHP template: to display the page, you access a database. This access requires a relatively large effort. Additionally, scaling up database servers is non-trivial and can cause bottlenecks in database access , which can limit overall system performance. In this case, it is appropriate to save the generated HTML page in a web cache to use the capacity of the database server for other tasks.

Disadvantages: cache is difficult to invalidate

Appointment

? There are only two hard things in Computer Science: cache invalidation and naming things ?.
(In computing there are only two difficult problems: invalidating the cache and naming things?).

Phil Karlton, www.martinfowler.com/bliki/TwoHardThings.html

The term cache invalidation refers to when it is decided that the cache data is no longer up to date and needs to be refreshed. Remember the previous analogy: the assistant helps the doctor by making the resources already used available for later use. However, since the available space is limited , the assistant is constantly clearing it during the operation. Resources that have already been used must be set aside, and new ones must be added. In some cases, the aide may withdraw a resource that the doctor still needs. In this case, the cache miss concept is used : the assistant must re-dedicate time and effort to locating the necessary resource.

Since cache misses are expensive, the best caching strategy is to avoid them whenever possible. However, this can lead to outdated data from the cache. This problem is compounded if there are multiple active and hierarchically adjacent caches. It can be difficult to determine when the data in the cache should be marked as up to date.

If a cache delivers data that is no longer up to date, the user often experiences problems: the visited website has display errors or old fragments appear when retrieving the data. Sometimes finding the exact source of the problem is not easy, so in that case it is recommended to clear the cache.

What types of caches are there?

Caches consist of hardware or software components. A hardware cache is a fast buffer that reduces access times to the underlying data memory. In principle, hardware caches are always very small compared to the total size of accelerated memory.

Instead, the caches implemented in the software can be even larger than the underlying resource. This is particularly the case if there are multiple versions of a resource in a cache.

Here is a summary of common resources caching shown, including the size of the cache , the access time to it and an indication of how much it would take to access resources unbuffered.

Resource Cache Cache size Cached access time × slowest time without cache
Principal memory Level 1 cache (hardware) Tens of kilobytes (KB) Less than a nanosecond (ns) 200 ×
HDD Motherboard cache (hardware) Tens of megabytes (MB) Hundreds of nanoseconds (ns) 100 ×
Browser Browser cache (software) Multiple gigabytes (GB) Tens of milliseconds (ms) 10-100 ×
Websites CDN, Google Page Cache, Wayback Machine (software) Thousands of terabytes (petabyte, PB) Less seconds (s) 2-5 ×

Hardware caches

Processor cache

A modern processor works incredibly fast. Processes on the chip only require fractions of nanoseconds, that is, billionths of a second! In contrast, access to main memory is comparatively slow: it lasts a few hundred nanoseconds. Because of this, modern processors have a hierarchy of processor caches .

A cache hit from the fastest processor cache, known as a level 1 cache or L1 cache ( level 1 cache ), is about 200 times faster than accessing main memory.

Hard drive cache

A hard drive spins at several thousand revolutions per minute. The read-write head cycles through the disks and reads the data in sequence. Being a physical process, accessing a hard drive is comparatively slow.

For this reason, each hard drive has its own little cache . In this way, at least the most used data , such as parts of the operating system, does not have to be read each time from the hard disk, with the corresponding effort.

The hard drive cache allows you to load essential data about 100 times faster. This makes them available? Immediately? for the user.

Software caches

Browser cache

When visiting a website, much of the page data is temporarily stored on the visitor's device. In addition to the actual content, this includes resources such as images, style sheets, and JavaScript files . In general, many of these resources are required on multiple pages. For the pages to load faster, it is advantageous to store these in the browser cache of the local device.

Although browser caches are very useful for browsing the Internet, they can also cause problems. For example, it sometimes happens that developers make changes to a resource on the website, but the browser cache still contains the old version of the resource . In this case, display errors may occur. To fix it, you can empty the browser cache.

Google Page cache

The function? Cached? Google stores the pages of many websites . This means that the pages can be accessed even if the original website is offline. The status of the pages corresponds to the date of the last Googlebot indexing.

DNS cache

The domain name system, known as DNS, is a system used throughout the world for translating internet domains into IP addresses (or the other way around). DNS returns an IP address for a domain name. For example, for the domain ionos.es , it returns the IP address 217.160.86.33 .

Queries already answered to DNS are stored locally in the DNS cache of the device itself, which means that each resolution is always just as fast.

However, using the DNS cache can also cause problems, for example if the IP address belonging to a domain has changed due to a server move, but the old address is still cached in the local DNS cache. In this case, the connection to the server fails. This is solved by clearing the DNS cache.

Content Delivery Network (CDN)

Content Delivery Networks (content distribution networks), implemented throughout the world, keep much of the data from the most popular websites in the so-called edge nodes or peripheral nodes. These edge nodes reflect the data at the? Edge? from Internet. The nodes are located locally close to the user and designed to deliver the data as quickly as possible. A CDN acts as a cache for the data on the websites it contains, minimizing access times, especially to streaming providers and websites.

Web cache

A web cache contains web documents such as HTML pages, images, style sheets, or JavaScript files for repeated use. Modern web caches, such as Varnish and Redis, store frequently used data in RAM, resulting in greatly reduced response times.

When the data is queried again, the response only requires very fast memory access . This greatly reduces response times and the load on underlying cache systems such as the web server and database. Other well-known web caches are OPcache and the alternative PHP Cache (APC).


...