public
Last active

Fast Rails + Heroku Configuration

  • Download Gist
Gemfile
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
group :production do
gem 'passenger'
 
# Enable gzip compression on heroku, but don't compress images.
gem 'heroku-deflater'
 
# Heroku injects it if it's not in there already
gem 'rails_12factor'
end
 
gem 'memcachier'
gem 'dalli'
# Fast IO for memcache
gem 'kgio'
 
# Serve static assets through Rack + Memcache
# https://devcenter.heroku.com/articles/rack-cache-memcached-rails31
gem 'rack-cache'
Procfile
1
web: bundle exec passenger start -p $PORT --max-pool-size 3
addons.md
Markdown

Free addons!

heroku addons:add memcachier
heroku addons:add mandrill:starter
heroku addons:add newrelic
heroku addons:add pgbackups
heroku addons:add papertrail
production.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
# config/environments/production.rb
 
# ...
 
# Enable Rails's static asset server for Heroku
config.serve_static_assets = true
 
# Set static assets cache header. rack-cache will cache those.
config.static_cache_control = "public, max-age=31536000"
 
config.cache_store = :dalli_store
 
client = Dalli::Client.new(ENV["MEMCACHIER_SERVERS"],
:value_max_bytes => 10485760,
:expires_in => 86400) # 1 day
 
# Configure rack-cache for using memcachier
config.action_dispatch.rack_cache = {
:metastore => client,
:entitystore => client
}
 
# Send emails via sendgrid
 
ActionMailer::Base.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => 'smtp.mandrillapp.com',
:port => '587',
:authentication => :plain,
:user_name => ENV['MANDRILL_USERNAME'],
:password => ENV['MANDRILL_APIKEY'],
:domain => 'heroku.com',
}
 
# ...

Some suggestions:

  • sucker_punch (to avoir using workers for just emails)
  • add a comment on new relic regarding the monitoring feature to keep the app online without using external services

sucker_punch is great but one must be really carefull if you aren't using ruby 2.0.

Can't precompile assets: undefined method `day' for 1:Fixnum
expires_in takes the "default TTL in seconds if you do not pass TTL as a parameter to an individual operation, defaults to 0 or forever" [source: http://www.rubydoc.info/github/mperham/dalli/Dalli/Client]
If it's 1 day you want then value should be 86400

client = Dalli::Client.new(ENV["MEMCACHIER_SERVERS"],
                             :value_max_bytes => 10485760,
                             :expires_in => 86400)

These settings, minus Memcache, are in Suspenders. I prefer fronting my Heroku apps with Fastly to Memcache for the fastest static asset serving that only hits the Heroku dynos once per cache via origin pull.

You can change the Sendgrid by the Mandrill. In the free plan, they have the double of emails.

@mathieugagne Thanks! I just fixed it!

@maurogeorge Done!

@croaky Fastly charges a minimum of $50 a month right?

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.