Avoid Initial Slow Loading Times of Rails apps under Apache & Passenger

Published on February 19, 2011 by , in Rails

If you have enough RAM on your server, then you will probably want to avoid the lengthy booting times you experience after an application idles.

There are 2 solutions I could find used to solve this problem:

1. Use a cron job to access the application every couple of minutes to keep it in memory.

One of the ‘raw’ solutions used are setting up a cron job to automatically access the application every few minutes and therefore keep it in memory.

crontab -e

And use nano (or your favorite editor) to add this at the end of the cron jobs:

*/5 * * * * wget http://www.example.com > /dev/null

This issues a wget request every 5 minutes and trashes the response.

2. Setup passenger not to automatically kill the rails processes.

But, on the other hand, if you have access to the server’s configuration, a better approach is to modify a few of Passenger’s constants:

PassengerMaxPoolSize 30 (use 15 if you have a machine with 1GB, 30 if you have 2GB of RAM, etc.). This will enable more processes to be spawned if necessary.

PassengerPoolIdleTime 0 (using 0, application instances will not be shutdown unless it’s really necessary – when the available resources on the server are low)

On an Ubuntu machine, you would have to edit the /etc/apache2/httpd.conf file by adding these two lines:

PassengerMaxPoolSize 30
PassengerPoolIdleTime 0

Restart apache

sudo /etc/init.d/apache2 restart

And you should not have that slow boot problem anymore.

*3. Use nginx + thin