TTL-based caching trades correctness for speed, but there are better strategies that avoid this tradeoff entirely. Five cache strategies are detailed: (1) never invalidating immutable data and relying on LRU eviction, (2) update-on-write to keep the cache current alongside the database, (3) invalidate-on-write when a change affects multiple cache keys, (4) namespacing to handle cases where cache keys can't be enumerated, and (5) HTTP-level caching with cache-busting and PUSH/PURGE verbs. Practical pitfalls are also covered: don't store non-cache data in the cache, never require a cache hit, and configure Redis correctly (set maxmemory and allkeys-lru) for caching use cases. The post also briefly introduces pyappcache, a Python library supporting these patterns.

14m read timeFrom calpaterson.com
Post cover image
Table of contents
Strategy 1: Just never invalidateStrategy 2: Update-on-writeStrategy 3: Invalidate-on-writeStrategy 4: NamespacingStrategy 5: HTTP - and PUSH and PURGETips and pitfallsMentioning my own libraryCaching is undeniably extra faff - but sometimes worth itContact/etcSee also

Sort: