7

I am a bit confused about the purpose of Varnish and Rack-Cache for a Rails app. In config/environments/production.rb caching can be set with something like

config.static_cache_control = "public, max-age=3600"

Given that, what exactly is the purpose of Varnish and Rack-Cache if you can set caching in the Rails app itself?

And what causes the default Rails app to use rack-cache?

user782220
  • 10,677
  • 21
  • 72
  • 135

1 Answers1

7

Static Cache Control affects the http headers for Cache-Control. As in, the server suggests to intermediate caches that the max-age=3600.

Varnish, Rack-Cache, Squid and others actively cache generated content on the server. Database calls are expensive and, even when a request doesn't make a call to the db, the less infrastructure the request has to go through, generally the faster it will be.

Rack::Cache is rack middleware that supports HTTP standards compliant caching. Their FAQ page has some good information about it's pros and cons over other caching solutions. Here's a question comparing rack::cache to varnish on heroku. Rails also has ActiveSupport::Cache which handles fragment and page caching. I'm not sure what the differences are, but both are included in Rails by default. I had said earlier that rack::cache wasn't default, but I was wrong.

Varnish, Squid, and others exist outside the Rails stack in front of the webserver(eg Apache/Nginx/etc) as a separate process. They are highly configurable, application independent, and have some advanced features (such as Squid's ACL's). Varnish and others have the benefit of minimizing the infrastructure a request has to go through to get served. If it's fresh, the request hits Varnish and immediately returns to the client. This probably has the most benefit for high-traffic sites and might be overkill for smaller apps.

Here's an article on heroku detailing the use of rack::cache in Rails3. There are also some good railscasts on doing page/fragment caching in-app and on using memcached as a backend(which is very important). For varnish and others, you can start with this tutorial on varnish's site.

Community
  • 1
  • 1
GorrillaMcD
  • 1,884
  • 1
  • 14
  • 22
  • 1
    What you're describing sounds like server-side caching. Doesn't Rails itself have built in support for server-side caching? – user782220 Oct 17 '12 at 05:10
  • I wrote that answer kind of late at night and didn't make it very clear. I'll update it. Rails3 has caching support from ActiveSupport::Cache, so I'm not sure if rack::cache is redundant with that or not. I'll expand on the use of varnish too, just give me a minute. – GorrillaMcD Oct 17 '12 at 18:23
  • I corrected some misinformation (sorry about that). Rack::cache and ActiveSupport::cache are both part of rails. I'm not sure though what the differences are. – GorrillaMcD Oct 19 '12 at 02:23