• Random
  • Archive
  • RSS
  • Ask me anything

Ryan Angilly

A business guy who became a really good software developer first by accident

I love fragment caching in Rails

Phil Karlton said the only difficult things in software development were cache expiration and naming things.

While this statement can be debated, nobody can argue that these two things are extremely difficult (and for the record, I tend to agree with Phil).  This post is about the first one: caching and the expiration of that cache.

While caching can become a headache as the data model gets more complex, at a rudimentary level, a little caching in Rails can not only be done extremely easily, but it can go a LONG way.

FourthSegment had a dashboard action that was performing less than ideal, so I decided to spend some time seeing if I could speed things up.

I sat down for about 20 minutes, made a couple changes, and pushed them live.  The results — recorded over 7 days by New Relic —  speak for themselves:

Yeah.

All I did here was implement some simple fragment caching around the view of the conversation table.  The caching was extremely simple:

And the expiration was also simple:

Note that the cache fragments are keyed by a prefix and the site ID.  If I left the site ID out, then all users would see the dashboard from whoever was the last one to refresh the fragment.  Not ideal :)

FourthSegment runs on Heroku, so I used their free memcached add-on to facilitate the actual caching.  It was super easy to setup.

There’s something very interesting in this graph that might not be obvious right away.  Notice the dark green on the left side.  That’s request queuing time.  It’s the amount of time Heroku’s nginx layer had to hold onto the request before my application was ready to process it.  It doesn’t show up nearly as much after the fix was put in place.  In this case, the dashboard was taking so long that, despite having several concurrent processing threads, they were piling up and blocking other requests.

The bottom line here is that you shouldn’t let the fact that caching is hard stop you from spending a little bit of time on your worst endpoints.  It can go a long way towards speeding up your entire application.

    • #fragment caching
    • #caching
    • #rails
    • #memcache
    • #phil karlton
  • 10 months ago
  • 43
  • Comments
  • Permalink
  • Share
    Tweet

Portrait/Logo

About

Hi, I'm Ryan, and I build stuff on the internet. I'm currently building Signal Genius.

I blog about my failed startup, MessageSling, at The Day Series.

Things I used to do:

  • Built and launched FourthSegment
  • Hacked at Punchbowl.com.
  • Founded MessageSling.com.
  • Spent several years at EMC

Me, Elsewhere

  • @angilly on Twitter
  • Facebook Profile
  • angilly on Flickr
  • angilly on Foursquare
  • My Skype Info
  • ryana on github

Twitter

loading tweets…

Following

I Dig These Posts

  • Photo via tmills

    A serious bath-taking bear.

    [via]

    Photo via tmills
  • Photo via tmills

    I read this thing on Vice tonight about how girls hate girls even when they’re friends and while all things women are forever hermetically sealed...

    Photo via tmills
  • Link via graysky
    cdixon.org – chris dixon's blog / Best practices for raising a VC round

    (via Instapaper)

    Link via graysky
  • Photo via dancroak

    Puppy.

    Photo via dancroak
See more →
  • RSS
  • Random
  • Archive
  • Ask me anything
  • Mobile

Effector Theme by Carlo Franco.

Powered by Tumblr