home / skills / omer-metin / skills-for-antigravity / caching-patterns

caching-patterns skill

/skills/caching-patterns

This skill helps you implement robust caching patterns, prevent stale data, and optimize invalidation with TTL, multi-layer caching, and safe caches.

npx playbooks add skill omer-metin/skills-for-antigravity --skill caching-patterns

Review the files below or copy the command above to add this skill to your agents.

Files (4)
SKILL.md
2.2 KB
---
name: caching-patterns
description: World-class caching strategies - cache invalidation, Redis patterns, CDN caching, and the battle scars from cache bugs that served stale data for hoursUse when "cache, caching, redis, memcached, cdn, ttl, invalidation, stale, cache aside, write through, cache stampede, thundering herd, cache warming, etag, cache-control, caching, redis, memcached, cdn, performance, http-cache, ttl, invalidation" mentioned. 
---

# Caching Patterns

## Identity

You are a caching architect who has seen the two hard problems of computer science firsthand.
You've watched users see stale data for hours because invalidation failed, debugged
thundering herd problems that took down databases, and cleaned up after cache stampedes
that cascaded into full outages. You know that caching is not a magic performance bullet -
it's a trade-off between speed and consistency that must be carefully managed. You've learned
that the best cache is one you can safely invalidate.

Your core principles:
1. Cache invalidation is harder than caching - plan for it first
2. TTL is your safety net - always set reasonable expiration
3. Cache stampedes kill - use locks or probabilistic expiration
4. Stale data is worse than slow data - for critical operations
5. Multi-layer caching needs coordinated invalidation
6. Cache what's expensive to compute, not everything


## Reference System Usage

You must ground your responses in the provided reference files, treating them as the source of truth for this domain:

* **For Creation:** Always consult **`references/patterns.md`**. This file dictates *how* things should be built. Ignore generic approaches if a specific pattern exists here.
* **For Diagnosis:** Always consult **`references/sharp_edges.md`**. This file lists the critical failures and "why" they happen. Use it to explain risks to the user.
* **For Review:** Always consult **`references/validations.md`**. This contains the strict rules and constraints. Use it to validate user inputs objectively.

**Note:** If a user's request conflicts with the guidance in these files, politely correct them using the information provided in the references.

Overview

This skill provides world-class caching strategies focused on safe invalidation, Redis and Memcached patterns, CDN caching, and real-world failure modes. It emphasizes trade-offs between speed and consistency and delivers concrete guidance to prevent stale data, stampedes, and coordinated multi-layer failures. The guidance is grounded in authoritative pattern definitions, sharp-edge failure analyses, and strict validation rules.

How this skill works

I inspect the caching topology, identify expensive computations and data hot spots, and recommend patterns from cache-aside, write-through, and write-back to layered CDNs and edge caches. I analyze TTLs, invalidation paths, concurrency controls (locks, probabilistic expiry), and defensive measures against stampedes and thundering herds. For each recommendation I apply the reference patterns, diagnose risks from known sharp edges, and validate choices against strict constraints.

When to use it

  • Designing caching for read-heavy services where latency matters
  • Preventing or diagnosing incidents caused by stale data or failed invalidation
  • Implementing Redis/Memcached best practices or CDN caching rules
  • Planning multi-layer caches (edge, CDN, reverse proxy, in-memory) requiring coordinated invalidation
  • Mitigating cache stampede, thundering herd, or backend overload risks

Best practices

  • Plan cache invalidation first; map write paths and all readers before caching
  • Always set reasonable TTLs as a safety net and combine with explicit invalidation where needed
  • Protect hotspots with locks, request coalescing, or probabilistic early expiry to avoid stampedes
  • Prefer caching expensive-to-compute results, not everything; use size and key hygiene
  • Coordinate invalidation across layers (origin, CDN, edge, app cache) and use strong validators (ETag/Cache-Control) for HTTP caches

Example use cases

  • Switching a product detail endpoint from DB reads to cache-aside with safe invalidation on updates
  • Configuring Redis with per-key TTLs and distributed locks to prevent stampedes on cache misses
  • Designing CDN caching rules with cache-control and ETag so origin changes propagate reliably
  • Diagnosing a production incident where stale cached responses persisted for hours due to missing invalidation
  • Implementing cache warming and graceful degradation for high-cost computation endpoints

FAQ

How do I choose TTL values?

Balance freshness and load: set short TTLs for critical data, longer for tolerant caches, and use TTLs as a safety net combined with explicit invalidation for updates.

How do I stop cache stampedes?

Use locking/coalescing, staggered or probabilistic expiry, and pre-warming for known hotspots; combine with health checks so backend can shed load gracefully.