Blog

A single dyno on Heroku can process a lot of hits if you tune it

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

  1. 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.)
  2. Install asset_sync
    1. gem 'asset_sync' in Gemfile
    2. rails g asset_sync:install
  3. 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
  4. Configure your asset host in config/environments/production.rb:
    config.action_controller.asset_host = Proc.new do |source, request|
      request.ssl? ? "https://#{ENV['FOG_DIRECTORY']}.s3.amazonaws.com" : "http://#{ENV['FOG_DIRECTORY']}.s3.amazonaws.com"
    end
         
  5. Run
    rake assets:precompile
    (you'll know it works if you are seeing files uploaded to amazon)
  6. (install heroku addons:releases if you haven't to save your butt if something goes wrong deploying)
  7. git commit this stuff (you can ignore committing public/assets, its not needed on heroku)
  8. git push heroku master
  9. See if you site loads, and check the source to see if you js/css/image files are pointing to heroku
  10. 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.

New site launched: http://searchthescriptures.org

Search the Scriptures

I just launched a new site for Search the Scriptures

Search the Scriptures is a Christian resources site and radio broadcast ministry. They run a Christian radio program on the East Coast.

Cool things about this site

  • I listen to this content and content from sites just like it, so it was fun to build.
  • It uses Amazon S3 and Amazon Cloudfront to create streaming radio broadcasts and sermons at a very low cost to the ministry
  • I really like the design

Nashville Code Retreat

NashDL / Rick Bradley / The Nashville programming community put on a code retreat on Saturday.

It was great.

About 20 programmers on various languages and experience met together to by guided through team programming exercises designed to hone the craft of software development in a relaxed group setting.

Rick lined up a great Ruby developer and TDD expert: (how's that for sweet Google keyword attribution!) Pat Maddox who led the sessions.

I learned a lot about testing, OO software design, and recursion.

Also thanks to the Entrepreneur Center in Nashville for meeting space.

MacBook Air and Cucumber speed

I just switched from a 2008 MacBook to a MacBook Air, and my cucumber test suites run much faster.

An small app's tests run in 1m 28s on the MacBook and 48s on the MacBook Air-- 45% faster.

I attribute most of the gains to a SSD harddrive vs. a normal harddrive. Cucumber tests setup and teardown a lot of records in the database and that happens much faster with an SSD

Removing fluoride from the water supply

More cities have stopped adding fluoride to the water supply. This is a very good thing. Fluoride is an animal poison that reduces IQ and causes cancer. Even the CDC says not to make baby formula with tap water because the fluoride is dangerous for infants.

This video sums up a lot of the evidence

Links

http://www.cdc.gov/fluoridation/safety/infant_formula.htm