Current time: 12-11-2017, 03:28 PM Hello There, Guest! (LoginRegister)

Post Reply 
 
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Etag/Last Modified - the numpty question I've always wanted to know.
11-01-2010, 06:09 AM
Post: #1
Etag/Last Modified - the numpty question I've always wanted to know.
So, using Google-speak, cache-control: max-age/s-maxage and expires headers are "strong caching headers" while etags and last-modified headers are "weak".

Google recommend only one of each kind - which make perfect sense, but they also recommend one weak and one strong.

2 question chaps - and I've only just plucked up the courage on this one as I'm sure I'm asking a bonehead question:

1. Which strong and which weak one do we choose? (Google recommend expires over cache-control: max-age, but they are vague on the reasons.)

2. Why do we need a weak cache header at all? The behaviour I think I get (but I'd love to know others' observations) is endless conditional GETs each of which gets a 304; where what I think I'd like is just a single new unconditional GET and then no traffic for as long as the strong header mandates.

Be gentle chaps Smile

N
Find all posts by this user
Quote this message in a reply
11-02-2010, 01:38 AM
Post: #2
RE: Etag/Last Modified - the numpty question I've always wanted to know.
I do not know why but my firefox ignores the expires-header. When i visit my website, i always find some 304 in my logfile. Possibly the reason you should use a strong and a weak is, that there is no rule a browser must use the local cache as the header says it.

So the 2nd rule is just a fall back to get the speed up and the transferred data low.

test and execute PHP functions online free and easy => http://www.functions-online.com/home.html
Visit this user's website Find all posts by this user
Quote this message in a reply
11-02-2010, 01:43 AM
Post: #3
RE: Etag/Last Modified - the numpty question I've always wanted to know.
Do you have a reference for the Google recommendation? All I could find was the Page Speed optimization docs: http://code.google.com/speed/page-speed/...serCaching and they don't seem to say anything specific about having both strong and weak headers.
Visit this user's website Find all posts by this user
Quote this message in a reply
11-02-2010, 02:13 AM
Post: #4
RE: Etag/Last Modified - the numpty question I've always wanted to know.
(11-02-2010 01:43 AM)pmeenan Wrote:  Do you have a reference for the Google recommendation? All I could find was the Page Speed optimization docs: http://code.google.com/speed/page-speed/...serCaching and they don't seem to say anything specific about having both strong and weak headers.

Pat, this sentence is on that page:
"It is important to specify one of Expires or Cache-Control max-age, and one of Last-Modified or ETag, for all cacheable resources."

So yeah, send 1 strong one (Expires or CC) *and* one weak one (Last-Modified/ETag).
Imo, this is a good best practice.

The weak one will only kick in if the Expires has .... expired. The browser will then have a file in cache but is not sure if it may use it. So it sends a conditional request to the server, asking the server "hey dude, may I use this file or not?" and the server will respond with either a 304 "yeah man, go ahead" or a 200 "nope, here is a new one, use that and throw away the old".
Find all posts by this user
Quote this message in a reply
11-02-2010, 07:26 AM
Post: #5
RE: Etag/Last Modified - the numpty question I've always wanted to know.
(11-02-2010 02:13 AM)aaronpeters Wrote:  
(11-02-2010 01:43 AM)pmeenan Wrote:  Do you have a reference for the Google recommendation? All I could find was the Page Speed optimization docs: http://code.google.com/speed/page-speed/...serCaching and they don't seem to say anything specific about having both strong and weak headers.

Pat, this sentence is on that page:
"It is important to specify one of Expires or Cache-Control max-age, and one of Last-Modified or ETag, for all cacheable resources."

So yeah, send 1 strong one (Expires or CC) *and* one weak one (Last-Modified/ETag).
Imo, this is a good best practice.

The weak one will only kick in if the Expires has .... expired. The browser will then have a file in cache but is not sure if it may use it. So it sends a conditional request to the server, asking the server "hey dude, may I use this file or not?" and the server will respond with either a 304 "yeah man, go ahead" or a 200 "nope, here is a new one, use that and throw away the old".

Thanks for going easy chaps. The thing that I think I don't like is that I can't work out whether the 304 "carry on" response itself persists. Is it just a "yeah man, carry on this time", or a "yeah man, carry on and BTW there's no need to bug my ass again for another n seconds"?

I suppose what I should really do is get off my backside and wireshark this stuff across browsers with various combinations of header :-D. (What I see in Firebug really confuses me - as jabubo finds as well, I think. I see conditional Gets *before* CC expiry, but only on every second request. Maybe that's what Google mean with the statments "the browser applies a heuristic to determine whether to fetch the item from cache or not. (The heuristics are different among different browsers.)").

BTW Pat, you recommend against Etags: 'ETag headers should generally not be used unless you have an explicit reason to need them'. Can you elucidate?

Thanks again everyone.

Neil
Find all posts by this user
Quote this message in a reply
11-02-2010, 07:38 AM
Post: #6
RE: Etag/Last Modified - the numpty question I've always wanted to know.
The E-tag recommendation comes from here: http://developer.yahoo.com/blogs/ydn/pos...ormanc_11/

Basically, if you don't need the dynamic capabilities that e-tags give you then you're better off just sticking with a last-modified. It's not a rule that I feel particularly strongly about (along with the cookies check) which is why they aren't in the grades at the top of the page but once you've done everything else they're worth considering.
Visit this user's website Find all posts by this user
Quote this message in a reply
11-02-2010, 08:03 AM
Post: #7
RE: Etag/Last Modified - the numpty question I've always wanted to know.
(11-02-2010 07:38 AM)pmeenan Wrote:  The E-tag recommendation comes from here: http://developer.yahoo.com/blogs/ydn/pos...ormanc_11/

Basically, if you don't need the dynamic capabilities that e-tags give you then you're better off just sticking with a last-modified. It's not a rule that I feel particularly strongly about (along with the cookies check) which is why they aren't in the grades at the top of the page but once you've done everything else they're worth considering.

Thanks for the quick reply, Pat.

"Even if your components have a far future Expires header, a conditional GET request is still made whenever the user hits Reload or Refresh." Hmmm... interesting.

Should mention my other reason for being interested in this. On my site, a *lot* of far future expires resources are quick to produce server side and also are within the standard 2 packet window size - so returning bytes is not going to affect latency much over and above an empty 304.

Now what I *really* want to know, is what having CDN inbetween adds to the mix. Lol.
Find all posts by this user
Quote this message in a reply
01-23-2011, 04:11 AM
Post: #8
RE: Etag/Last Modified - the numpty question I've always wanted to know.
Ok, question to see if I understand correctly: if I only use Expires header, and I do not use a Last Modified header also, in some cases things can go wrong? Eg. some clients cannot react properly? Is there an example use case for that? And does this apply to all different kind of files?
Find all posts by this user
Quote this message in a reply
01-23-2011, 04:30 AM
Post: #9
RE: Etag/Last Modified - the numpty question I've always wanted to know.
It's actually a little more fringe than that. You need to be running a site that has multiple servers serving the same files and those servers would have to be using an Apache config with the default etag behavior (that includes machine-specific information in the hash or at least used to).

In this worst-case scenario, it's possible that when the conditional e-tag check is made, instead of a "not modified", the server will send the full file.

It's such a fringe condition that it's not really worth investing any time into and I'll be removing it from the checks to eliminate confusion (or premature optimization).

Thanks,

-Pat
Visit this user's website Find all posts by this user
Quote this message in a reply
01-23-2011, 04:41 AM
Post: #10
RE: Etag/Last Modified - the numpty question I've always wanted to know.
We have a big site with multiple site servers. We disabled Etag everywhere, exactly because of those problems :-). We now use far future expires headers but I wonder if I should add Last Modified headers.
Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


User(s) browsing this thread: 1 Guest(s)