Application caching: lessons we paid for

When someone new takes on anything around application caching, we hand them approximately this list. It fits within one page.

The lessons

  1. Refreshing the busiest keys a little ahead of expiry, by one process, ensures readers never pile onto the rebuild.

  2. An overall hit rate near nine in ten can mask a group of keys that almost never hits, so we break the number down by key prefix.

  3. Each cached value should have a clear answer to a single question: when does it go stale? Without that, the cache eventually hands out wrong data.

To show what this looks like, below is the Redis setup we use, in a trimmed form:

# cache only, nothing we cannot rebuild
redis-cli CONFIG SET maxmemory 1gb
redis-cli CONFIG SET maxmemory-policy allkeys-lru
redis-cli INFO stats | grep keyspace
redis-cli --scan --pattern 'menu:*' | head -n 20
redis-cli TTL menu:32995

The current state of our cache setup script lives in our codebase, with a short reason for every line in its comments.