HTTP Caching

Overview

In the HTTP standard, like elsewhere, caching refers to the process by which responses are saved and returned on subsequent requests. There are two main types of caches: private caches and shared caches.

A fresh HTTP response usually indicates that the response is valid and can be reused. A stale HTTP response usually indicates the cached response has already expired. Validation refers to the act of checking whether or not a stale HTTP response can still be used. That is, it asks the server if the same response can be marked fresh again.

Private Caches

A private cache is a cache tied to a specific client (e.g. a browser client). A stored response is not shared with other clients and may therefore store personalized responses. A private directive passed to the Cache-Control header is used for this purpose.

Shared Caches

A shared cache is located between the client and server. It is used to store responses shared among users.

Proxy

A proxy cache is a cache that sits between a client and a server, potentially returning cached responses and otherwise forwarding requests to the server.

Managed

A managed cache is a shared cache explicitly deployed to offload the origin server and deliver content efficiently. Examples include reverse proxies, CDNs, etc.

Headers

Cache-Control

The Cache-Control header holds directives in both requests and responses that control caching in browsers and shared caches. Possible directives include (but are not limited to):

Multiple directives can be specified with a comma (,).

Note that even without a Cache-Control header set in the response, a browser may still cache results when certain conditions are met. This is called heuristic caching. To avoid this, a Cache-Control header should generally always be set.

Expires

The Expires header is used to specify the lifetime of a cached response using an explicit time rather than an elapsed amount of time.

Usually the max-age=<N> directive is preferred.

Vary

By default, cached responses are generally distinguished by their URLs. Oftentimes though, responses vary based on values of HTTP headers. The Vary header can be used to specify header values that should be used to distinguish cached responses.

If-Modified-Since

The If-Modified-Since header can be sent in a request to a backend to determine if a cached response has changed since the specified timestamp.

If the response has not been modified, the server can respond with a 304 Not Modified indicating the stale response can be marked fresh again.

If-None-Match

The value of the ETag response header is an arbitrary value generated by the server. If the response is stale, the client can send the same value to the server with the If-None-Match header.

If the response has not been modified, the server can respond with a 304 Not Modified indicating the stale response can be marked fresh again.

Powered by Forestry.md