Don't Waste 1 Hour on eTags

eTags are awesome! They speed up the browsing experience considerably for apps where users tend to revisit the same pages many times during a browsing session. And rails 4 has them by default.

When I ran ZipZoomAuto, we implemented eTags for our vehicle pricing mobile app site. That plus a couple of other hacks made our app the fastest one in the market and our customers loved us for it.

What are eTags?

eTags save you bandwidth and server processing time. Here's how it works:

  1. User goes to the /articles page on their browser.
  2. Browser sends request to server.
  3. Server returns /articles page BUT before doing so, it creates an MD5 hash on the returned payload and puts it aside.
  4. Browser receives /articles and renders it.

No magic here. The only unusual thing is the server & browser running the MD5.

30 minutes later the user goes back to the /articles page and this time the browsers send an MD5 of that page along with the request. The server receives the request and sees the MD5 as well. Before rendering the page (server processing time savings) it checks to see if the MD5 the browser sent matches the MD5 it put aside in step #3 above.

It is! It matches! So the server says: stop! I ain't doing no mo' work ya'll (she's from Texas!) and instead of sending the rendered page back to the browser it sends back a message (bandwidth savings), a 304 Not Modified status code, telling the browser to pull the page up from its cache.

The browser still has to render the page, but we've saved tons of time by minimizing server side work and possible latency.

What About The One Hour Thing in the Title?

When I discovered that Rails 4 has eTags by default I wanted to see them in action, so I wrote a quick and dirty rails app and loaded up a page while monitoring the HTTP headers in Chrome Dev tools.

AND FOR THE LIFE OF ME eTAGS WOULD JUST NOT WORK. The server kept sending back the whole page with status 200. No 304 love.

No Love

After checking and re-checking my code for almost an hour, changing my config options, clearing my cache I finally found out that Disable cache is checked. This was preventing the if-None-Match value from being sent to the browser which was effectively disabling eTags.

Uncheck, send request twice and voila! 304 love!

love

Author image
About Nash Kabbara
Houston, TX
I work at Manna Systems where we solve business problems with software. Business, software, and stepping outside my comfort zone make about 80% of my life. The other 20% is highly confidential!