Files
skills/public/caching-cache-invalidation-strategies.html
zeekayandHanzo Dev 9e10c1c10e Each llms index points at the other two
hanzoskills.com/llms.txt now opens with the install line, the API
reference index and the company index, so an agent that lands on any
one of the three can find the rest.

robots.txt claimed 537 documents while llms.txt counts 920. The number
is gone from robots.txt; llms.txt derives it.

public/ is regenerated from scripts/site.py, which it had fallen
behind: the copy-prompt menu and agent bar were in the generator and
not in the committed pages. tests/site.mjs passes against the result.

Co-authored-by: Hanzo Dev <[email protected]>
2026-09-11 00:27:59 -07:00

713 lines
36 KiB
HTML

<!doctype html>
<html lang="en"><head>
<meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
<title>caching-cache-invalidation-strategies</title>
<meta name="color-scheme" content="dark">
<style>
/* Committed to one visual world: a dark instrument panel. No light variant —
the dot field and the hairline rules only read on black, and a naive
inversion would wash both out. Every colour is painted explicitly so the page
never borrows a host background. */
@font-face{font-family:'Zen';src:url('/fonts/Zen-Variable.woff2') format('woff2');font-weight:100 900;font-style:normal;font-display:swap}
@font-face{font-family:'Zen';src:url('/fonts/Zen-Italic-Variable.woff2') format('woff2');font-weight:100 900;font-style:italic;font-display:swap}
@font-face{font-family:'Zen Mono';src:url('/fonts/ZenMono-Variable.woff2') format('woff2');font-weight:100 900;font-style:normal;font-display:swap}
@font-face{font-family:'Zen Mono';src:url('/fonts/ZenMono-Italic-Variable.woff2') format('woff2');font-weight:100 900;font-style:italic;font-display:swap}
@font-face{font-family:'Zen Pixel Square';src:url('/fonts/ZenPixel-Square.woff2') format('woff2');font-style:normal;font-display:swap}
:root{
--ink:#f4f4f2; --soft:#9c9c98; --faint:#6a6a66; --ground:#000; --panel:#0a0a0a;
--line:#232323; --line-hi:#3a3a3a; --accent:#f4f4f2;
--font-zen-sans:'Zen',-apple-system,BlinkMacSystemFont,"Segoe UI",Helvetica,Arial,sans-serif;
--font-zen-mono:'Zen Mono',ui-monospace,"SF Mono",SFMono-Regular,Menlo,Consolas,monospace;
--font-zen-pixel-square:'Zen Pixel Square',monospace;
--mono:var(--font-zen-mono);
--sans:var(--font-zen-sans);
/* Controls are pills and surfaces are soft, as on hanzo.ai. */
--r:999px; --r-soft:14px;
}
*{box-sizing:border-box}
html{-webkit-text-size-adjust:100%}
body{margin:0;background:var(--ground);color:var(--ink);font-family:var(--sans);
font-size:16px;line-height:1.65;-webkit-font-smoothing:antialiased}
.wrap{max-width:60rem;margin:0 auto;padding:0 1.75rem}
/* The dot field. A halftone made of one repeating radial-gradient, faded out
along the diagonal by a mask so it reads as a wave rather than wallpaper. */
/* No overflow clip here: the dot field is inset to the box already, and a clip
would cut off the copy-prompt menu. */
.hero{position:relative;border-bottom:1px solid var(--line)}
.hero::before{content:"";position:absolute;inset:0;
background-image:radial-gradient(var(--line-hi) 1px, transparent 1px);
background-size:7px 7px;
-webkit-mask-image:linear-gradient(115deg,#000 0%,rgba(0,0,0,.55) 38%,transparent 72%);
mask-image:linear-gradient(115deg,#000 0%,rgba(0,0,0,.55) 38%,transparent 72%);
pointer-events:none}
.hero .wrap{position:relative;padding-top:5.5rem;padding-bottom:4rem}
nav{border-bottom:1px solid var(--line)}
nav .in{max-width:60rem;margin:0 auto;padding:1.1rem 1.75rem;display:flex;
justify-content:space-between;align-items:center;gap:1rem}
.brand{font-family:var(--mono);font-size:.85rem;letter-spacing:.14em;
text-transform:uppercase;color:var(--ink);text-decoration:none;
display:inline-flex;align-items:center;gap:.6rem}
.brand i{font-style:normal;color:var(--faint)}
.mark{width:1.15em;height:1.15em;display:block;flex:0 0 auto}
nav a.up{font-family:var(--mono);font-size:.72rem;letter-spacing:.14em;
text-transform:uppercase;color:var(--soft);text-decoration:none}
nav a.up:hover{color:var(--ink)}
h1{font-family:var(--mono);font-size:clamp(2rem,5.4vw,3.4rem);line-height:1.12;
letter-spacing:-.02em;margin:0 0 1.1rem;font-weight:500;text-wrap:balance}
h1 em{font-style:normal;color:var(--faint)}
.lede{color:var(--soft);font-size:1.05rem;max-width:52ch;margin:0 0 2.25rem}
.btns{display:flex;flex-wrap:wrap;gap:.7rem;margin:0 0 .5rem;align-items:center}
.btn{display:inline-flex;align-items:center;gap:.5rem;font-family:var(--mono);
font-size:.72rem;letter-spacing:.14em;text-transform:uppercase;
padding:.8rem 1.4rem;border:1px solid var(--line-hi);border-radius:var(--r);
color:var(--ink);text-decoration:none;background:transparent;cursor:pointer;
line-height:1.2;font-weight:400}
.btn:hover{background:#141414;text-decoration:none;border-color:var(--soft)}
.btn.solid{background:var(--ink);color:#000;border-color:var(--ink)}
.btn.solid:hover{background:#d8d8d4}
.btn .chev{width:.7em;height:.7em;flex:0 0 auto;transition:transform .15s ease}
.btn[aria-expanded="true"] .chev{transform:rotate(180deg)}
/* Copy-prompt selector. Each row copies the prompt for its own target. */
.copy{position:relative;display:inline-flex}
.menu{position:absolute;top:calc(100% + .5rem);left:0;z-index:20;min-width:21rem;
background:var(--panel);border:1px solid var(--line-hi);border-radius:var(--r-soft);
padding:.4rem;display:flex;flex-direction:column;
box-shadow:0 12px 32px rgba(0,0,0,.75)}
.menu[hidden]{display:none}
.mi{display:flex;align-items:center;gap:.7rem;width:100%;text-align:left;
padding:.62rem .8rem;border:0;border-radius:calc(var(--r-soft) - .35rem);
background:transparent;color:var(--ink);font-family:var(--mono);font-size:.76rem;
letter-spacing:.04em;text-decoration:none;cursor:pointer;line-height:1.3;
white-space:nowrap}
.mi:hover,.mi:focus-visible{background:#181818;outline:none}
.mi .mark{width:1.05rem;height:1.05rem;color:var(--soft)}
.mi:hover .mark{color:var(--ink)}
.mi u{margin-left:auto;color:var(--faint);text-decoration:none;font-size:.68rem;
letter-spacing:.1em;text-transform:uppercase}
/* Works-with bar: one scrolling row, name and mark per agent. */
.strip{border-bottom:1px solid var(--line);background:var(--panel)}
.strip .in{max-width:60rem;margin:0 auto;padding:.9rem 1.75rem;display:flex;
align-items:center;gap:1.25rem;font-family:var(--mono);font-size:.72rem;
letter-spacing:.1em;text-transform:uppercase;color:var(--faint)}
.strip b{color:var(--soft);font-weight:400;flex:0 0 auto;
border-right:1px solid var(--line);padding-right:1.25rem;align-self:stretch;
display:flex;align-items:center}
.bar{display:flex;align-items:center;gap:.5rem;flex:1 1 auto;min-width:0;
overflow-x:auto;scrollbar-width:none;scroll-snap-type:x proximity;
-webkit-overflow-scrolling:touch;
-webkit-mask-image:linear-gradient(90deg,#000 calc(100% - 3rem),transparent);
mask-image:linear-gradient(90deg,#000 calc(100% - 3rem),transparent)}
.bar::-webkit-scrollbar{display:none}
.agent{display:inline-flex;align-items:center;gap:.45rem;flex:0 0 auto;
padding:.4rem .85rem;border:1px solid var(--line);border-radius:var(--r);
color:var(--soft);white-space:nowrap;scroll-snap-align:start;
text-decoration:none;background:var(--ground)}
.agent:hover{color:var(--ink);border-color:var(--line-hi);text-decoration:none}
.agent .mark{width:.95rem;height:.95rem}
h2{font-family:var(--mono);font-size:1.1rem;letter-spacing:-.01em;margin:2.75rem 0 .75rem;font-weight:500}
h3{font-size:1rem;margin:2rem 0 .5rem;font-weight:600}
h4,h5,h6{font-size:.94rem;margin:1.5rem 0 .5rem;font-weight:600}
p{margin:0 0 1rem;max-width:70ch}
ul,ol{margin:0 0 1rem;padding-left:1.35rem;max-width:70ch}
li{margin:.22rem 0}
a{color:var(--ink);text-decoration:underline;text-underline-offset:3px;
text-decoration-color:var(--line-hi)}
a:hover{text-decoration-color:var(--ink)}
a:focus-visible{outline:1px solid var(--ink);outline-offset:3px}
code{font-family:var(--mono);font-size:.85em;background:var(--panel);
border:1px solid var(--line);border-radius:5px;padding:.1em .36em}
pre{background:var(--panel);border:1px solid var(--line);padding:1rem 1.1rem;
border-radius:var(--r-soft);overflow-x:auto;margin:0 0 1.25rem}
pre code{background:none;border:0;padding:0;font-size:.82rem;line-height:1.7}
blockquote{margin:0 0 1rem;padding-left:1rem;border-left:1px solid var(--line-hi);color:var(--soft)}
hr{border:0;border-top:1px solid var(--line);margin:2.5rem 0}
table{border-collapse:collapse;width:100%;margin:0 0 1.25rem;font-size:.92rem;
display:block;overflow-x:auto}
th,td{border:1px solid var(--line);padding:.5rem .7rem;text-align:left}
.curl{display:block;background:var(--panel);border:1px solid var(--line);
border-radius:var(--r-soft);padding:.95rem 1.1rem;font-family:var(--mono);
font-size:.8rem;color:var(--soft);overflow-x:auto;margin:0 0 2.5rem;
white-space:pre;line-height:1.9}
.curl b{color:var(--ink);font-weight:400}
.curl i{font-style:normal;color:var(--faint)}
/* index */
#q{width:100%;padding:.85rem 1.2rem;font-family:var(--mono);font-size:.85rem;color:var(--ink);
background:var(--panel);border:1px solid var(--line);border-radius:var(--r);margin:0 0 .6rem}
#q::placeholder{color:var(--faint);text-transform:uppercase;letter-spacing:.1em;font-size:.72rem}
#q:focus{outline:none;border-color:var(--soft)}
#count{color:var(--faint);font-family:var(--mono);font-size:.72rem;letter-spacing:.1em;
text-transform:uppercase;margin:0 0 2rem}
.cat{margin:2.5rem 0 .5rem;font-family:var(--mono);font-size:.7rem;letter-spacing:.16em;
text-transform:uppercase;color:var(--faint);border-bottom:1px solid var(--line);
padding-bottom:.45rem}
.row{display:block;padding:.6rem .9rem;margin:0 -.9rem;border-bottom:1px solid var(--line);
border-radius:var(--r-soft);text-decoration:none}
.row:hover{background:#0a0a0a}
.nm{font-family:var(--mono);font-size:.84rem;color:var(--ink);display:block}
.ds{color:var(--soft);font-size:.9rem;display:block;margin-top:.12rem}
footer{margin-top:4rem;padding:2rem 0 4rem;border-top:1px solid var(--line);
color:var(--faint);font-size:.85rem}
footer a{color:var(--soft)}
.fmap{display:grid;gap:1.75rem;grid-template-columns:repeat(auto-fit,minmax(11rem,1fr));
margin:0 0 2rem;font-size:.85rem}
.fmap b{display:block;font-family:var(--mono);font-size:.68rem;letter-spacing:.16em;
text-transform:uppercase;color:var(--faint);font-weight:400;margin:0 0 .6rem;
border-bottom:1px solid var(--line);padding-bottom:.4rem}
.fmap a{display:inline-block;color:var(--ink);text-decoration:none;margin:.3rem .35rem 0 0}
.fmap a:hover{text-decoration:underline}
.fmap div{color:var(--faint);line-height:1.5}
.fend{border-top:1px solid var(--line);padding-top:1.25rem;font-size:.82rem}
@media (max-width:640px){
.hero .wrap{padding-top:3.25rem;padding-bottom:2.75rem}
.btns{gap:.5rem}
.btn{padding:.75rem 1.15rem;font-size:.68rem}
.copy,.copy .btn{width:100%;justify-content:center}
.menu{left:0;right:0;min-width:0}
.strip .in{flex-direction:column;align-items:flex-start;gap:.6rem;padding:.9rem 1.75rem}
.strip b{border-right:0;padding-right:0}
.bar{width:100%}
}
</style>
<link rel="alternate" type="text/markdown" href="/caching-cache-invalidation-strategies.md">
<link rel="icon" href="/favicon.svg">
<link rel="canonical" href="https://hanzoskills.com/caching-cache-invalidation-strategies">
</head>
<!-- Reading this with a machine? Every page here is also plain markdown.
This one: https://hanzoskills.com/caching-cache-invalidation-strategies.md
Index: https://hanzoskills.com/llms.txt
Corpus: https://hanzoskills.com/all.md
Append .md to any path for the markdown twin. -->
<body>
<nav><div class="in">
<a class="brand" href="/"><svg class="mark" viewBox="0 0 67 67" fill="currentColor" aria-hidden="true"><path d="M22.21 67V44.6369H0V67H22.21Z"/><path d="M66.7038 22.3184H22.2534L0.0878906 44.6367H44.4634L66.7038 22.3184Z"/><path d="M22.21 0H0V22.3184H22.21V0Z"/><path d="M66.7198 0H44.5098V22.3184H66.7198V0Z"/><path d="M66.7198 67V44.6369H44.5098V67H66.7198Z"/></svg> HANZO<i>SKILLS</i></a>
<a class="up" href="/">All skills</a>
</div></nav>
<div class="wrap">
<h1>caching-cache-invalidation-strategies</h1><p class="lede">Cache invalidation patterns and techniques - time-based, event-based, key-based, and version-based invalidation for maintaining cache consistency.</p><h1>Cache Invalidation Strategies</h1>
<p><strong>Last Updated</strong>: 2025-10-25</p>
<h2>When to Use This Skill</h2>
<p>Use this skill when:</p>
<ul><li>Ensuring cached data stays synchronized with source of truth</li><li>Designing invalidation strategies for distributed caches</li><li>Dealing with stale cache issues in production</li><li>Implementing event-driven cache updates</li><li>Migrating from simple TTL to sophisticated invalidation</li><li>Optimizing cache hit rates while maintaining data freshness</li></ul>
<p><strong>Famous quote</strong>: &quot;There are only two hard things in Computer Science: cache invalidation and naming things.&quot; - Phil Karlton</p>
<p><strong>Prerequisites</strong>: Understanding of <code>caching-fundamentals.md</code> and <code>redis-caching-patterns.md</code></p>
<h2>Core Concepts</h2>
<p>Cache invalidation is the process of removing or updating stale data from a cache. The challenge is balancing <strong>freshness</strong> (data accuracy) with <strong>performance</strong> (cache hit rate).</p>
<h3>Invalidation Strategy Decision Matrix</h3>
<p>| Strategy | Freshness | Complexity | Use Case | |----------|-----------|------------|----------| | <strong>Time-Based</strong> | Low-Medium | Low | Content that changes predictably | | <strong>Event-Based</strong> | High | Medium-High | Real-time updates required | | <strong>Key-Based</strong> | High | Medium | Related data needs coordinated invalidation | | <strong>Version-Based</strong> | Very High | Low-Medium | Immutable data with versions |</p>
<h2>Time-Based Invalidation</h2>
<h3>Fixed TTL (Time-To-Live)</h3>
<p><strong>Simplest approach</strong> - cache entries expire after a fixed duration.</p>
<pre><code class="lang-python">import redis
import json
from datetime import datetime, timedelta
class FixedTTLCache:
def __init__(self, redis_client: redis.Redis):
self.cache = redis_client
def set_with_ttl(self, key: str, value: dict, ttl_seconds: int) -&gt; None:
&quot;&quot;&quot;Set cache entry with fixed TTL&quot;&quot;&quot;
self.cache.setex(key, ttl_seconds, json.dumps(value))
print(f&quot;Cached {key} with TTL={ttl_seconds}s&quot;)
def get(self, key: str) -&gt; dict:
&quot;&quot;&quot;Get cached value (auto-expires after TTL)&quot;&quot;&quot;
cached = self.cache.get(key)
if cached:
ttl_remaining = self.cache.ttl(key)
print(f&quot;Cache hit: {key} (expires in {ttl_remaining}s)&quot;)
return json.loads(cached)
print(f&quot;Cache miss: {key}&quot;)
return None
# Usage: Different TTLs for different data types
cache = FixedTTLCache(redis_client)
cache.set_with_ttl(&quot;user:123&quot;, user_data, 3600) # 1 hour (semi-static)
cache.set_with_ttl(&quot;product:456&quot;, product_data, 300) # 5 minutes (dynamic)
cache.set_with_ttl(&quot;homepage&quot;, html, 60) # 1 minute (very dynamic)</code></pre>
<h3>Absolute Expiration</h3>
<p><strong>Expire at specific time</strong> - useful for time-sensitive data.</p>
<pre><code class="lang-python">from datetime import datetime, timezone
class AbsoluteExpirationCache:
def __init__(self, redis_client: redis.Redis):
self.cache = redis_client
def set_expire_at(self, key: str, value: dict, expire_at: datetime) -&gt; None:
&quot;&quot;&quot;Set cache to expire at specific timestamp&quot;&quot;&quot;
self.cache.set(key, json.dumps(value))
self.cache.expireat(key, int(expire_at.timestamp()))
print(f&quot;Cached {key}, expires at {expire_at.isoformat()}&quot;)
# Usage: Flash sale ending at specific time
sale_end = datetime(2025, 10, 25, 23, 59, 59, tzinfo=timezone.utc)
cache.set_expire_at(&quot;flash_sale:electronics&quot;, sale_data, sale_end)</code></pre>
<h3>Sliding Expiration</h3>
<p><strong>Reset TTL on access</strong> - keeps frequently accessed data cached.</p>
<pre><code class="lang-python">class SlidingExpirationCache:
def __init__(self, redis_client: redis.Redis, ttl: int = 1800):
self.cache = redis_client
self.ttl = ttl # 30 minutes default
def get_with_sliding(self, key: str) -&gt; dict:
&quot;&quot;&quot;Get value and reset TTL (sliding window)&quot;&quot;&quot;
cached = self.cache.get(key)
if cached:
# Reset expiration on access
self.cache.expire(key, self.ttl)
print(f&quot;Cache hit: {key}, TTL reset to {self.ttl}s&quot;)
return json.loads(cached)
return None
def set_sliding(self, key: str, value: dict) -&gt; None:
self.cache.setex(key, self.ttl, json.dumps(value))
# User session storage: extends session on activity
session_cache = SlidingExpirationCache(redis_client, ttl=1800)</code></pre>
<h2>Event-Based Invalidation</h2>
<h3>Pub/Sub Invalidation</h3>
<p><strong>Invalidate on data change events</strong> - use Redis Pub/Sub to propagate invalidation.</p>
<pre><code class="lang-python">import threading
class PubSubInvalidation:
def __init__(self, redis_client: redis.Redis):
self.cache = redis_client
self.pubsub = redis_client.pubsub()
def start_listener(self):
&quot;&quot;&quot;Listen for invalidation events&quot;&quot;&quot;
self.pubsub.subscribe(&#x27;cache:invalidate&#x27;)
thread = threading.Thread(target=self._listen)
thread.daemon = True
thread.start()
def _listen(self):
&quot;&quot;&quot;Background thread processing invalidation events&quot;&quot;&quot;
for message in self.pubsub.listen():
if message[&#x27;type&#x27;] == &#x27;message&#x27;:
key_pattern = message[&#x27;data&#x27;].decode(&#x27;utf-8&#x27;)
self._invalidate_pattern(key_pattern)
def _invalidate_pattern(self, pattern: str):
&quot;&quot;&quot;Invalidate all keys matching pattern&quot;&quot;&quot;
keys = self.cache.keys(pattern)
if keys:
self.cache.delete(*keys)
print(f&quot;Invalidated {len(keys)} keys matching {pattern}&quot;)
def publish_invalidation(self, key_pattern: str):
&quot;&quot;&quot;Publish invalidation event&quot;&quot;&quot;
self.cache.publish(&#x27;cache:invalidate&#x27;, key_pattern)
print(f&quot;Published invalidation: {key_pattern}&quot;)
# Usage
invalidator = PubSubInvalidation(redis_client)
invalidator.start_listener()
# When user updates profile
def update_user_profile(user_id: int, new_data: dict):
save_to_database(user_id, new_data)
# Invalidate all user-related caches
invalidator.publish_invalidation(f&quot;user:{user_id}:*&quot;)</code></pre>
<h3>Change Data Capture (CDC)</h3>
<p><strong>Invalidate based on database changes</strong> - monitor database transaction log.</p>
<pre><code class="lang-python"># Pseudo-code for CDC-based invalidation (using Debezium/Kafka pattern)
class CDCInvalidation:
def __init__(self, kafka_consumer, redis_client):
self.consumer = kafka_consumer
self.cache = redis_client
def process_cdc_events(self):
&quot;&quot;&quot;Process CDC events from Kafka&quot;&quot;&quot;
for message in self.consumer:
event = json.loads(message.value)
if event[&#x27;operation&#x27;] in [&#x27;UPDATE&#x27;, &#x27;DELETE&#x27;]:
# Extract entity ID from CDC event
entity_type = event[&#x27;table&#x27;]
entity_id = event[&#x27;after&#x27;][&#x27;id&#x27;] if event[&#x27;operation&#x27;] == &#x27;UPDATE&#x27; else event[&#x27;before&#x27;][&#x27;id&#x27;]
# Invalidate related cache entries
self._invalidate_entity(entity_type, entity_id)
def _invalidate_entity(self, entity_type: str, entity_id: int):
&quot;&quot;&quot;Invalidate cache for specific entity&quot;&quot;&quot;
pattern = f&quot;{entity_type}:{entity_id}:*&quot;
keys = self.cache.keys(pattern)
if keys:
self.cache.delete(*keys)
print(f&quot;CDC invalidation: {entity_type}:{entity_id}&quot;)
# Real-world example: PostgreSQL logical replication + Kafka
# 1. Enable logical replication in PostgreSQL
# 2. Debezium connector streams changes to Kafka
# 3. Consumer invalidates cache based on database changes</code></pre>
<h2>Key-Based Invalidation (Cache Tags)</h2>
<h3>Surrogate Keys / Cache Tags</h3>
<p><strong>Tag cache entries for bulk invalidation</strong> - Netflix uses this pattern.</p>
<pre><code class="lang-python">class CacheTagInvalidation:
def __init__(self, redis_client: redis.Redis):
self.cache = redis_client
def set_with_tags(self, key: str, value: dict, tags: list[str], ttl: int = 3600):
&quot;&quot;&quot;Store cache entry with associated tags&quot;&quot;&quot;
# Store the value
self.cache.setex(key, ttl, json.dumps(value))
# Associate key with each tag
for tag in tags:
tag_set = f&quot;tag:{tag}&quot;
self.cache.sadd(tag_set, key)
self.cache.expire(tag_set, ttl + 300) # Tag outlives cache entry
print(f&quot;Cached {key} with tags: {tags}&quot;)
def invalidate_by_tag(self, tag: str):
&quot;&quot;&quot;Invalidate all cache entries with specific tag&quot;&quot;&quot;
tag_set = f&quot;tag:{tag}&quot;
keys = self.cache.smembers(tag_set)
if keys:
# Delete all tagged entries
self.cache.delete(*keys)
# Delete tag set
self.cache.delete(tag_set)
print(f&quot;Invalidated {len(keys)} entries with tag &#x27;{tag}&#x27;&quot;)
# Usage: E-commerce product cache
tagger = CacheTagInvalidation(redis_client)
product_data = {&quot;id&quot;: 123, &quot;name&quot;: &quot;Laptop&quot;, &quot;category_id&quot;: 5, &quot;brand_id&quot;: 10}
tagger.set_with_tags(
key=&quot;product:123&quot;,
value=product_data,
tags=[&quot;product&quot;, &quot;category:5&quot;, &quot;brand:10&quot;],
ttl=3600
)
# When category 5 updates, invalidate all products in that category
tagger.invalidate_by_tag(&quot;category:5&quot;)</code></pre>
<h3>Dependency Graph Invalidation</h3>
<p><strong>Invalidate related data hierarchically</strong> - complex but precise.</p>
<pre><code class="lang-python">class DependencyGraphInvalidation:
def __init__(self, redis_client: redis.Redis):
self.cache = redis_client
def set_with_dependencies(self, key: str, value: dict, dependencies: list[str], ttl: int = 3600):
&quot;&quot;&quot;Store cache with dependency tracking&quot;&quot;&quot;
self.cache.setex(key, ttl, json.dumps(value))
# Store dependency graph
dep_key = f&quot;deps:{key}&quot;
self.cache.sadd(dep_key, *dependencies)
self.cache.expire(dep_key, ttl)
def invalidate_cascade(self, key: str):
&quot;&quot;&quot;Recursively invalidate key and all dependents&quot;&quot;&quot;
# Find all keys that depend on this key
dependents = self._find_dependents(key)
# Invalidate in topological order
for dependent in dependents:
self.cache.delete(dependent)
self.cache.delete(f&quot;deps:{dependent}&quot;)
# Invalidate the key itself
self.cache.delete(key)
self.cache.delete(f&quot;deps:{key}&quot;)
print(f&quot;Cascade invalidation: {key} + {len(dependents)} dependents&quot;)
def _find_dependents(self, key: str) -&gt; list[str]:
&quot;&quot;&quot;Find all keys that depend on given key&quot;&quot;&quot;
dependents = []
# Scan all dependency sets (simplified - use index in production)
for dep_key in self.cache.scan_iter(&quot;deps:*&quot;):
deps = self.cache.smembers(dep_key)
if key.encode() in deps:
dependent_key = dep_key.decode().replace(&quot;deps:&quot;, &quot;&quot;)
dependents.append(dependent_key)
return dependents
# Usage: User profile affects multiple derived caches
graph = DependencyGraphInvalidation(redis_client)
graph.set_with_dependencies(&quot;user:123&quot;, user_data, dependencies=[], ttl=3600)
graph.set_with_dependencies(&quot;user:123:posts&quot;, posts, dependencies=[&quot;user:123&quot;], ttl=1800)
graph.set_with_dependencies(&quot;user:123:feed&quot;, feed, dependencies=[&quot;user:123&quot;, &quot;user:123:posts&quot;], ttl=600)
# When user updates, cascade invalidation
graph.invalidate_cascade(&quot;user:123&quot;) # Invalidates user, posts, feed</code></pre>
<h2>Version-Based Invalidation</h2>
<h3>Immutable Data with Versioning</h3>
<p><strong>Never invalidate, just create new versions</strong> - GitHub uses this pattern.</p>
<pre><code class="lang-python">import hashlib
class VersionBasedCache:
def __init__(self, redis_client: redis.Redis):
self.cache = redis_client
def set_versioned(self, base_key: str, value: dict, ttl: int = 86400):
&quot;&quot;&quot;Store data with content-based version key&quot;&quot;&quot;
# Generate version hash from content
content_hash = self._hash_content(value)
versioned_key = f&quot;{base_key}:v:{content_hash}&quot;
# Store with long TTL (immutable data)
self.cache.setex(versioned_key, ttl, json.dumps(value))
# Update pointer to latest version
self.cache.setex(f&quot;{base_key}:latest&quot;, ttl, content_hash)
print(f&quot;Stored {versioned_key} (immutable)&quot;)
return content_hash
def get_versioned(self, base_key: str, version: str = None) -&gt; dict:
&quot;&quot;&quot;Get specific version or latest&quot;&quot;&quot;
if not version:
# Get latest version
version = self.cache.get(f&quot;{base_key}:latest&quot;)
if not version:
return None
version = version.decode()
versioned_key = f&quot;{base_key}:v:{version}&quot;
cached = self.cache.get(versioned_key)
return json.loads(cached) if cached else None
def _hash_content(self, value: dict) -&gt; str:
&quot;&quot;&quot;Generate content hash for versioning&quot;&quot;&quot;
content = json.dumps(value, sort_keys=True)
return hashlib.sha256(content.encode()).hexdigest()[:12]
# Usage: API responses with versioning
vcache = VersionBasedCache(redis_client)
# Store new version
version = vcache.set_versioned(&quot;api:users:list&quot;, users_data)
# Get latest
latest = vcache.get_versioned(&quot;api:users:list&quot;)
# Get specific version (for rollback or A/B testing)
specific = vcache.get_versioned(&quot;api:users:list&quot;, version=&quot;a1b2c3d4e5f6&quot;)</code></pre>
<h3>Cache Busting with Fingerprints</h3>
<p><strong>Frontend asset invalidation</strong> - append hash to filenames.</p>
<pre><code class="lang-typescript">// TypeScript: Frontend asset fingerprinting
class AssetFingerprinting {
private manifest: Map&lt;string, string&gt; = new Map();
// Build time: Generate manifest
generateManifest(files: string[]): void {
files.forEach(file =&gt; {
const hash = this.hashFile(file);
const fingerprinted = this.addFingerprint(file, hash);
this.manifest.set(file, fingerprinted);
});
}
// Runtime: Get fingerprinted URL
getAssetUrl(file: string): string {
return this.manifest.get(file) || file;
}
private addFingerprint(file: string, hash: string): string {
const ext = file.split(&#x27;.&#x27;).pop();
const base = file.replace(`.${ext}`, &#x27;&#x27;);
return `${base}.${hash}.${ext}`;
}
private hashFile(file: string): string {
// Simplified: Use file content hash in production
return Math.random().toString(36).substring(2, 10);
}
}
// HTML generation with fingerprinted assets
const assets = new AssetFingerprinting();
// app.js -&gt; app.a1b2c3d4.js (immutable, cache forever)
const scriptUrl = assets.getAssetUrl(&#x27;app.js&#x27;);
// &lt;script src=&quot;/static/app.a1b2c3d4.js&quot;&gt;&lt;/script&gt;
// Cache-Control: public, max-age=31536000, immutable</code></pre>
<h2>Netflix Case Study: Mixed Invalidation</h2>
<p><strong>Real-world example</strong>: Netflix hybrid approach (2024 data).</p>
<pre><code class="lang-python">class NetflixStyleInvalidation:
&quot;&quot;&quot;
Netflix invalidation strategy (simplified):
- Time-based for most content (TTL)
- Event-based for critical updates (user actions)
- Key-based for related content (recommendations)
Result: 30% CPU reduction, faster cache refresh
&quot;&quot;&quot;
def __init__(self, redis_client: redis.Redis):
self.cache = redis_client
def cache_movie_metadata(self, movie_id: int, metadata: dict):
&quot;&quot;&quot;Time-based: Movie metadata changes rarely&quot;&quot;&quot;
ttl = 86400 # 24 hours
self.cache.setex(f&quot;movie:{movie_id}&quot;, ttl, json.dumps(metadata))
# Tag for bulk invalidation
self.cache.sadd(&quot;tag:movie_metadata&quot;, f&quot;movie:{movie_id}&quot;)
def cache_user_recommendations(self, user_id: int, recommendations: list):
&quot;&quot;&quot;Event-based: Invalidate on user actions&quot;&quot;&quot;
ttl = 3600 # 1 hour
key = f&quot;recs:{user_id}&quot;
self.cache.setex(key, ttl, json.dumps(recommendations))
# Watch for user events to invalidate
self.cache.setex(f&quot;recs_invalidate:{user_id}&quot;, ttl, &quot;pending&quot;)
def on_user_watch_event(self, user_id: int, movie_id: int):
&quot;&quot;&quot;Event: User watched movie -&gt; invalidate recommendations&quot;&quot;&quot;
self.cache.delete(f&quot;recs:{user_id}&quot;)
print(f&quot;Event invalidation: User {user_id} recs after watching {movie_id}&quot;)
def invalidate_movie_category(self, category: str):
&quot;&quot;&quot;Key-based: Invalidate all movies in category&quot;&quot;&quot;
keys = self.cache.smembers(f&quot;tag:category:{category}&quot;)
if keys:
self.cache.delete(*keys)
print(f&quot;Bulk invalidation: {len(keys)} movies in {category}&quot;)</code></pre>
<h2>Cache Warming After Invalidation</h2>
<p><strong>Prevent thundering herd</strong> - proactively populate cache after invalidation.</p>
<pre><code class="lang-python">import asyncio
import aiohttp
class CacheWarming:
def __init__(self, redis_client: redis.Redis):
self.cache = redis_client
async def warm_cache(self, keys_to_warm: list[str], fetch_fn):
&quot;&quot;&quot;Asynchronously warm cache after invalidation&quot;&quot;&quot;
tasks = [self._warm_single(key, fetch_fn) for key in keys_to_warm]
await asyncio.gather(*tasks)
print(f&quot;Warmed {len(keys_to_warm)} cache entries&quot;)
async def _warm_single(self, key: str, fetch_fn):
&quot;&quot;&quot;Warm single cache entry&quot;&quot;&quot;
data = await fetch_fn(key)
if data:
self.cache.setex(key, 3600, json.dumps(data))
# Usage: Warm popular items after deployment
async def fetch_product(product_id: str):
# Fetch from database
return {&quot;id&quot;: product_id, &quot;name&quot;: f&quot;Product {product_id}&quot;}
warmer = CacheWarming(redis_client)
popular_products = [&quot;product:1&quot;, &quot;product:2&quot;, &quot;product:3&quot;]
await warmer.warm_cache(popular_products, fetch_product)</code></pre>
<h2>Graceful Degradation</h2>
<p><strong>Handle invalidation failures</strong> - serve stale data if necessary.</p>
<pre><code class="lang-python">class GracefulCacheInvalidation:
def __init__(self, redis_client: redis.Redis):
self.cache = redis_client
def get_with_stale_fallback(self, key: str, fetch_fn, ttl: int = 300):
&quot;&quot;&quot;Serve stale data if refresh fails&quot;&quot;&quot;
cached = self.cache.get(key)
ttl_remaining = self.cache.ttl(key)
# Fresh cache hit
if cached and ttl_remaining &gt; 0:
return json.loads(cached)
# Try refresh
try:
fresh_data = fetch_fn()
self.cache.setex(key, ttl, json.dumps(fresh_data))
return fresh_data
except Exception as e:
print(f&quot;Fetch failed: {e}&quot;)
# Serve stale data if available
if cached:
print(f&quot;Serving stale data for {key}&quot;)
# Extend TTL briefly while fixing issue
self.cache.expire(key, 60)
return json.loads(cached)
raise # No stale data available, propagate error</code></pre>
<h2>Anti-Patterns</h2>
<h3>❌ Invalidating Too Aggressively</h3>
<pre><code class="lang-python"># WRONG: Invalidate on every write
def update_user(user_id: int, data: dict):
save_to_db(user_id, data)
cache.delete(f&quot;user:{user_id}&quot;)
cache.delete(f&quot;user:{user_id}:*&quot;) # Overly aggressive
# Next read causes cache miss
# CORRECT: Update cache instead of deleting
def update_user(user_id: int, data: dict):
save_to_db(user_id, data)
cache.setex(f&quot;user:{user_id}&quot;, 3600, json.dumps(data))</code></pre>
<h3>❌ No Invalidation Strategy</h3>
<pre><code class="lang-python"># WRONG: Set cache, never invalidate
cache.set(&quot;config&quot;, config_data) # Stale data forever
# CORRECT: Use appropriate TTL or event-based invalidation
cache.setex(&quot;config&quot;, 300, json.dumps(config_data)) # 5 min TTL</code></pre>
<h3>❌ Invalidating Without Warming</h3>
<pre><code class="lang-python"># WRONG: Invalidate during traffic spike
cache.delete(&quot;popular_product:*&quot;) # Thundering herd to database
# CORRECT: Warm cache before invalidating
warm_cache(popular_products)
cache.delete(&quot;popular_product:*&quot;)</code></pre>
<h2>Quick Reference</h2>
<p><strong>Invalidation Strategy Selection</strong>:</p>
<pre><code>Static content → Time-based (long TTL)
User-specific → Sliding expiration
Real-time updates → Event-based (pub/sub)
Related data → Key-based (tags)
Immutable data → Version-based (no invalidation)</code></pre>
<p><strong>Best Practices</strong>:</p>
<ul><li>Use TTL as a safety net (always set expiration)</li><li>Combine strategies (Netflix: time + event + key-based)</li><li>Warm cache after invalidation</li><li>Monitor cache hit rate to tune invalidation</li><li>Plan for graceful degradation</li></ul>
<p><strong>Common TTL Values</strong>:</p>
<pre><code>Static assets: 1 year (31536000s) + fingerprinting
API responses: 5-30 minutes (300-1800s)
User sessions: 30 minutes sliding
Database queries: 1-10 minutes (60-600s)
Real-time data: 10-60 seconds</code></pre>
<h2>Related Skills</h2>
<ul><li><code>caching-fundamentals.md</code> - Core caching patterns</li><li><code>redis-caching-patterns.md</code> - Redis-specific implementations</li><li><code>http-caching.md</code> - Browser cache invalidation (ETag, Last-Modified)</li><li><code>cdn-edge-caching.md</code> - CDN cache purging strategies</li><li><code>cache-performance-monitoring.md</code> - Measuring invalidation effectiveness</li></ul>
<h2>Summary</h2>
<p>Cache invalidation is one of the hardest problems in computer science, but following proven strategies makes it manageable:</p>
<p><strong>Key Takeaways</strong>:</p>
<ol><li><strong>There&#x27;s no one-size-fits-all</strong> - Combine multiple strategies (Netflix approach)</li><li><strong>Time-based is simplest</strong> - Use TTL as default, add sophistication as needed</li><li><strong>Event-based is most accurate</strong> - But requires infrastructure (pub/sub, CDC)</li><li><strong>Version-based is most elegant</strong> - Treat data as immutable when possible</li><li><strong>Always warm after invalidation</strong> - Prevent thundering herd</li><li><strong>Monitor invalidation effectiveness</strong> - Track cache hit rate, stale data incidents</li></ol>
<p>The goal is balancing freshness with performance. Start simple (TTL), add event-based invalidation for critical paths, and use versioning for static assets.</p>
<footer>
<div class="fmap">
<div><b>Build</b>
<a href="https://hanzo.app">hanzo.app</a> app builder
<a href="https://hanzo.codes">hanzo.codes</a> code with an agent
<a href="https://hanzoskills.com">hanzoskills.com</a> what an agent reads</div>
<div><b>Run</b>
<a href="https://console.hanzo.ai">console.hanzo.ai</a> cloud console
<a href="https://api.hanzo.ai">api.hanzo.ai</a> the API</div>
<div><b>Account</b>
<a href="https://hanzo.id">hanzo.id</a> sign in
<a href="https://pay.hanzo.ai">pay.hanzo.ai</a> top up
<a href="https://billing.hanzo.ai">billing.hanzo.ai</a> invoices and usage</div>
<div><b>Company</b>
<a href="https://hanzo.ai">hanzo.ai</a> canonical
<a href="https://hanzo.ventures">hanzo.ventures</a> ventures</div>
</div>
<div class="fend"><a href="/caching-cache-invalidation-strategies.md">This page as plain text</a> · <a href="/llms.txt">Index for agents</a></div>
</footer>
</div></body></html>