Jamie's Space

JP

Getting the Most Out of Heroku's Freebies

I used to use Heroku a few years back whenever I needed a little bit of Ruby on Rails hosting, but then I started needing background workers (which you can’t get for free) and larger databases so I shifted to self-hosting. I already had a Rimuhosting VPS, so it didn’t make sense to spend money when I didn’t need to.

I’ve gotten sick of having to write cron jobs and organise monitoring and all that sort of busy work for what are pretty basic sites, so I decided to have another shot at using Heroku.

Turns out my current side project (a place my family and I can record and share our weight loss goals) fits within the free tier for everything except for the email notification jobs. Drat. Luckily I can minimise the cost of this by using a gem called HireFire. I will only need maybe a few minutes of worker time per month, and this gem will make sure that workers are spun up and down as needed so I don’t waste any money I don’t need to.

On top of that, there are a number of free Heroku Addons that can keep everything speedy on a low use site. The Memcache addon has a 5MB free option that you can hook into Rack::Cache and Rails for faster caching. If you are on a small site you will still get a bit of extra speed out of that.

This next trick is a bit cheeky, but it kinda fits with my monitoring needs any way. The New Relic addon offers uptime monitoring (as well as short term site performance stats), which it gives you by pinging your site on a scheduled basis. This has the side effect of keeping your app running - Heroku will shut down a free site that hasn’t been hit in a while. Still not sure if I’m ok with this or not…

There is one more thing you might overlook - Gzip compression of static assets. Heroku’s Cedar stack serves requests directly (see the HTTP Routing documentation)to the application stack, so you don’t get HTTP caching or gzip compression for free. Your app needs to handle these itself, but this is relatively easy. You need two pieces of Rack middleware: Rack::Cache and Rack::Deflater.

For Rack::Cache you will need to add the Memcache addon to your app in Heroku, install the dalli gem and setup the Rack::Cache middleware to use it by adding this to your config/environment/production.rb file:

config.middleware.use Rack::Cache, :metastore => "memcached://#{ENV['MEMCACHE_SERVERS']}", :entitystore => "memcached://#{ENV['MEMCACHE_SERVERS']}" While you are there, set the cache store to use Dalli as well with config.cache_store = :dalli_store

Using Rack::Deflater is easy as pie. In config.ru add the following right above your run MyApp::Application line: use Rack::Deflater In theory you shouldn’t need to do this. Rails 3.1 added Rack::Deflater by default, but the Cedar stack on Heroku doesn’t seem to do it. An unfortunate side effect of adding Deflater this way is it will gzip everything, including images. I’m still thinking of a way to get around this, probably with my own middleware wrapper around Deflater.

That’s all I’ve done so far and things are pretty snappy. If you have any other tips for making the most out of Heroku, let me know in the comments.