Skip to main content

Cache in Computer Science

The Internet is an important part of modern life and a key part of many important international operations. New services like e-commerce, video streaming on demand, and home-working platforms have been made possible by the Internet. Because these services could have big economic and social benefits in the long run, they are essential to modern life. The Internet is a technology that makes it easier for networks to talk to each other and share information. Almost any kind of information can be sent over the Internet, from important information about how utility networks work to remote working platforms for businesses and consumer-focused services like online shopping and video streaming. Much of the known information is part of the World Wide Web which enables Internet users to discover and access the content. Caching is now included in the content distribution chain between hosts (who store content but aren't always the content providers) and users (who access it).

Caches, which are made up of both hardware and software, are steps in between that let certain types of content be temporarily copied in many places closer to customers to make the content easier to send.

This article will discuss the various cache dimensions, including the definition of cache, the number of cache types, their application areas, as well as their pros and cons.

What is a Cache in Computing?

In computing, a cache is a fast layer of data storage that holds a subset of data that is usually temporary. This is done so that requests for this data can be met faster than if the data's main storage location had to be accessed. Caching allows you to efficiently reuse previously retrieved or computed data.

The cache is software or hardware used to temporarily store information, often data. It is a minor kind of faster, more expensive memory used to improve the performance of data that is frequently accessed. Temporarily, cached data is stored on a storage medium accessible only to the cache user and distinct from the primary storage. The central processing unit (CPU), apps, web browsers, and operating systems all use cache.

The cache is employed because the primary or bulk storage cannot keep up with the needs of the users. Cache reduces data access times, decreases latency, and boosts input and output (I/O). Almost all application workloads rely on I/O activities; hence, the caching technique improves application performance.

There are hardware and software caches.

The CPU is the part of your desktop, laptop, smartphone, or tablet that processes software data. It has its own cache. This CPU cache is a small memory block that helps the CPU get to data that is often asked for. It holds information that the main memory of the device uses to run instructions much faster than if it had to load each piece of data only when it was needed.

Every web browser has its own cache, like Microsoft Edge, Google Chrome, Firefox, and Safari. A browser cache stores files required for displaying online pages accessed by the browser. This includes the HTML document that specifies the website, the CSS guidelines, Javascript, cookies, and images.

Let's keep looking at why caching is now one of the most important topics in computer science.

What are the Types of Caches?

There are several cache kinds, and each serves a unique purpose. Your gadget has its own cache, which consists of many tiers. The goal of this CPU cache is to get data from your device that you use often. Other caches pertain to web browsers and applications, each of which uses a distinct cache type.

Types of caches are listed below:

  1. Memory Cache
  2. Web Cache
  3. Application/Software Caches
  4. Data Caching
  5. Application/Output Caches
  6. Distributed Caching

Types of Caches

Figure 1. Types of Caches

1. Memory Cache

We may categorize Memory Cache as follows:

  • Primary Cache L1

  • Secondary Cache L2

  • Cache in Main Memory L3

Some of these caches interact directly with the central processing unit (CPU) of your device. Others are external caches, which store data away from the CPU but make it easy to get it back when needed. However, they all focus on caching the data that your device needs to operate efficiently and accurately.

  • Primary Cache L1: This sort of cache is a component of your device's central processing unit. Its memory ranges from 2 to 64 kilobytes on average. So, the primary cache of your device is not meant to hold a lot of information. Instead, it functions similarly to random-access memory.

  • Secondary Cache L2: Secondary caches function as intermediaries between your device's processor and its main memory. If your device has a cache miss, it usually looks in the secondary cache to see if it can find the data. A cache miss is any attempt to retrieve an instruction from the cache that fails. In this case, your device might not be able to load an instruction into its primary cache quickly enough, so it checks its secondary cache instead.

  • Main Memory L3 Cache: The term "main memory cache" is rather deceptive, as this cache is typically quicker than your device's main memory. However, it is not stored near the CPU, hence the term. This is the biggest of the three caches associated with the CPU. L3 cache is usually between 1 and 8 megabytes in size, which lets it store a lot more instructions. Typically, if you have a multicore CPU, your L3 cache will serve all of the cores simultaneously.

2. Web Cache

Browser caching is a type of site caching that is built into a user's web browser. The visitor's browser saves parts of your site along with other files that are linked to your content. This makes it easy to get to the information needed to load pages faster. Without web caching, every time you go to a website, your browser has to send a new request. A browser's cache can store HTML pages, CSS stylesheets, pictures, and other types of multimedia data.

Both browser caching and site caching happen on the client side, so they are similar. The main difference is that the cache is controlled by the browser, not by the user. Every browser has a cache that automatically gets rid of files that are no longer needed.

If a website's static content is already in the cache of the device, it can send it quickly. Delays only occur when the user views previously unseen content.

The purpose of the various types of web caches is to reduce the number of server queries made by your browser. Fewer server queries make pages load faster and reduce the amount of work a website's server has to do. Webmasters save money on network charges as a result of this decreased network demand.

There are four primary kinds of web caching:

  • Site Cache: Site cache, also known as page cache, caches information about a website the first time a user visits it. When you go back to a website, it pulls the saved information from the site's cache to make some parts load faster.

  • Browser Cache: Your web browser's browser cache is a form of the site cache. Like a site cache, it keeps a cached copy of most of the websites you visit. This helps the browser load pages more quickly, enhancing your web browsing experience. Browser caches work by saving parts of websites, such as HTML pages, multimedia data like photos, and CSS stylesheets. Then, it puts these pieces together with other files related to the website's content to make navigation faster. Images and materials that have been cached load faster since the browser does not need to download them again to show them.

  • Micro Cache: Many website owners and online users are unaware of this cache type. The micro cache temporarily saves stuff to ensure that it displays appropriately. For instance, it is frequently used to store the static components of dynamic content. The micro cache may temporarily store files for up to 10 seconds. This sort of web cache is the least frequent due to its little storage period. Few websites can profit from its implementation. Those that do often include dynamic material with static components.

  • Server Cache: Server cache is an umbrella word for many types of cache memory. Object caching, opcode caching, and content delivery network (CDN) caching are examples. This is the only type of online caching over which you have no control. Because server caches keep frequently visited material on the website's server, this rule applies to the website's owner. The server cache is used by website owners to minimize server loads. Content saved in a server cache can be immediately sent to a browser upon request. This enables the website to manage greater traffic while delivering content more quickly. The website will retrieve data from the cache instead of traversing the website via the server to find its usual memory location.

3. Application/Software Caches

Most apps have their own caches to store files and data that the developer thinks users will want to access quickly. These caches let the application get frequently used user data from the cache memory and give it to users.

The information saved in software caches varies by application. Some websites retain your search history and preferences. Others may save video previews or backgrounds. No matter what is stored, all software caches try to minimize latency and give users a cheap way to get back to the frequently requested information.

With the Application Cache (or AppCache), a developer can tell the browser which files to store and make available to users who are not online. Even if the user presses the refresh button when they're not online, your app will load and work fine.

4. Data Caching

If a website or application has to obtain data from a database, data caching is most likely implemented. Input and output requests required to get data from databases are hardware-intensive. If customers often request the same set of data that does not vary over time, it is likely that the developer will cache the data to offer it more rapidly.

On the web or application server, the data cache is maintained. The storage of data in these caches reduces server demand, making the use of data caches a more cost-effective method of delivering data.

5. Application/Output Caches

This is the sort of cache often employed by content delivery networks. Application and output caching is a feature of several content management software systems. Like most of the other cache types on this list, they reduce the load on the server by caching frequently used static content. This cache speeds up applications by making it faster to load pages with mostly written information.

6. Distributed Caching

This sort of cache is reserved for professionals. Google and Amazon employ distributed caching because they operate high-volume systems. This cache enables large corporations to keep data across several database servers. Each of these database servers' data is cached on separate web servers. These web servers provide data to distributed cache applications.

Distributed caching is distinguished from other cache types by the fact that it never runs out of storage capacity. If a storage limit is going to be exceeded, the corporation may add more servers to its pool without affecting the customer experience. So, distributed caching is a great way for high-traffic systems to handle an increase in the number of user visits.

Caches: How are they used?

The main goal of a cache is to speed up data retrieval by reducing the need to access the slower storage layer underneath. In exchange for speed, a cache usually only stores a small subset of data for a short time. This is different from databases, where the data is usually complete and lasts for a long time.

Let's look at a few common uses of caching. Your use case may be the same as one of these.

  • Database Caching

  • Content Delivery Network (CDN)

  • Domain Name System (DNS) Caching

  • Session Management

  • Application Programming Interfaces (APIs)

  • Caching for Hybrid Environments

  • Web Caching

  • General Cache

  • Integrated Cache

  • Mobile

  • Internet of Things (IoT)

  • Advertising Technology

  • Gaming

  • Media

  • Ecommerce

  • Social Media

  • Healthcare and Wellness

  • Finance and Financial Technology

The most common use cases for cache are outlined below:

  • In-memory data lookup: If you have a mobile or web application front end, you may choose to cache information such as user profiles, historical or static data, or API responses based on your use cases. Caching will aid in the storage of such data. 2-dimensional arrays or hash maps also function as caches while developing a dynamic programming solution for a given problem.

  • RDBMS Speedup: Relational databases are sluggish when processing millions of records. Unnecessary or outdated data, as well as large volumes of data, can cause a database's index to run more slowly. Regardless of whether you use sharding or partitioning, a single node can still face delays and latency in query response when the database is at capacity. In such situations, it is likely that many "SELECT" or "read" queries can be externally cached for at least a little duration. Relational databases also employ their own caching, however, external caching can have far more capacity than internal caching for improved speed. It is one of the most common applications of caching.

  • Session Store: Active online sessions are often visited data, whether you need to perform API authentication or keep current cart information in an e-commerce application, the cache can serve sessions effectively.

  • Token Caching: API Tokens can be stored in memory for efficient authentication and validation of users.

  • Gaming: The player profile and leaderboard are two of the most frequently visited displays in online multiplayer games. With millions of gamers, it becomes crucial to update and retrieve such data quickly. Caching is also applicable to this use case.

  • Web Page Caching: To develop a lightweight and adaptable user interface for a mobile or web application, you may construct dynamic web pages on the server and send them through API with the relevant data. Therefore, if you have millions of visitors, you may provide full/fragmented web pages built on the fly from the cache for a limited duration.

  • Global ID or Counter Generation: If you have a variable number of relational database instances across nodes and you want to produce an auto-incrementing primary key for them, or if you want to provide unique ids to your users, you may utilize caching to get and update such data at scale.

  • Fast Access To Any Suitable Data: Frequently, we believe that cache is solely utilized to store frequently accessed material for reading. Although generally accurate, this behavior can vary depending on use situations. A cache can be used to store less frequently accessed data if rapid access is required. We utilize cache to retrieve data quickly, therefore keeping often accessed vs seldom accessed data is a question of use case.

What are the Benefits of Caches?

Caching helps both people who use content and people who make content. Some of the ways caching make content delivery better are as follows:

  • Boost the way an Application works: Because memory is many times faster than disk (magnetic or SSD), it is very fast to read data from an in-memory cache (sub-millisecond). This much faster access to data makes the application work better as a whole.

  • Cut the Cost of Databases: A single cache instance can provide hundreds of thousands of IOPS (Input/Output Operations Per Second), which could replace several database instances and lower the total cost. This is especially important if the main database charges based on how much data is sent and received. In those situations, you could save dozens of percentage points on the price.

  • Lessen the Burden on the Backend: Caching can reduce the load on your database by moving a large portion of the read load from the backend database to the in-memory layer. This keeps your database from slowing down when it's busy or even crashing when it gets too busy.

  • Provide Predictable Results: Dealing with times when a lot of people use the app at once is a common problem in modern apps. Social apps during the Super Bowl or on election day, e-commerce sites on Black Friday, etc., are all examples. When the database is busy, it takes longer to get data, which makes the overall performance of the application hard to predict. This problem can be fixed by using an in-memory cache with high throughput.

  • Eliminate Database Hotspots: In many applications, a small subset of data, like a profile of a celebrity or a popular product, is likely to be accessed more often than the rest. This can cause "hot spots" in your database and may require you to have more database resources than you need based on how often your most-used data is accessed. Putting common keys in an in-memory cache reduces the need to overprovision and makes sure that the most frequently accessed data works quickly and reliably.

  • Increase Read Throughput (IOPS): Along with less latency, in-memory systems have much higher request rates (IOPS) than a similar disk-based database. A single instance used as a distributed side-cache can handle hundreds of thousands of requests per second.

What are Caches' Shortcomings?

Here are the disadvantages of utilizing a device cache:

  • Performance Issues: Sometimes, temporary files saved or opened from a cache don't work right. When cache files get corrupted, they can cause applications or web pages to crash or show elements in the wrong way.

  • Delayed Updates: Even though caches should only store data that can be used again and again for a short time, they sometimes keep older versions of data that have been updated, like on a website. For the newer version to show up, the user would have to clear the cache in their browser.

  • Consumed Storage: Due to the fact that caches utilize your device's local storage, they might consume space that could be used for other purposes. If your device's storage grows too full, your computer may become slower. Additionally, it might slow down your programs and online browsers. To limit the amount of storage your caches consume, it might be good to clean them periodically to lower their size and enhance the efficiency of your device.

  • Corruption: Sometimes caches provide security threats to individuals because hackers can add malware and viruses to the information and data they store. These infections can more readily access your computer and personal files, posing extra obstacles and dangers. If you're concerned about potential security threats, it's crucial to delete your cache as frequently as possible or use private windows when browsing the internet. Private windows do not save information about visited websites or search history. Typically, they also remove all cached data when you exit the browser.

  • Security risks: As each cache stores copies of the most recently used data, it makes users more vulnerable to cyberattacks by giving hackers more ways to get at their data. For example, web browser caches store cookies, transaction history, personally identifiable information (PII), and other sensitive information like usernames and passwords.

    Even HTTPS caches don't store encrypted versions of files locally, so this information can be easily used to do bad things. This risk is increased when you use public WiFi networks without clearing your browser's cache since these networks often have poor security.

    Domain Name System (DNS) caches can also be hacked using techniques like DNS cache poisoning (or DNS spoofing). When false DNS information is put into a DNS resolver's cache, this is called DNS cache poisoning. This information makes DNS queries return the wrong answer, which often sends users from a good website to a bad one that is trying to steal sensitive information or install malware.

  • Lack of data privacy: Although it is advantageous for websites to utilize caches to display relevant adverts, storing and sharing sensitive information in caches might pose a privacy risk. This information may include your purchasing patterns, websites you've visited, and things you placed in your shopping basket but did not complete the purchase for. If you do not want other websites to have access to this information, you can use private browsing windows, you should clear your caches more often or refuse cookies and cached data from websites that ask for it.

How to Clear Cache?

Depending on the machine, there are different ways to get rid of cached data. You can clear your web browser's cache by going to the Settings menu and clicking on "Clear browser data" in the "Privacy" section.

Since there is usually no settings tab, it can be much harder to clear data on a server. To delete the chase manually, you would have to go to the root folder and then find where it is stored. Most of the time, developers will set up tasks that will automatically clear the cache after a certain amount of time.

How often should you clear your cache in Chrome?

The cache and cookies of a browser like Chrome store some data from websites that you visit. Some issues, like websites that won't load or don't look right, can be fixed by clearing them.

Cookies are small files that are made by websites you visit. They make it easier to use the Internet by saving your browsing data. The cache remembers parts of pages, like images, so that the next time you visit, they load faster.

What is a Buffer Cache?

The whole point of the buffer cache is to keep processes from having to wait for data to be read or written from or to slow disks. So, it wouldn't be helpful to write a lot of data at once. Instead, data should be written in small chunks at regular intervals so that I/O operations have a small effect on the speed of user processes and the time it takes for humans to see results.

The kernel keeps a lot of information about each buffer to help it decide when to write. This includes a "dirty" bit that tells it when the buffer has changed in memory and needs to be written, and a timestamp that tells it how long it should stay in memory before being flushed to disk. Information about buffers is stored in buffer heads, which we learned about in the last chapter. These data structures need to be taken care of along with the user data buffers themselves.

The size of the buffer cache can be different. When a new buffer is needed but none is available, a page frame is made when one is not already there. When there isn't much free memory, buffers are freed up and the page frames that go with them are recycled.

There are two different kinds of data structures in the buffer cache:

  • A group of buffer heads that tell what each buffer in the cache is.

  • A hash table that helps the kernel quickly find the buffer head that describes the buffer for a given pair of devices and block numbers.

The main difference between cache and cookie is that cache is used to store web page resources in a browser for long-term use or to speed up loading time. On the other hand, cookies are used to remember things like a user's browsing session so that the user's preferences can be found.