Fix slow first request with Passenger / mod_rails?

The main page of this site is now a Rails app, and I plan to use Rails more extensively as time goes on. I run Passenger with Apache, which is a pretty nice solution. (I’m thinking of dropping in REE in lieu of standard Ruby, too?)

But it had one crippling bug: if you were the first to hit the page in a while, it was unbearably slow. It turns out that my guess about what was happening was correct: after a period of inactivity, your app will spin down to free up memory for other threads. The benefit is that it’s proper and efficient. The downside is that if someone hits the application (here, “the application” is the main page) for the first time in a while, Passenger has to load the application into memory again. And as someone who works with Rails on a daily basis, I have to say that it’s excruciatingly slow to start. 10-30 seconds is the norm for me.

The fix is apparently pretty easy: set PassengerPoolIdleTime much higher to leave idle threads running longer, or set it to 0 to leave them running forever “unless it’s really necessary” to make room for something else. The documentation also notes, “Setting the value to 0 is recommended if you’re on a non-shared host that’s only running a few applications, each which must be available at all times.”

You’ll probably also want to set RailsFrameworkSpawnerIdleTime and RailsAppSpawnerIdleTime to 0 if you go that route.

Drop the settings right into httpd.conf, restart Apache, and viola.

4 thoughts on “Fix slow first request with Passenger / mod_rails?

  1. Thanks, Worked for me …

    I have hosted three rails application. Problem is whenever I update code on one application and reload apache all application reloaded and slowdown for some time. I also tried restart.txt file on /tmp directory which also not helped me..

    Can You please tell me how to reload only one rails application.

  2. @Avin – If you reload Apache, it will always cause all the Rails apps to restart again. If you’re just restarting one Passenger app, the tmp/restart.txt file is how it’s supposed to be done.

    It wasn’t clear from your comment — are you touching /tmp/restart.txt, i.e. at the root of the server? It’s actually within the directory Apache is serving out of, e.g. something like /var/www/html/your_rails_app/tmp/restart.txt that you want to ‘touch’, which will cause Passenger to reload your app on the next page load.

    Section 3.3 of the Passenger users guide talks about this if it’s helpful. It’s probably possible to figure out on the command line which threads are serving a particular app and try to send them a signal with kill, but that’s not something I have any experience with, nor something the Passenger folks seem to talk about. (Section 7.4 is tangentially related.)

    Hope this helps?

Leave a Reply

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