user200783
user200783

Reputation: 14348

How is Cache-Control: max-age affected by intermediate caching?

I am currently serving an HTML page to users with a Cache-Control: max-age=600 header. I believe this allows the users' browsers to cache the page for a maximum of 10 minutes.

I am considering adding a CDN in front of my server which, due to the above header, should also cache the page for a maximum of 10 minutes.

If a user obtains the page when it has been in the CDN cache for 9 minutes, say, will it still be cached in the browser for 10 minutes, giving a total cached time of 19 minutes? Or will it only be cached in the browser for 1 minute?

Upvotes: 2

Views: 2114

Answers (1)

Vasiliy Faronov
Vasiliy Faronov

Reputation: 12310

There are two basic approaches here.

The CDN may decide that it is an HTTP cache, as defined by RFC 7234. In that case, it must add an Age header to every response that it serves from cache (without contacting your server). It must also keep the Date header sent by your server. This enables a downstream cache (such as the browser’s cache) to determine for how much more time it can cache the response.

For example, if your server sends:

HTTP/1.1 200 OK
Server: nginx/1.9.12
Date: Sun, 15 May 2016 16:51:31 GMT
Cache-Control: max-age=600

then the cache might send:

HTTP/1.1 200 OK
Server: nginx/1.9.12
Date: Sun, 15 May 2016 16:51:31 GMT
Cache-Control: max-age=600
Age: 541
Via: 1.1 mycdn

This approach is used, for example, by Amazon CloudFront and by Varnish (and thus by Fastly). Sometimes, caches get it wrong: until a couple years ago, Varnish would erroneously reset the Date header on cached responses. In my experiments, though, Firefox and Chrome do not seem to be confused by this (because Age is still there).

Alternatively, the CDN may decide that it’s an HTTP origin server, and cooperate with your server by its own rules. In that case, it must not add an Age header, and it must reset the Date header—but then it can also do whatever it wants to the other headers, including Cache-Control. For example, it can decrement the max-age by the number of seconds that the object spent in its cache:

HTTP/1.1 200 OK
Server: MyCDN/1.2.3
Date: Sun, 15 May 2016 17:00:32 GMT
Cache-Control: max-age=59

But it may not even do this, as generally caching is most useful for static content, which is usually versioned and cached “forever.” Thus, for example, whenever you request https://code.jquery.com/jquery-2.2.3.js (served by MaxCDN), it comes with the same max-age=315360000 and an Expires far in the future.

So you will probably need to check how your particular CDN handles this.

Upvotes: 2

Related Questions