rack-cache and querystrings
With the introduction of the Cedar platform, Heroku pretty much flipped the bird to everyone that was relying on their reverse proxy cache (Varnish).
The suggested solution for Ruby customers was to use rack-cache.
Apart from the initial configuration pain to make it work with memcache, the experience has been good.
About a month ago we’ve launched a new landing page. This page had a little bit more data crunching than usual. Marketing did also rump-up PPC spending directed to this pages. That quickly activated New Relic alarms, and it meant we had to increase number of dynos.
I expected full page http caching to mitigate this type or problems so I started investigating.
I found out that the software used to manage our PPC ads was adding in the querystring a unique identifier. That meant that rack-cache was creating a different key for each view of this landing page.
To solve this problem I thought it would be nice to exclude certain parameters from the url before rack used it to create a key for the page.
I’ve found a poorly documented configuration paramerter for rack-cache: “rack-cache.cache_key”. Using that it’s possible to pass a class responsible for the creation of a key using the parameters of the requests. So I’ve ended up creating the following class:
As you can see it inherits from Rack::Cache::Key and it overrides the private method query_string in order to remove the parameters we don’t want.
A useful addition suggested by my team mate Federico was to add a warning (it is an exception at the moment, but that’s not ideal) when using any of the blacklisted parameters in development. Just in case we rely of any of this parameters.
This solution works well for now but I’m wondering how other people solve this problem when using a reverse proxy like nginx or varnish. Full page http caching is not applicable in every situation, but, I’ve found that it is easy to understand, performs well and it’s easy to implement.
Am I missing something? Is there a much cleaner solution that I’m not aware of? Let us know!