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.
