5 ms·
How To Optimize Your Site With HTTP Caching
- ehc 15y agoArticle from 2007
- oniTony 15y ago2008+ version -- throw your assets into CloudFront (or another CDN of choice), let CDN handle the caching. ;)
- mkchandler 15y agoI know you say that in jest, but I would say that it is still good to know the underlying technology. It is much easier to debug issues in the future once you understand them.
- bitops 15y agoConcur - I think that, once you have a certain amount of experience built up working in frameworks like Rails, you can only go so far before you hit a wall. At that point, it's necessary to start learning the protocols and the lower level stuff to advance your understanding and your craft.
- bittermang 15y agoGood information doesn't necessarily have an expiration date, but I concede to your point that it might not be the most up to date source out there.
- kalid 15y agoYep, I was surprised too to see it on HN :). I think one of the meta-takeaway is that understanding the fundamentals of web caching can help with your general CS knowledge ("There are only two hard problems in Computer Science: cache invalidation and naming things." -- Phil Karlton). Looking at Apache, we see a few strategies: * Include last-modified metadata * Include content metadata (eTag/md5 of content) * Include explicit expiration date * Include a max-age * Include metadata about who can cache (public/private/no-caching, i.e. users can cache but proxies cannot) These approaches could be used when designing data flows with Memcache, Redis, etc.
- SquareWheel 15y agoOne thing I don't understand. If the server has asked the client to cache an image for a year, and the image is indeed updated in that time, is there some way of telling the client to download that image anyway? I'd take it to Google, but I have no idea how I'd ask that in Google query form.
- pork 15y agoIn HTTP, since it's stateless, you don't "tell" the client anything without it first asking. The usual way to bust the cache is to add a junk parameter to the end of a GET request.
- SquareWheel 15y agoI see. I assume you would change the html code to say <img src="image.png?cache=no"> or something like that to force the browser to redownload it? What if the html page itself were cached for a year? Is there an Apache setting that can give a global "no caching" command, or something like that?
- kalid 15y agoYep, exactly -- not only can the images be cached, but the HTML too! The ideal way to do it is have the "loader file" (index.html) only cached with last-modified date, so as soon as it changes the client is aware. The client requests the file each time, and is returned the full file or a simple Not Modified response. Within the file, you have references to permanently-cached, versioned resources (<img src="/images/foo.png?build=123" />). If the cache expiration is far enough away, the browser won't even issue the request to check for a new version. Some browsers don't cache query params so you might use rewriting rules to change foo.png to foo.123.png. This rewriting is done automatically for you with Google Page Speed module for Apache.
- SquareWheel 15y agoIt sounds like a much deeper subject than I first appreciated. I'll definitely read up on this. Using URL rewriting with caching is interesting, I've not seen that before. I work with one lady that complains about a slow loading JQuery slideshow, and smarter caching may very well be the solution (at least, after the first load).
- ceol 15y agoThis is a great article for an introduction to HTTP caching. It's well-written and even covers how to set up caching in Apache.
- kalid 15y agoThanks for the kind words (I'm the author). My main, ever-evolving goal when writing tutorials is to try to write what I'd like to see: 1) Explain the underlying concept 2) Show variations 3) Explain how to do it yourself 4) Show how to verify you did it correctly 5) Meta: be as concise as possible, maximize bang for the buck
- ceol 15y agoI really enjoyed your writing style. It didn't push its (successful!) attempts at humor and used them sparingly enough to be a joy when they were read.
- kalid 15y agoThanks! It's easy to get self-conscious about putting humor into a tech article :)
- raghus 15y agoI've always enjoyed your writings kalid. I esp liked http://betterexplained.com/articles/a-visual-intuitive-guide-to-imaginary-numbers/ http://betterexplained.com/articles/a-visual-intuitive-guide.... Thanks so much for your site!
- kalid 15y agoThanks, I appreciate it!
- fexl 15y agoAnd it links to another great article on gzip compression.
- js2 15y agoBe careful with ETags if you're serving content from a web-farm - http://developer.yahoo.com/performance/rules.html#etags http://developer.yahoo.com/performance/rules.html#etags
- riledhel 15y agoIn the same spirit, there's a lot to gain and very little to lose if you activate ETags and serve content from just one source.
- bitops 15y agoThis is a good writeup, though I'm surprised no-one has mentioned the holy grail of caching with HTTP. That of course is good old RFC 2616: http://www.ietf.org/rfc/rfc2616.txt http://www.ietf.org/rfc/rfc2616.txt There's an entire section in there devoted just to caching in HTTP. Very well worth reading in its entirety.
- d0mine 15y agohttp://tools.ietf.org/html/rfc2616 http://tools.ietf.org/html/rfc2616 adds errata, RFCs that update it and hyperlinks (it is a web after all) Direct link to Caching in HTTP http://tools.ietf.org/html/rfc2616#section-13 http://tools.ietf.org/html/rfc2616#section-13
- js4all 15y agoA pretty basic write up. Static caching is standard these days. The article doesn't help with speeding up today's dynamic sites.