What Is HTTP Caching?
Data exchange over the internet is growing every day and has by now reached a considerable scale. Vast amounts of information are transferred between server and client systems, evaluated, and displayed. A large part of it, for a static web page, for example, often would not need to be transferred more than once. Any information that will not change in the foreseeable future, or that does not need to be updated to the second, can in principle be cached in different ways. This can happen on the client system in the browser or on a proxy server along the information chain.
If an image file is transferred to the recipient together with a news article, for example, it could be kept available for later display in the browser. Displaying it repeatedly on the web page would then be considerably faster, because the browser has the “unchanged” file at hand immediately and does not have to request it again from the server over the internet. This method has been common for quite some time and is defined in the HTTP standard as HTTP caching.
Caching Data in Practice
The caching of data and information follows a fixed scheme. The data flow begins when a web page is opened in the web browser. In response to the request, the browser receives one or more responses from the server with the corresponding parameters. When an image file such as the website logo is transferred, the header looks like the following example.
HTTP / 2.0 OK
accept-ranges: bytes
cache-control: max-age=31536000
content-length: 4255
content-type: image/jpeg
date: Thu, 13 Dec 2018 11:53:44 GMT
etag: "109f-57b4155594e55"
last-modified: Thu, 22 Nov 2018 14:03:38 GMT
server: Apache
X-Firefox-Spdy: h2From this set of parameters, the browser first recognizes that this is an image file (content-type: image/jpeg). In this example, its size is 4,255 bytes. The parameter cache-control: max-age=31536000 additionally tells the browser that this image file is marked as current for one year (the value is given in seconds). The two parameters date: Thu, 13 Dec 2018 11:53:44 GMT and etag: "109f-57b4155594e55" provide further markers for browser caching. Through date:, the browser knows at any time when this file was first requested. The parameter etag: gives the image file a unique ID.
If this image file is requested again through a page refresh or a few days later, the browser finds it in the cache. Based on the parameters max-age and date:, it can decide whether the image file is still valid and then display it immediately from the cache. A new transfer from the server is no longer needed. If the image file is already outdated, the browser has to request it again, but it can identify the image file quickly and uniquely through the ID from the parameter etag:.
Control Options for HTTP Caching
So that a client (usually the customer’s browser) can carry out and control caching properly, cache directives are added to every request and response. To give all parties the opportunity to process them correctly, all directives must first be passed on as a matter of principle. HTTP caching describes corresponding protocols for requests and responses. The following excerpt briefly describes the directives they have in common.
The cache request directives include the following.
- no-cache states that the returned response cannot be used for a subsequent request as it is. The client must also ask the server whether the response has changed. If an ETag is present, however, an additional download is avoided.
- no-store generally forbids caches and browsers from storing data. As a result, every request generates a new response and a download.
- max-age describes the maximum validity period (in seconds) for the requested resource. If this time span has been exceeded and no other value is defined (for example max-stale), the resource is requested again.
The cache response directives use the request directives mentioned above and, in addition, the following extended directives.
- public also allows information marked with HTTP authentication to be stored in the cache, which would normally not be possible. The directive is rarely used, however, because max-age, for example, already allows storage explicitly.
- private means that the response may be stored only in the cache of the designated client and not on any proxy or resource servers in between.
- must-revalidate and proxy-revalidate denote a mandatory check and the reload that goes with it. must-revalidate forces a check of the validity of the resource, with a possible reload. proxy-revalidate refers only to intermediate caches and not to the browser cache on the client.
There are further control options for HTTP caching that cover special cases and specific requirements. The Mozilla Developer Network offers a comprehensive overview and source code examples at (1).
Conclusion
As is so often the case, HTTP caching has no single optimal solution. The first step is to control the flow deliberately and to include security aspects. In a flow diagram, the developer should define in advance which resources can sensibly be cached and set the parameters accordingly. It is also important to assign resources an ID through the ETag parameter. This supports reliable identification and fast retrieval when a resource is reloaded.
If resources are used several times under different URLs, you should aim for consistency and for central management of the resources (see CDNs and CDN servers). To avoid larger download volumes, checking whether resources can be stored on a proxy can produce good results. Different groups of users can then be served quickly with unchanged resources at the proxy level. No less important is assigning a sensible cache lifetime to resources consistently. Browsers on the client side can then determine for themselves which resources need to be reloaded and how often.
(1) https://developer.mozilla.org/docs/Web/HTTP/Headers/Cache-Control