What's the difference between Cache-Control: max-age=0 and no-cache?

rubyruy

The header Cache-Control: max-age=0 implies that the content is considered stale (and must be re-fetched) immediately, which is in effect the same thing as Cache-Control: no-cache.

Michael Krebs

I had this same question, and found some info in my searches (your question came up as one of the results). Here's what I determined...

There are two sides to the Cache-Control header. One side is where it can be sent by the web server (aka. "origin server"). The other side is where it can be sent by the browser (aka. "user agent").


When sent by the origin server

I believe max-age=0 simply tells caches (and user agents) the response is stale from the get-go and so they SHOULD revalidate the response (eg. with the If-Not-Modified header) before using a cached copy, whereas, no-cache tells them they MUST revalidate before using a cached copy. From 14.9.1 What is Cacheable:

no-cache

...a cache MUST NOT use the response to satisfy a subsequent request without successful revalidation with the origin server. This allows an origin server to prevent caching even by caches that have been configured to return stale responses to client requests.

In other words, caches may sometimes choose to use a stale response (although I believe they have to then add a Warning header), but no-cache says they're not allowed to use a stale response no matter what. Maybe you'd want the SHOULD-revalidate behavior when baseball stats are generated in a page, but you'd want the MUST-revalidate behavior when you've generated the response to an e-commerce purchase.

Although you're correct in your comment when you say no-cache is not supposed to prevent storage, it might actually be another difference when using no-cache. I came across a page, Cache Control Directives Demystified, that says (I can't vouch for its correctness):

In practice, IE and Firefox have started treating the no-cache directive as if it instructs the browser not to even cache the page. We started observing this behavior about a year ago. We suspect that this change was prompted by the widespread (and incorrect) use of this directive to prevent caching.

...

Notice that of late, "cache-control: no-cache" has also started behaving like the "no-store" directive.

As an aside, it appears to me that Cache-Control: max-age=0, must-revalidate should basically mean the same thing as Cache-Control: no-cache. So maybe that's a way to get the MUST-revalidate behavior of no-cache, while avoiding the apparent migration of no-cache to doing the same thing as no-store (ie. no caching whatsoever)?


When sent by the user agent

I believe shahkalpesh's answer applies to the user agent side. You can also look at 13.2.6 Disambiguating Multiple Responses.

If a user agent sends a request with Cache-Control: max-age=0 (aka. "end-to-end revalidation"), then each cache along the way will revalidate its cache entry (eg. with the If-Not-Modified header) all the way to the origin server. If the reply is then 304 (Not Modified), the cached entity can be used.

On the other hand, sending a request with Cache-Control: no-cache (aka. "end-to-end reload") doesn't revalidate and the server MUST NOT use a cached copy when responding.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

What is the difference between max-age=0 and max-age=-1 in Cache Control?

What is difference between max-age and max-stale in cache control mechanism

What's the difference between cache.period and cache.cachecontrol.max-age?

"Cache-Control: max-age=0, no-cache" but browser bypasses server query (and hits cache)?

Symfony 3.4 http cache , always Cache-Control: max-age=0, must-revalidate, private

Express static server cache control with max-age=0,must-revalidate

Access-Control-Max-Age vs Cache-Control

What happens when you use Cache-Control: max-age with ETags?

what does setting Cache-Control: max-age=20 in request header do?

What is the difference between ram cache and cache ram?

S3/CloudFront, serve all files with header "Cache-Control: max-age=365000000, immutable"

What is the difference between buffer and cache?

What is the difference between a cache and a buffer?

PHP session sending header "Cache-Control: no-store, no-cache, must-revalidate". How to change to "Cache-Control: s-maxage=10, public, max-age=10"

Google chrome not respecting max-age in Cache-Control

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

Setting cache-control max-age using Apache not working

No cache: Varnish age 0

What's the difference between lvmcache and dm-cache?

What's the difference between conflict and compulsory cache miss?

What's the difference between `--repository_cache` and `--distdir`

HTTP Cache-Control header max-age vs max-age, must-revalidate

Difference between Pragma and Cache-Control headers?

Are "max-age" and "no-cache" from "Cache-Control" header mutually exclusive?

Does `Age` have to be used when using `Cache-Control: max-age`?

What Is The Difference Between Cache Storage and Cache Object in CacheAPI

What is the difference between buffer and cache memory in Linux?

What is the difference between createOrReplaceTempView(viewName) and cache() on a DataSet

What is difference between cache and static html?