Debugging Rails with Memcached

Many apps use Memcached in their infrastructure so it's important to know how to debug thorny caching errors.

Part 8 in the series A Comprehensive Guide To Debugging Rails

Memcached Mirrors

  • Memcached general stats: In production you can use the Heroku dashboard to access detailed stats about Memcached usage. You’ll know you’re doing it right if you have a high hit rate, a low eviction rate, and a healthy excess of memory used compared to the allocated Memcached size (Once you run out of space Memcached deletes the oldest cached item, reducing the effectiveness of your caching). Memcached won’t alert you whenever your cache is in a poor state, so you’ll need to log in and check regularly. Indeed I found my cache in a rotten form when I was researching this article. You can also view global Memcached stats with the Rails console using Rails.cache.stats. Read this for a legend for reading memcached stats output, and then understand some confusing nuances with this Stack Overflow discussion.

  • Memcached Cache contents: In your codebase or in the console, set the value for a cache key with Rails.cache.write(key) { code_that_returns_value } or Rails.cache.fetch(key) {code_that_returns_value }. Read the value associated with a particular key using Rails.cache.read(key). For cached content set at a controller level, for example set with with caches_action, the cache key isn’t easy to guess from what you read within the code. Figure out the key by temporarily setting the log level on production to debug with heroku config:add LOG_LEVEL="debug" then searching for entries starting with “Cache” that get triggered following a request to the particular page you are debugging. One example of such an entry is “Cache read: views/www.oxbridgenotes.co.uk/?cache_tag=1398678715/1379180679/407/6804/1398699264&format=html.html”. To inspect the contents of this cached entry within the console you’ll want to type Rails.cache.read(“views/www.oxbridgenotes.co.uk/?cache_tag=1398678715/1379180679/407/6804/1398699264&format=html.html”). I warned you that you’d be unlikely to guess the key.


More Articles:

Debugging Rails with Logs

Everything you ought to be looking at when interpreting the Rails built-in logs. Also looks at other logs, such as Amazon S3 access logs and scheduler logs.


A Comprehensive Guide To Debugging Rails

Feedback systems for finding errors in your web application.


Debugging Rails with Pry Console

Tips and tricks for snooping about your codebase with the Pry console. You'll learn how to view the source code of ANY method on demand, see global variables, or change "self" to another object.