First hit on app takes a long time

So, I've deployed my first rails app, however I'm the only one hitting it for now.

I've noticed that the first time I hit the app after some long period, it takes a really long time to respond (2-3 seconds). After that, it's very fast.

At first, I thought it might be my VPS "waking up" from some sleep state, but Rails does log the long load time (log below).

My app just uses the database for login/user auth. Otherwise, it serves up pages on the filesystem in textile and renders them as HTML. There is no explicit caching done by me, and the filesystem/ textile/rendering is all hand-rolled (so there's no caching done implicitly).

What are some things I should look for to understand why this happens?

log of two sequential requests for the same page:

Processing WikiPagesController#show (for 98.218.223.189 at 2009-08-12 20:16:41) [GET]   Parameters: {"id"=>"MainPage"} Rendering template within layouts/application Rendering wiki_pages/show   User Columns (1.3ms) SHOW FIELDS FROM `users`   User Load (0.4ms) SELECT * FROM `users` WHERE (`users`.`id` = 4) LIMIT 1 Repopulating the stop words Rendered common/_header (16.7ms) Rendered common/_nav (4.4ms) Rendered common/_search (2.4ms) Rendered common/_footer (0.6ms) Completed in 2422ms (View: 134, DB: 2) | 200 OK [http://myserver.com/ wiki_pages/MainPage]

Processing WikiPagesController#show (for 98.218.223.189 at 2009-08-12 20:17:04) [GET]   Parameters: {"id"=>"MainPage"} Rendering template within layouts/application Rendering wiki_pages/show   User Load (0.4ms) SELECT * FROM `users` WHERE (`users`.`id` = 4) LIMIT 1 Rendered common/_header (10.9ms) Rendered common/_nav (1.6ms) Rendered common/_search (1.7ms) Rendered common/_footer (0.2ms) Completed in 138ms (View: 53, DB: 0) | 200 OK [http://myserver.com.com/ wiki_pages/MainPage]

Are you using Passenger?

Jamey

Yes, I'm using passenger with a config like so:

passenger.conf

LoadModule passenger_module /usr/share/passenger/ext/apache2/ mod_passenger.so PassengerRoot /usr/share/passenger PassengerRuby /usr/bin/ruby PassengerLogLevel 2

httpd.conf

RailsBaseURI /my_app

The passenger.conf was configured by my hosting provider and the (one line of) configuration for Apache was via me reading the passenger configuration

Dave

Things may have changed, but AFAIK, passenger does go to sleep after a period of no http activity on your site. What I ususally do is create a cron job that runs every 5 minutes and just hits the web server, something like:

wget http://localhost

or whatever. This ensures that passenger keeps ruby loaded in memory and the app is no longer sluggish on that first hit.

HTH,

Jamey

Passenger will shut down it's rails instances after a certain amount
of inactivity. So when you hit the site after a long absence it takes
a second or two load up the rails instance (just like ./script/console
takes a second or two). After that it's up and you don't have that lag.

You're hosting provider is probably doing this intentionally so it
doesn't have a lot of rails processes sitting around doing nothing.

Looks like this will solve the problem: http://www.modrails.com/documentation/Users%20guide.html#PassengerPoolIdleTime

Just ran into this issue on my VPS.

- JK.