A cache is a temporary data storage location that stores copies of frequently accessed data or files to provide faster access to software or hardware. Computers, mobile devices, web browsers, and other applications use cache to speed up data retrieval.
Caches allow faster access to this data by removing the need to reload it each time the device/app needs it. Cached data is reusable and can be retrieved directly.
How Does a Cache Work?
A cache works by temporarily storing frequently used data, which provides devices and applications with a faster way of accessing it.
Caches store copies of data that are readily available and small in size. This functionality removes the need to search larger memory banks, which takes much longer.
The precise way a cache works depends on the type of software or hardware that is using it.
Types of Caches
Many different devices and technologies use caching to optimize system performance. The way they use and store cache varies between devices.
Below are examples of the main types of caches.
1. CPU (Central Processing Unit) Cache
Desktop computers, laptops, smartphones, and tablets use Central Processing Units (CPUs) to process and execute instructions.
These devices need frequent access to specific data to run processes and applications efficiently. They use internal Random Access Memory (RAM) to access this data faster.
RAM temporarily stores the data actively being used by the CPU to run current processes.
Without RAM, such devices, e.g., a computer, would have to access this data from other components like the hard drive or solid-state drive (SSD), which offer longer-term storage but are also much slower.
There are two types of RAM: Dynamic RAM (DRAM) and Static RAM (SRAM).
DRAM stores memory using capacitors which must be refreshed constantly to retain data. As these capacitors are powered by electricity, DRAM loses its data every time the computer is switched off.
SRAM does not need to be refreshed constantly, making it much faster and more expensive than DRAM. SRAM is used for CPU cache.
Computers use CPU cache, also called cache memory, to run more efficiently. A CPU cache is a memory chip that temporarily stores data that the CPU frequently accesses from RAM.
The CPU checks the memory cache first when it needs to access data for actively running processes. A 'cache hit' occurs when the required data is cached, optimizing performance.
A 'cache miss' occurs when data is not cached. Cache misses cause latency as the CPU must access the data further down the memory hierarchy, e.g. from RAM.
Memory Hierarchy
The memory hierarchy structures computer memory based on its response time. Cache is at the top of the pyramid as it runs the fastest.
Its speed is partially due to the fact it is located much closer to the CPU than other memory types. It can even be directly integrated into the CPU chip. Caches also have the smallest storage capacity, which lowers latency.
The cost and speed of each type of memory depend on its capacity size, as reflected in the memory hierarchy shown below.
Cache Levels
CPU cache is graded into three different levels: Level 1 (L1), Level 2 (L2), and Level 3 (L3). The levels are ordered from highest speed and smallest size to lowest speed and largest size.
- Level 1 Cache: L1 cache is usually integrated into the CPU chip and offers the highest speed. The CPU accesses L1 cache most frequently.
- Level 2 Cache: L2 is located between the CPU and the RAM and larger than the L1 cache, but slower. The CPU accesses L2 when required data isn't found in L1.
- Level 3 Cache: L3 is the slowest cache level and the largest. The CPU accesses L3 when required data isn't found in either L1 or L2. If there is a cache miss, the CPU searches RAM next, followed by main memory or disk cache.
2. Web Cache (or HTTP Cache)
Web caches store copies of website data to optimize website performance through HTTP headers that hold caching instructions for web requests and responses.
There are two main types of web caching:
Web Client Cache (or Web Browser Cache)
Web browser caching enables websites to load faster by storing frequently accessed content from web servers. The first time a user (client) visits a website, the browser must retrieve all the web assets (e.g., CSS, HTML, JavaScript, and image files) from the origin server.
The web cache then stores copies of this data locally, e.g., on the hard drive or SSD. The next time the user visits the site, the browser will retrieve the web assets from the cache instead of the origin server.
Retrieving these assets locally is much faster than requesting data from the origin server again. The browser only needs to request updated content from the web server, reducing network congestion and improving responsiveness.
Browsers use HTTP cache headers to determine caching algorithms and policies like Time to Live (TTL). TTL is the duration that content can be served through cache before the assets expire and need to be retrieved from the web server again.
Web Server Cache
Web server caching works similarly to web browser caching, except the cache is located server-side rather than client-side. Web servers can use reverse proxy caches like Apache and Nginx to store copies of web assets from the server to facilitate user requests.
Like browser caching, web server caching allows websites to load faster as the user does not need to access data directly from the web server. Web caching also reduces the load on the web server, optimizing performance and user experience.
3. Database Cache
Most databases, such as MongoDB and MySQL use internal caching to eliminate repetitive database queries. Databases can cache the most recent search queries to enable faster retrieval times.
Database caching also alleviates pressure from the database itself as it does not need to process the same queries consecutively.
4. Content Delivery Network (CDN) Cache
A content delivery network (CDN) is a network of geographically distributed proxy servers that help improve website load times by storing content closer to users' IP locations.
When a user requests content from a web server, the proxy will respond to the user on the web server's behalf if a cached copy is available.
Proxy servers reduce latency and the number of requests the origin server receives by intercepting user requests.
5. Domain Name System (DNS) Cache
When a user types in a domain, such as example.com, the web browser will use the user's operating systems stub resolver to translate the site's domain name into an IP address. If the stub resolver doesn't know the translation, it will relay the request for DNS data to more complicated recursive resolvers, which are often operated by Internet service providers (ISPs), governments, and organizations such as Google, OpenDNS, and Quad9.
Once the recursive resolver has the user's request, it sends its own DNS requests to multiple authoritative name servers until it can find a definitive answer.
Domain name servers maintain a directory of domain names and translate them to IP addresses, just as a regular phone book translates names to phone numbers.
The stub resolver and recursive resolvers will cache the domain name to IP address translation to improve performance. Next time a user visits a website, it doesn't need to query the nameservers for a certain amount of time, known as the time to live (TTL). When the TTL expires, the process is repeated.
Cache Benefits
Improved Speeds
Caching allows faster load times as copies of frequently accessed data are stored locally and readily available for the user. For example, web browser caches store web assets locally, which is faster than making repeated web server requests.
Reduced Network Congestion
Caching reduces network congestion on devices like web servers and databases as users can access local copies of requested data. By reducing the number of database/server requests, caching reduces latency and improves system performance.
Offline Access
Caches can allow users to access web pages and other online resources like mobile apps without an Internet connection by storing frequently accessed data locally.
Cache Drawbacks
Performance Issues
Sometimes temporary files do not save/open correctly from a cache. Corrupted cache files often lead to performance issues, causing applications or web pages to crash or incorrectly displaying elements.
Delayed Updates
Although caches should only offer temporary storage of reusable data, sometimes they hold onto older versions of data that have since been updated, e.g., on a web page. In this case, the user would need to clear their browser's cache to allow the newer version to display.
Cybersecurity Risks
As each cache stores copies of recently used data, they increase users' attack surfaces, exposing them to greater cybersecurity risk.
For example, web browser caches store cookies, transaction history, personally identifiable information (PII), and other sensitive data like usernames and passwords.This information can easily be exploited through data breaches as even HTTPS caches do not store encrypted versions of files locally. Using public WiFi networks without clearing browser cache increases this vulnerability as they often run with poor network security.
Domain Name System (DNS) caches are also vulnerable to cyber attacks like DNS cache poisoning (or DNS spoofing). DNS cache poisoning occurs when false DNS information is introduced into a DNS resolver's cache. This information causes DNS queries to return an incorrect response, which commonly redirects users from a legitimate website to a malicious website designed to steal sensitive information or install malware.
Why You Should Clear Your Cache
While caching benefits users through improved load times and user experience, drawbacks like cybersecurity risks provide sufficient reason to clear cache when necessary to improve data security.
How to Clear Cache
Below are instructions on how to clear web cache and app cache from popular browsers and devices.
Google Chrome
Google Chrome has a 'clear browsing data' option that allows users to clear cached data.
Click here to learn how to clear Google Chrome browsing data on a computer.
Click here to learn how to clear Google Chrome browsing data on iPhone and iPad.
Click here to learn how to clear Google Chrome browsing data on Android.
Mozilla Firefox
Firefox allows users to clear cache from the Preferences menu.
Click here to learn how to clear cache on Firefox.
Apple Safari
Safari allows users to clear cache from the Settings menu.
Click here to learn how to clear cache on Safari.
Apple iOS
Apple iOS allows users to clear third-party app cache from the Settings menu.
Click here to learn how to clear app cache on Apple iOS.
Android
Android allows users to clear third-party app cache from the Settings menu.