I'm reading this article on Azure CDN. I can either control caching with:
- An ETag
- Last Modified
- Default heuristics
What are the benefits and drawbacks of the first two choices? Does it matter?
Places where one might work vs the other could be:
- Cross domain calls
- Private browsing
- IFrames
- Ajax
... where that data is either visible by those methods, and not the others.
Both are valid to use in whatever scenario.
eTag
is newer so it is considered best practice to use that instead oflast-modified
, however it is up to you.Here is the
MS Docs
page explaining thateTag
takes precedence overlast-modified
in which system interprets it:https://docs.microsoft.com/en-us/azure/cdn/cdn-how-caching-works#cdn-caching
Let me make two real-life assumptions:
ETag
orLast-Modified
for youwith this in mind, the most obvious difference is that for files, it's easy for you to either accidentally or purposely change the modification date, thus changing
Last-Modified
header. (You could even, gasp, return to an earlier date.) In general case, you cannot impactETag
so easily. Good or bad? Depends on your use case. If the code that generatesETag
is decent, I'd say it is better suited to a general use case aka just-works-out-of-the-box-dont-touch-the-knobs. But to know whether it's decent requires testing, especially testing upgrades and downgrades.ETag is good for content that changes more than once per second (example: a heavily used chat), because Last-Modified only supports 1-second granularity.
ETag for compressed response should be re-generated by your server, it cannot be copied from uncompressed one. But it's much less CPU usage than compressing anyway.
With CORS, youu have
Last-Modified
for free, but to haveeTag
your server needs to support an extra headerAccess-Control-Expose-Headers: ETag
(MDN).