Using Multiple Cloudfront Domains with Paperclip

In order to speed up asset loading using a CDN is generally regarded as a good idea. It is also recommended to split up requests among separate hostnames to allow the browser to parallelize loading.

Enabling this in Rails with Paperclip is pretty easy, though the documentation isn’t extremely rich.

You’ll want to set the s3_host_alias option to a proc which determines the correct domain alias based on the id of the object the attachment is for.

  has_attached_file :image, S3_OPTS.merge(
    :s3_host_alias => Proc.new {|attachment| "images#{attachment.instance.id % 4}.pixieengine.com" }, 
    :styles => {
      ...
    }
  )

This sends requests to the following hostnames:

images0.pixieengine.com
images1.pixieengine.com
images2.pixieengine.com
images3.pixieengine.com

The best part is that the same image will always have the same hostname. I’ve seen some people suggest randomly choosing a domain, but that reduces caching potential as the same item could be requested from multiple different domains over time.

3 thoughts on “Using Multiple Cloudfront Domains with Paperclip

  1. I found the given technique interfered w/ marshaling for Rails caching, causing an exception ‘no _dump_data is defined for class Proc’ deep in the cache serialization code.

    Instead, I patched the s3_host_alias method, which worked great:

    #
    # Override Paperclip to use multiple CNAMES to allow browsers to parallelize image asset loading
    #
    module Paperclip
    module Storage
    module S3
    # Override
    def s3_host_alias
    host_alias = @options[:s3_host_alias]
    host_alias.sub(‘%d’, (self.instance.id % 4).to_s)
    end
    end
    end
    end

  2. Only works if self.instance.id is an Integer. If you’re using mongoid I would do:

    .sub(‘%d’, rand(0..3).to_s)

  3. You’d want to be sure to map the same instance to the same number for better caching performance. If the last character in your ids is in the range 0-f you could parse it as a hex integer and % 4, that way the same instances will have the same caching domain.

Leave a Reply

Your email address will not be published. Required fields are marked *