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 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/”. To inspect the contents of this cached entry within the console you’ll want to type“views/”). I warned you that you’d be unlikely to guess the key.

More Articles: Click here for full archive

Debugging Rails with Operating System Tools

Unix/Command-Line level tools for making web-developers' lives easier– lsof, nslookup, gdb, tcpdump, ctags, ack, strace, and top

Debugging Rails with Git

Source control software gives us a unique insight into changes in code and the human intention behind them.

Debugging Rails with Pry Debugger

Master the universal debugger commands "step" and "next" and learn how to crack open a Pry session at any point.