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.

Fastly varnish results

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.

Varnish http cache miss

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

Varnish http cache miss

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)
    request.session_options[:skip] = true  # removes session data
    response.headers['Cache-Control'] = 'public, no-cache'
    response.headers['Surrogate-Control'] = "max-age=#{max_age}"
  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).

Load Testing

Blitz.io offers a load testing service. We'll run some load on our app and see how its performing. We hit 250 req/sec with 7ms average response time!

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.