mirror of
https://github.com/hanzoai/skills.git
synced 2026-11-19 00:47:36 +00:00
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]>
713 lines
36 KiB
HTML
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>: "There are only two hard things in Computer Science: cache invalidation and naming things." - 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) -> None:
|
|
"""Set cache entry with fixed TTL"""
|
|
self.cache.setex(key, ttl_seconds, json.dumps(value))
|
|
print(f"Cached {key} with TTL={ttl_seconds}s")
|
|
|
|
def get(self, key: str) -> dict:
|
|
"""Get cached value (auto-expires after TTL)"""
|
|
cached = self.cache.get(key)
|
|
if cached:
|
|
ttl_remaining = self.cache.ttl(key)
|
|
print(f"Cache hit: {key} (expires in {ttl_remaining}s)")
|
|
return json.loads(cached)
|
|
print(f"Cache miss: {key}")
|
|
return None
|
|
|
|
# Usage: Different TTLs for different data types
|
|
cache = FixedTTLCache(redis_client)
|
|
cache.set_with_ttl("user:123", user_data, 3600) # 1 hour (semi-static)
|
|
cache.set_with_ttl("product:456", product_data, 300) # 5 minutes (dynamic)
|
|
cache.set_with_ttl("homepage", 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) -> None:
|
|
"""Set cache to expire at specific timestamp"""
|
|
self.cache.set(key, json.dumps(value))
|
|
self.cache.expireat(key, int(expire_at.timestamp()))
|
|
print(f"Cached {key}, expires at {expire_at.isoformat()}")
|
|
|
|
# Usage: Flash sale ending at specific time
|
|
sale_end = datetime(2025, 10, 25, 23, 59, 59, tzinfo=timezone.utc)
|
|
cache.set_expire_at("flash_sale:electronics", 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) -> dict:
|
|
"""Get value and reset TTL (sliding window)"""
|
|
cached = self.cache.get(key)
|
|
if cached:
|
|
# Reset expiration on access
|
|
self.cache.expire(key, self.ttl)
|
|
print(f"Cache hit: {key}, TTL reset to {self.ttl}s")
|
|
return json.loads(cached)
|
|
return None
|
|
|
|
def set_sliding(self, key: str, value: dict) -> 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):
|
|
"""Listen for invalidation events"""
|
|
self.pubsub.subscribe('cache:invalidate')
|
|
thread = threading.Thread(target=self._listen)
|
|
thread.daemon = True
|
|
thread.start()
|
|
|
|
def _listen(self):
|
|
"""Background thread processing invalidation events"""
|
|
for message in self.pubsub.listen():
|
|
if message['type'] == 'message':
|
|
key_pattern = message['data'].decode('utf-8')
|
|
self._invalidate_pattern(key_pattern)
|
|
|
|
def _invalidate_pattern(self, pattern: str):
|
|
"""Invalidate all keys matching pattern"""
|
|
keys = self.cache.keys(pattern)
|
|
if keys:
|
|
self.cache.delete(*keys)
|
|
print(f"Invalidated {len(keys)} keys matching {pattern}")
|
|
|
|
def publish_invalidation(self, key_pattern: str):
|
|
"""Publish invalidation event"""
|
|
self.cache.publish('cache:invalidate', key_pattern)
|
|
print(f"Published invalidation: {key_pattern}")
|
|
|
|
# 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"user:{user_id}:*")</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):
|
|
"""Process CDC events from Kafka"""
|
|
for message in self.consumer:
|
|
event = json.loads(message.value)
|
|
|
|
if event['operation'] in ['UPDATE', 'DELETE']:
|
|
# Extract entity ID from CDC event
|
|
entity_type = event['table']
|
|
entity_id = event['after']['id'] if event['operation'] == 'UPDATE' else event['before']['id']
|
|
|
|
# Invalidate related cache entries
|
|
self._invalidate_entity(entity_type, entity_id)
|
|
|
|
def _invalidate_entity(self, entity_type: str, entity_id: int):
|
|
"""Invalidate cache for specific entity"""
|
|
pattern = f"{entity_type}:{entity_id}:*"
|
|
keys = self.cache.keys(pattern)
|
|
if keys:
|
|
self.cache.delete(*keys)
|
|
print(f"CDC invalidation: {entity_type}:{entity_id}")
|
|
|
|
# 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):
|
|
"""Store cache entry with associated tags"""
|
|
# Store the value
|
|
self.cache.setex(key, ttl, json.dumps(value))
|
|
|
|
# Associate key with each tag
|
|
for tag in tags:
|
|
tag_set = f"tag:{tag}"
|
|
self.cache.sadd(tag_set, key)
|
|
self.cache.expire(tag_set, ttl + 300) # Tag outlives cache entry
|
|
|
|
print(f"Cached {key} with tags: {tags}")
|
|
|
|
def invalidate_by_tag(self, tag: str):
|
|
"""Invalidate all cache entries with specific tag"""
|
|
tag_set = f"tag:{tag}"
|
|
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"Invalidated {len(keys)} entries with tag '{tag}'")
|
|
|
|
# Usage: E-commerce product cache
|
|
tagger = CacheTagInvalidation(redis_client)
|
|
|
|
product_data = {"id": 123, "name": "Laptop", "category_id": 5, "brand_id": 10}
|
|
tagger.set_with_tags(
|
|
key="product:123",
|
|
value=product_data,
|
|
tags=["product", "category:5", "brand:10"],
|
|
ttl=3600
|
|
)
|
|
|
|
# When category 5 updates, invalidate all products in that category
|
|
tagger.invalidate_by_tag("category:5")</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):
|
|
"""Store cache with dependency tracking"""
|
|
self.cache.setex(key, ttl, json.dumps(value))
|
|
|
|
# Store dependency graph
|
|
dep_key = f"deps:{key}"
|
|
self.cache.sadd(dep_key, *dependencies)
|
|
self.cache.expire(dep_key, ttl)
|
|
|
|
def invalidate_cascade(self, key: str):
|
|
"""Recursively invalidate key and all dependents"""
|
|
# 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"deps:{dependent}")
|
|
|
|
# Invalidate the key itself
|
|
self.cache.delete(key)
|
|
self.cache.delete(f"deps:{key}")
|
|
|
|
print(f"Cascade invalidation: {key} + {len(dependents)} dependents")
|
|
|
|
def _find_dependents(self, key: str) -> list[str]:
|
|
"""Find all keys that depend on given key"""
|
|
dependents = []
|
|
# Scan all dependency sets (simplified - use index in production)
|
|
for dep_key in self.cache.scan_iter("deps:*"):
|
|
deps = self.cache.smembers(dep_key)
|
|
if key.encode() in deps:
|
|
dependent_key = dep_key.decode().replace("deps:", "")
|
|
dependents.append(dependent_key)
|
|
return dependents
|
|
|
|
# Usage: User profile affects multiple derived caches
|
|
graph = DependencyGraphInvalidation(redis_client)
|
|
|
|
graph.set_with_dependencies("user:123", user_data, dependencies=[], ttl=3600)
|
|
graph.set_with_dependencies("user:123:posts", posts, dependencies=["user:123"], ttl=1800)
|
|
graph.set_with_dependencies("user:123:feed", feed, dependencies=["user:123", "user:123:posts"], ttl=600)
|
|
|
|
# When user updates, cascade invalidation
|
|
graph.invalidate_cascade("user:123") # 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):
|
|
"""Store data with content-based version key"""
|
|
# Generate version hash from content
|
|
content_hash = self._hash_content(value)
|
|
versioned_key = f"{base_key}:v:{content_hash}"
|
|
|
|
# Store with long TTL (immutable data)
|
|
self.cache.setex(versioned_key, ttl, json.dumps(value))
|
|
|
|
# Update pointer to latest version
|
|
self.cache.setex(f"{base_key}:latest", ttl, content_hash)
|
|
|
|
print(f"Stored {versioned_key} (immutable)")
|
|
return content_hash
|
|
|
|
def get_versioned(self, base_key: str, version: str = None) -> dict:
|
|
"""Get specific version or latest"""
|
|
if not version:
|
|
# Get latest version
|
|
version = self.cache.get(f"{base_key}:latest")
|
|
if not version:
|
|
return None
|
|
version = version.decode()
|
|
|
|
versioned_key = f"{base_key}:v:{version}"
|
|
cached = self.cache.get(versioned_key)
|
|
return json.loads(cached) if cached else None
|
|
|
|
def _hash_content(self, value: dict) -> str:
|
|
"""Generate content hash for versioning"""
|
|
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("api:users:list", users_data)
|
|
|
|
# Get latest
|
|
latest = vcache.get_versioned("api:users:list")
|
|
|
|
# Get specific version (for rollback or A/B testing)
|
|
specific = vcache.get_versioned("api:users:list", version="a1b2c3d4e5f6")</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<string, string> = new Map();
|
|
|
|
// Build time: Generate manifest
|
|
generateManifest(files: string[]): void {
|
|
files.forEach(file => {
|
|
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('.').pop();
|
|
const base = file.replace(`.${ext}`, '');
|
|
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 -> app.a1b2c3d4.js (immutable, cache forever)
|
|
const scriptUrl = assets.getAssetUrl('app.js');
|
|
// <script src="/static/app.a1b2c3d4.js"></script>
|
|
// 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:
|
|
"""
|
|
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
|
|
"""
|
|
def __init__(self, redis_client: redis.Redis):
|
|
self.cache = redis_client
|
|
|
|
def cache_movie_metadata(self, movie_id: int, metadata: dict):
|
|
"""Time-based: Movie metadata changes rarely"""
|
|
ttl = 86400 # 24 hours
|
|
self.cache.setex(f"movie:{movie_id}", ttl, json.dumps(metadata))
|
|
|
|
# Tag for bulk invalidation
|
|
self.cache.sadd("tag:movie_metadata", f"movie:{movie_id}")
|
|
|
|
def cache_user_recommendations(self, user_id: int, recommendations: list):
|
|
"""Event-based: Invalidate on user actions"""
|
|
ttl = 3600 # 1 hour
|
|
key = f"recs:{user_id}"
|
|
self.cache.setex(key, ttl, json.dumps(recommendations))
|
|
|
|
# Watch for user events to invalidate
|
|
self.cache.setex(f"recs_invalidate:{user_id}", ttl, "pending")
|
|
|
|
def on_user_watch_event(self, user_id: int, movie_id: int):
|
|
"""Event: User watched movie -> invalidate recommendations"""
|
|
self.cache.delete(f"recs:{user_id}")
|
|
print(f"Event invalidation: User {user_id} recs after watching {movie_id}")
|
|
|
|
def invalidate_movie_category(self, category: str):
|
|
"""Key-based: Invalidate all movies in category"""
|
|
keys = self.cache.smembers(f"tag:category:{category}")
|
|
if keys:
|
|
self.cache.delete(*keys)
|
|
print(f"Bulk invalidation: {len(keys)} movies in {category}")</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):
|
|
"""Asynchronously warm cache after invalidation"""
|
|
tasks = [self._warm_single(key, fetch_fn) for key in keys_to_warm]
|
|
await asyncio.gather(*tasks)
|
|
print(f"Warmed {len(keys_to_warm)} cache entries")
|
|
|
|
async def _warm_single(self, key: str, fetch_fn):
|
|
"""Warm single cache entry"""
|
|
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 {"id": product_id, "name": f"Product {product_id}"}
|
|
|
|
warmer = CacheWarming(redis_client)
|
|
popular_products = ["product:1", "product:2", "product:3"]
|
|
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):
|
|
"""Serve stale data if refresh fails"""
|
|
cached = self.cache.get(key)
|
|
ttl_remaining = self.cache.ttl(key)
|
|
|
|
# Fresh cache hit
|
|
if cached and ttl_remaining > 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"Fetch failed: {e}")
|
|
|
|
# Serve stale data if available
|
|
if cached:
|
|
print(f"Serving stale data for {key}")
|
|
# 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"user:{user_id}")
|
|
cache.delete(f"user:{user_id}:*") # 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"user:{user_id}", 3600, json.dumps(data))</code></pre>
|
|
<h3>❌ No Invalidation Strategy</h3>
|
|
<pre><code class="lang-python"># WRONG: Set cache, never invalidate
|
|
cache.set("config", config_data) # Stale data forever
|
|
|
|
# CORRECT: Use appropriate TTL or event-based invalidation
|
|
cache.setex("config", 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("popular_product:*") # Thundering herd to database
|
|
|
|
# CORRECT: Warm cache before invalidating
|
|
warm_cache(popular_products)
|
|
cache.delete("popular_product:*")</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'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> |