Cache invalidation

From InugamiDev/ultrathink-oss · View interactive version

Cache invalidation is an AI agent skill from the GitHub repository InugamiDev/ultrathink-oss. Cache invalidation — TTL, event-driven, cache-aside, write-through, SWR, tag purge

How to use this skill

Skill file

Cache Invalidation

Overview

Cache invalidation is the process of removing or updating stale cached data when the underlying source of truth changes. It is famously one of the two hard problems in computer science. The goal is to balance freshness (serving current data) against performance (avoiding unnecessary origin fetches).

When to Use

Key Patterns

Cache-Aside (Lazy Loading)

Application checks cache first; on miss, reads from DB and populates cache.

async function getProduct(id: string): Promise<Product> {
  const cacheKey = `product:${id}`;
  const cached = await redis.get(cacheKey);
  if (cached) return JSON.parse(cached);

  const product = await db.query('SELECT * FROM products WHERE id = $1', [id]);
  await redis.setex(cacheKey, 3600, JSON.stringify(product)); // 1h TTL
  return product;
}

async function updateProduct(id: string, data: Partial<Product>) {
  await db.query('UPDATE products SET ... WHERE id = $1', [id]);
  await redis.del(`product:${id}`); // invalidate — next read repopulates
}

Write-Through

Write to cache and DB simultaneously. Cache is always fresh but writes are slower.

async function updateProduct(id: string, data: Partial<Product>) {
  const updated = await db.query(
    'UPDATE products SET ... WHERE id = $1 RETURNING *', [id]
  );
  await redis.setex(`product:${id}`, 3600, JSON.stringify(updated));
  return updated;
}

Event-Driven Invalidation

Decouple invalidation from the write path using events.

// Publisher (in the write service)
await db.query('UPDATE products SET price = $1 WHERE id = $2', [newPrice, id]);
await eventBus.publish('product.updated', { id, fields: ['price'] });

// Subscriber (cache invalidation worker)
eventBus.subscribe('product.updated', async (event) => {
  await redis.del(`product:${event.id}`);
  await cdn.purgeTag(`product-${event.id}`);
});

Tag-Based Purging (CDN / Vercel)

// Set tags when caching
// Next.js revalidateTag example
import { revalidateTag } from 'next/cache';

// In a fetch call
fetch('https://api.example.com/products', {
  next: { tags: ['products', `category-${categoryId}`] },
});

// On mutation — purge all caches with this tag
export async function updateCategory(id: string) {
  await db.updateCategory(id);
  revalidateTag(`category-${id}`);
}

Stale-While-Revalidate (SWR)

Serve stale data immediately while refreshing in the background.

// HTTP header approach
// Cache-Control: public, max-age=60, stale-while-revalidate=300

// Application-level SWR
async function getWithSWR<T>(key: string, fetcher: () => Promise<T>, ttl: number, swrWindow: number) {
  const entry = await redis.hgetall(`swr:${key}`);

  if (entry.data) {
    const age = Date.now() - Number(entry.timestamp);
    if (age < ttl * 1000) return JSON.parse(entry.data); // fresh

    if (age < (ttl + swrWindow) * 1000) {
      // stale but within SWR window — return stale, refresh async
      refreshInBackground(key, fetcher, ttl);
      return JSON.parse(entry.data);
    }
  }

  // Cache miss or expired beyond SWR window
  const fresh = await fetcher();
  await redis.hset(`swr:${key}`, { data: JSON.stringify(fresh), timestamp: Date.now() });
  return fresh;
}

Pitfalls

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.