In this tutorial, we’ll learn about web caching, its advantages, different types of web caching, and how to control web caches using HTTP headers.
2. What Is a Web Cache?
A web cache is located between the origin servers and the clients of a website and stores copies of the server responses that travel through the network. The saved responses are called representations and represent a version of a website at a certain time. After that, if another request with the same URL passes by, it sends out the stored representation without involving the origin server:
Therefore, there are two possible outcomes:
- A cache hit is when the web cache satisfies the request sent by the user without involving the origin server.
- A cache miss is when the web cache can’t satisfy the request sent by the user.
3. Advantages of Web Caching
Web caching has a lot of advantages:
3.1. Websites Load Faster
The content is served from the cache, which is typically closer to the end users, instead of the origin servers. As a result, it takes less time for it to reach the clients, and the website loads faster.
3.2. Hosting Costs Are Reduced
Web hosting providers charge for the amount of data that is transferred to and from the origin server. But with a web cache, since the content is stored and reused, less data is transferred to and from the origin servers. Therefore, the hosting costs are reduced.
3.3. Reduces the Load on the Server
Since many requests are satisfied by the web cache, they won’t reach the origin server. As a result, the load on the origin server is reduced.
3.4. Website Content Is More Available
If the origin server goes down, the content is still available on the web cache, and the end users can access it. Therefore, the website content is more available.
4. Types of Web Caches
There are three main types of web caches:
4.1. Browser Caches
This type of web cache is stored on the client’s browser. And they are a type of private cache since they belong to a single user. The users also have the option to change their browser settings and disable this type of web cache:
4.2. Proxy Caches
Proxy caches are a type of shared cache that serve a large number of users and are typically set up by big companies and ISPs to store and serve web content. In addition, since they’re servers, they are more configurable than browser caches:
However, since they are not part of the origin server or the client, the end users themselves have to configure their connection to use these proxies. Another method is using interception proxies, which receive user requests by the network itself without the need for user configuration.
4.3. Gateway Caches
This type of web cache sits between the origin server and the clients, and the webmasters themselves configure them to receive user requests in place of the origin server.
For example, Content delivery networks (CDNs) are a group of servers that are geographically distributed between the end users and the origin server. They use gateway caches to provide faster content delivery:
5. How to Control Web Caches
We can use HTTP headers, more specifically HTTP response headers, to control the caching process.
5.1. Expires Header
The expires header tells the web cache for how long the response is fresh. After that, the response (or representation) will be considered stale:
Even though the expires header is easy to work with, it has some limitations. The cache-control header is better for controlling the web cache and is more flexible.
5.2. Cache-Control Header
The cache-control header is another HTTP response header that doesn’t have the limitations of expires and can also accept multiple values:
In this article, we learned about web caching and its advantages. We also learned that even though CDNs use caching, not every caching is a CDN, and there are other types of web caching as well.