HerokuRichard SchneemanProblem & solution
Cache Invalidation Complexity: Rails 5.2 and Dalli Cache Store
Summary
Rails 5.2+ applications can fail to invalidate ActiveRecord-backed cache entries when cache versioning is enabled alongside a third-party store such as dalli_store 2.7.8 or earlier. Rails’ recyclable cache keys keep the cache_key stable and store an ActiveRecord cache_version in the entry, preventing unrelated entries from being evicted, but Dalli only understands the key and therefore repeatedly returns stale data. The post explains how to check the production cache_store and active_record.cache_versioning settings, then compares switching to Rails’ :mem_cache_store, disabling versioning, and upgrading Dalli to 2.7.9+. Its recommendation is to use :mem_cache_store and flush the cache, while noting that Dalli 2.7.9 fixes invalidation by putting the version into the key rather than implementing recyclable keys.
Context
Rails 5.2 introduced recyclable cache keys through ActiveRecord cache versioning, but third-party cache stores must understand the new cache-entry versioning behavior. With dalli_store 2.7.8 or earlier and cache versioning enabled, the stable cache_key prevents changed ActiveRecord objects from invalidating cached values.
Approach / What changed
The post provides production configuration checks and evaluates three mitigations: switching to Rails’ :mem_cache_store, disabling config.active_record.cache_versioning, or upgrading Dalli to 2.7.9+. It recommends :mem_cache_store followed by a cache flush, and proposes a capability check for custom stores.
Takeaways
Rails’ :mem_cache_store supports cache key recycling and compression; preliminary CodeTriage benchmarks found performance within 1% of the prior raw dalli_store setup.
Disabling cache versioning avoids the incompatibility but changes the cache-key format, effectively producing a cold cache and forfeiting recyclable keys and cache compression.
Dalli 2.7.9 fixes the invalidation problem by inserting the version into the cache key, but it does not implement Rails’ recyclable cache-key behavior.
Source and freshness
- Source
- heroku.com
- Published
- Added to Yomu
Summaries are generated from the original by a language model. Check the original before relying on it.
Keep reading
Related reading
The Good Stuff
Paid picks we use ourselves. Links carry a referral code — a subscription through one earns us a commission at no extra cost to you.
The best engineering reads, once a week
One useful email. Unsubscribe anytime.