Rails page caching and monitor services

This evening, I added page caching to a Rails app I’m building. I want to cache the top page of the site (the index action), so I added the caches_page :index to the correct controller.

All was going well until I deployed to a ‘production’ server.

Doing a View/Source on my web page, I see urls that looked like http:// /foo/bar with a space replacing the server name. This isn’t good.

It took me a few minutes of looking at the Rails production log to see that, about every 60 seconds, something was hitting my site without a server name. What hits my site every 60 seconds? Monit does!

For each of my Mongrel instances, I have a monit configuration that contains the line if failed port xxxx protocol http. This technique doesn’t send a server name when it does the test.

Why is this a problem?

Since monit is hitting the root of the web site, the cached url is what gets accessed. Since I have monit hitting the Mongrel instances directly, the page cache is bypassed, and a broken version of the page is generated and cached. Within 60 seconds, my site is broken!

What do I do?

I solved this by adding a ‘heartbeat’ action to my site that renders the text ‘OK’.

  def heartbeat
    render :text => "OK"
  end

I now have my monit configured to access the url instead of the port checking. This is also much lighter load than hitting the main page of the site.

if failed url http://127.0.0.1:xxxx/main/heartbeat content == "OK"

This entry was posted on Sun, 11 Mar 2007 07:58:58 GMT . You can follow any any response to this entry through the Atom feed. You can leave a comment or a trackback from your own site.
Tags


Trackbacks

Use the following link to trackback from your own site:
http://blog.craz8.com/trackbacks?article_id=rails-page-caching-and-monitor-services&day=10&month=03&year=2007

Comments

Leave a response

  1. Chris 1 day later:

    Simple and very elegant. Thanks for this … make sense.

  2. Dan Kubb 4 days later:

    Hmm. I use monit too, but I haven’t seen this problem. What if you change the monit configuration line to:

    if failed host [hostname] port [port] protocol http

  3. tom 4 days later:

    @Dan:

    That’s the line I had previously. Until I added page caching to the home page, I really didn’t notice the Monit calls on my server, but it seems to be doing a GET on / for the port to check that HTTP is running there, causing a hit on my home page (and re-building the cached page.

    Another interesting point – my home page was being served at 9 requests per second (hence the need for caching), but my heartbeat action, with sessions disabled for that action, runs at 2000 requests per second which is a lot more reasonable.

Leave a comment