What is the point of Varnish and Rack-Cache for a Rails app?
Asked Answered
N

1

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?

Nicki answered 17/10, 2012 at 4:26 Comment(0)
O
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.

Operetta answered 17/10, 2012 at 5:1 Comment(3)
What you're describing sounds like server-side caching. Doesn't Rails itself have built in support for server-side caching?Nicki
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.Operetta
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.Operetta

© 2022 - 2024 — McMap. All rights reserved.