Dealing with Heroku startup lag
Update: I think I may have been fairly high on cold medicine when I wrote this. It really isn't a good way to go about handling anything, unless maybe your site is just a playground with no traffic. Let's be honest here, if you have real visitors coming to your site: just pay for more dyno's and don't mess with silly hacks like this. </update>
So, if you haven't tried Heroku yet, you really should. It has got to be the most awesome way to deploy and manage a rails app that I have ever seen. But this article isn't another Heroku review, instead it is my attempt at getting around one minor pitfall with Heroku.
The problem? If you're site doesn't get much traffic, and you are on the free "1 dyno" plan, your dyno's (rails processes) tend to go offline. This means your next visitor sees your page load for a few seconds before they see anything. It's not terrible, but I wouldn't call it ideal, either.
But I think I have found a simple solution to ease the "pain" of this "problem." (Sarcasm intended): Heroku serves static files from varnish... no delay on those while we wait for the rails app to start.
So what's happening? Someone comes along to www . my cool site . com, but there are no active dyno's. As expected, they get sent to index.html, which fires off an AJAX request to the rails app. As soon as it gets a successful result, it redirects the user to the /home path within my rails site. Assuming there are active dyno's, this all happens very quickly. If the dyno's are off, a loading message fades in and a few seconds later they get redirect.
I know it isn't perfect... but it should work until my site gets busier and I can pay for more dyno's.
July 26th, 2010 - 17:11
Some notes that I may add if I rewrite this article.
Obviously, helpers like root_url and root_path are goners. Having the index.html file there clobbers that route from ever getting used.
That said, I do suggest defining a home route. Something like:
match ‘home’ => ‘controller#action’
Then in your application controller you can define the functions root_url and root_path and have them call home_url and home_path, respectively. You should also add a helper_method call in the application controller to the new root_* methods you created.
August 1st, 2011 - 13:54
Hi, I tried that for my blog hosted on heroku, and it doesn’t seem to work… it still takes 8 seconds or so to load and then i can see for an instant the content of the index.html saying “loading…” and in less than a sec it is redirected to my ‘home’ action. What can it be going wrong??
August 4th, 2011 - 15:06
It sounds as if there are multiple requests coming in at once. The process is already running, so this code quickly redirects to the actual content, which should be live, but perhaps that request is then getting queue’d and looks delayed. If you have some traffic, see if adding more dyno’s is helpful in reducing the delay… Otherwise, it might be the actual code taking a long time to process.
April 23rd, 2012 - 10:06
This is more for future people reading the thread.
But this goes through scaling a rails.
Some of the advanced caching videos deal with proxy servers (aka varnish) and cachecontrols.
http://railslab.newrelic.com/scaling-rails