These two settings can massively boost the web traffic handled by a single dyno and increase page load time.
Easiest: HTTP caching on heroku
If you don't have very personalized content on the same URL, you can add a single line to your controller and get a big speed boost.
response.headers['Cache-Control'] = 'public, max-age=300'
It looks like this:
class MyController < ApplicationController
def index
response.headers['Cache-Control'] = 'public, max-age=300'
@posts = Post.all
end
end
The 300 in means 300 seconds, and you can change that to whatever you want.
That one line will have heroku serve up a cached version of that url without even talking to Rails. Every 5 minutes (the 300 seconds) it checks back with Rails app and refreshes that page in the cache (when someone goes to that URL)
Docs: http://devcenter.heroku.com/articles/http-caching
Little harder: asset_sync and Rails 3.1
Offload serving up all your site images from Heroku and let it focus on just serving up your pages.
When someone visits you site, heroku gives back a webpage. Simple. But heroku also serves up every css file, js file and image on that page too. So one webpage isn't one request to heroku. Its like 32 (for every css, js, image file).
How many more webpages could heroku serve if it didn't have to also handle the 31 other web requests?
A lot!
So offload those css/js/image files to a CDN (content delivery network). Rails 3.1 and the asset_sync gem make this a lot easier than it used to be.
What you'll need
- Amazon S3 bucket and keys
- Rails 3.1+
- All you css / js / image files in the app/assets directory
- asset_sync gem
How to do it
- Make a bucket on your Amazon S3 for your production site if you haven't already (If you are using something like paperclip for user uploads on heroku, you already have this done.)
- Install asset_sync
- gem 'asset_sync' in Gemfile
- rails g asset_sync:install
- Configure config/initializers/asset_sync.rb
config.fog_provider = 'AWS'
mean you are using amazon s3
config.aws_access_key_id = 'your_amazon_s3_access_key'
config.aws_secret_access_key = 'your_amazon_s3_secret_access_key'
config.fog_directory = 'your_bucket_name_for_this_application'
make sure this bucket exists
- Run
rake assets:precompile
(you'll know it works if you are seeing files uploaded to amazon)
- (install heroku addons:releases if you haven't to save your butt if something goes wrong deploying)
- git commit this stuff (you can ignore committing public/assets, its not needed on heroku)
- git push heroku master
- See if you site loads, and check the source to see if you js/css/image files are pointing to heroku
- Having issues? Check out the asset_sync page at github
An example site
I just added these tunes to a client site: http://searchthescriptures.org/ if you want to see an example of this running.