Scale Rails with Varnish HTTP caching layer

Harlow
Harlow
Nov 16, 2013 · 3 min read

An HTTP caching layer can be used to dramatically speed up requests to our web applications. We’ll walk through setting up a Ruby on Rails application to return cacheable responses.

Varnish the Application Accelerator

We’ll use Varnish Cache as our HTTP Caching Layer. Instead of hosting our own Varnish instance, we’ll use a hosted solution provided by Fastly.

When a cache miss occurs, Fastly will fetch the content from our Rails application and cache it locally before returning the response to the user.

Cache miss, hits origin server for page content.

When a cache hit occurs, the response is returned directly to the user (the Rails application never sees the request).

Cache hit, serves page content from cache.

Cache-Control headers for Static Content

The Asset Pipeline allows us to combine and minimize our JavaScript and CSS files. When using the digests for unique asset URLs we can safely cache the files for long periods of time.

We’ll set up our production environment to return a max-age of one year on our static assets.

# config/environments/production.rb 
config.static_cache_control = "public, max-age=#{1.year.to_i}"

Cache-Control headers for Dynamic Content

The default behavior of a Rails app is to set Cache-Control to private and return a session cookie.

HTTP/1.1 200 OK Cache-Control: max-age=0, private, must-revalidate Set-Cookie: _hward_session=[REMOVED_DIGEST]; path=/; HttpOnly

We can manually remove the cookie from response header, and override the default Cache-Control header. Setting the Cache-Control to no-cache will tell the browser not to cache the response locally, but will indicate to Fastly its OK to cache the page on their servers.

# app/controllers/application_controller.rb class ApplicationController < ActionController::Base 
# ... private
def set_cache_control_headers(max_age = 1.year.to_s)
# removes session data
request.session_options[:skip] = true
response.headers['Surrogate-Control'] = "max-age=#{max_age}"
response.headers['Cache-Control'] = 'public, no-cache'
end
end

Next we’ll call the set_cache_control_headers from a before_filter on publicly accessible pages.

# app/controllers/posts_controller.rb 
class PostsController < ApplicationController
before_filter :set_cache_control_headers, only: [:index, :show] end

The resulting headers notify Varnish that the page is public and should be cached.

HTTP/1.1 200 OK Cache-Control: 'public, no-cache' Surrogate-Control: 'max-age=31557600'

Setting the Surrogate-Control header will indicate to Fastly that the page should be cached on Fastly servers (but not on the requesting client).

Purging Cache

Now that we have our content cached we need to create a mechanism for purging the cache when a page changes. See Varnish Cache Invalidation with Fastly Surrogate Keys for more details.

Harlow

Written by

Harlow

Engineering at @Clearbit. I like fast APIs, streaming event data, and of course my lovely @tayloresque. Read more at www.hward.com.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade