Faster Rails Deployment on Heroku

Heroku can be painfully slow when compiling assets, even if you do all your compiling locally. Luckily, with a couple small tweaks you can cut this time down significantly (given your deploys are indeed slowed down by assets and not something else). Since heroku treats each deployment as an isolated environment – which is actually pretty awesome, especially with heroku fork – Sprockets and other gems can’t really leverage those previous asset compilations. The solution is creating a shared cache between your deploys, assuming you don’t mind your assets being shared.. this solution is actually a bit counter-intuitive to heroku forks and some other features in some ways.

First you need to enable a Memcache Add-on for your application in the Heroku dashboard (or via command line). Like many heroku add-ons you can opt for a free plan with Iron Cache or MemCachier for simple and small applications. Once you’ve enabled your cache service you just need to enable environment variable for your app during pre-compile. It’s a labs feature for now, but I suspect it’ll stay around:

heroku labs:enable user-env-compile

If you’re going to use memcachier, you simply need to add the dalli and memcachier gems to your Gemfile and then configure Sprockets inside of your Rails app:

# config/environments/production.rb config.assets.cache_store = :dalli_store

And with that, you should see the benefits kick in right away! Post your results or other tricks in the comments!

Leave a comment

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