Rails’ page caching vs. HTTP reverse proxy caches

apachehttppage-cachingreverse-proxyruby-on-rails

I've been catching up with the Scaling Rails screencasts. In episode 11 which covers advanced HTTP caching (using reverse proxy caches such as Varnish and Squid etc.), they recommend only considering using a reverse proxy cache once you've already exhausted the possibilities of page, action and fragment caching within your Rails application (as well as memcached etc. but that's not relevant to this question).

What I can't quite understand is how using an HTTP reverse proxy cache can provide a performance boost for an application that already uses page caching. To simplify matters, let's assume that I'm talking about a single host here.

This is my understanding of how both techniques work (maybe I'm wrong):

  • With page caching the Rails process is hit initially and then generates a static HTML file that is served directly by the Web server for subsequent requests, for as long as the cache for that request is valid. If the cache has expired then Rails is hit again and the static file is regenerated with the updated content ready for the next request

  • With an HTTP reverse proxy cache the Rails process is hit when the proxy needs to determine whether the content is stale or not. This is done using various HTTP headers such as ETag, Last-Modified etc. If the content is fresh then Rails responds to the proxy with an HTTP 304 Not Modified and the proxy serves its cached content to the browser, or even better, responds with its own HTTP 304. If the content is stale then Rails serves the updated content to the proxy which caches it and then serves it to the browser

If my understanding is correct, then doesn't page caching result in less hits to the Rails process? There isn't all that back and forth to determine if the content is stale, meaning better performance than reverse proxy caching. Why might you use both techniques in conjunction?

Best Answer

You are right.

The only reason to consider it is if your apache sets expires headers. In this configuration, the proxy can take some of the load off apache.

Having said this, apache static vs proxy cache is pretty much an irrelevancy in the rails world. They are both astronomically fast.

The benefits you would get would be for your none page cacheable stuff.

I prefer using proxy caching over page caching (ala heroku), but thats just me, and a digression.

Related Topic