Get the FREE Ultimate OpenClaw Setup Guide →

caching

npx machina-cli add skill wpank/ai/caching --openclaw
Files (1)
SKILL.md
7.8 KB

Caching Patterns

A well-placed cache is the cheapest way to buy speed. A misplaced cache is the most expensive way to buy bugs.

Cache Strategies

StrategyHow It WorksWhen to Use
Cache-Aside (Lazy)App checks cache → miss → reads DB → writes to cacheDefault choice — general purpose
Read-ThroughCache fetches from DB on miss automaticallyORM-integrated caching, CDN origin fetch
Write-ThroughWrites go to cache AND DB synchronouslyRead-heavy with strong consistency
Write-BehindWrites go to cache, async flush to DBHigh write throughput, eventual consistency OK
Refresh-AheadCache proactively refreshes before expiryPredictable access patterns, low-latency critical
Cache-Aside Flow:

  App ──► Cache ──► HIT? ──► Return data
              │
              ▼ MISS
          Read DB ──► Store in Cache ──► Return data

Installation

OpenClaw / Moltbot / Clawbot

npx clawhub@latest install caching

Cache Invalidation

MethodConsistencyWhen to Use
TTL-basedEventual (up to TTL)Simple data, acceptable staleness
Event-basedStrong (near real-time)Inventory, profile updates
Version-basedStrongStatic assets, API responses, config
Tag-basedStrongCMS content, category-based purging

TTL Guidelines

Data TypeTTLRationale
Static assets (CSS/JS/images)1 year + cache-busting hashImmutable by filename
API config / feature flags30–60 secondsFast propagation needed
User profile data5–15 minutesTolerable staleness
Product catalog1–5 minutesBalance freshness vs load
Session dataMatch session timeoutSecurity requirement

HTTP Caching

Cache-Control Directives

DirectiveMeaning
max-age=NCache for N seconds
s-maxage=NCDN/shared cache max age (overrides max-age)
no-cacheMust revalidate before using cached copy
no-storeNever cache anywhere
must-revalidateOnce stale, must revalidate
privateOnly browser can cache, not CDN
publicAny cache can store
immutableContent will never change (within max-age)
stale-while-revalidate=NServe stale for N seconds while fetching fresh

Common Recipes

# Immutable static assets (hashed filenames)
Cache-Control: public, max-age=31536000, immutable

# API response, CDN-cached, background refresh
Cache-Control: public, s-maxage=60, stale-while-revalidate=300

# Personalized data, browser-only
Cache-Control: private, max-age=0, must-revalidate
ETag: "abc123"

# Never cache (auth tokens, sensitive data)
Cache-Control: no-store

Conditional Requests

MechanismRequest HeaderResponse HeaderHow It Works
ETagIf-None-Match: "abc"ETag: "abc"Hash-based — 304 if match
Last-ModifiedIf-Modified-Since: <date>Last-Modified: <date>Date-based — 304 if unchanged

Prefer ETag over Last-Modified — ETags detect content changes regardless of timestamp granularity.


Application Caching

SolutionSpeedShared Across ProcessesWhen to Use
In-memory LRUFastestNoSingle-process, bounded memory, hot data
RedisSub-ms (network)YesProduction default — TTL, pub/sub, persistence
MemcachedSub-ms (network)YesSimple key-value at extreme scale
SQLiteFast (disk)NoEmbedded apps, edge caching

Redis vs Memcached

FeatureRedisMemcached
Data structuresStrings, hashes, lists, sets, sorted setsStrings only
PersistenceAOF, RDB snapshotsNone
Pub/SubYesNo
Max value size512 MB1 MB
VerdictDefault choicePure cache at extreme scale

Distributed Caching

ConcernSolution
PartitioningConsistent hashing — minimal reshuffling on node changes
ReplicationPrimary-replica — writes to primary, reads from replicas
FailoverRedis Sentinel or Cluster auto-failover

Rule of thumb: 3 primaries + 3 replicas minimum for production Redis Cluster.


Cache Eviction Policies

PolicyHow It WorksWhen to Use
LRUEvicts least recently accessedDefault — general purpose
LFUEvicts least frequently accessedSkewed popularity distributions
FIFOEvicts oldest entrySimple, time-ordered data
TTLEvicts after fixed durationData with known freshness window

Redis default is noeviction. Set maxmemory-policy to allkeys-lru or volatile-lru for production.


Caching Layers

Browser Cache → CDN → Load Balancer → App Cache → DB Cache → Database
LayerWhat to CacheInvalidation
BrowserStatic assets, API responsesVersioned URLs, Cache-Control
CDNStatic files, public API responsesPurge API, surrogate keys
ApplicationComputed results, DB queries, external APIEvent-driven, TTL
DatabaseQuery plans, buffer pool, materialized viewsANALYZE, manual refresh

Cache Stampede Prevention

When a hot key expires, hundreds of requests simultaneously hit the database.

TechniqueHow It Works
Mutex / LockFirst request locks, fetches, populates; others wait
Probabilistic early expirationRandom chance of refreshing before TTL
Request coalescingDeduplicate in-flight requests for same key
Stale-while-revalidateServe stale, refresh asynchronously

Cache Warming

StrategyWhen to Use
On-deploy warm-upPredictable key set, latency-sensitive
Background jobReports, dashboards, catalog data
Shadow trafficCache migration, new infrastructure
Priority-basedLimited warm-up time budget

Cold start impact: A full cache flush can increase DB load 10–100x. Always warm gradually or use stale-while-revalidate.


Monitoring

MetricHealthy RangeAction if Unhealthy
Hit rate> 90%Low → cache too small, wrong TTL, bad key design
Eviction rateNear 0 steady stateHigh → increase memory or tune policy
Latency (p99)< 1ms (Redis)High → network issue, large values, hot key
Memory usage< 80% of maxApproaching max → scale up or tune eviction

NEVER Do

  1. NEVER cache without a TTL or invalidation plan — data rots; every entry needs an expiry path
  2. NEVER treat cache as durable storage — caches evict, crash, and restart; always fall back to source of truth
  3. NEVER cache sensitive data (tokens, PII) without encryption — cache breaches expose everything in plaintext
  4. NEVER ignore cache stampede on hot keys — one expired popular key can take down your database
  5. NEVER use unbounded in-memory caches in production — memory grows until OOM-killed
  6. NEVER cache mutable data with immutable Cache-Control — browsers will never re-fetch
  7. NEVER skip monitoring hit/miss rates — you won't know if your cache is helping or hurting

Source

git clone https://github.com/wpank/ai/blob/main/skills/api/caching/SKILL.mdView on GitHub

Overview

Caching patterns cover strategies such as Cache-Aside, Read-Through, Write-Through, Write-Behind, and Refresh-Ahead. It also maps invalidation approaches (TTL-based, event-based, version-based, tag-based), HTTP caching, and distributed caches. This matters for read-heavy workloads, performance, and data freshness.

How This Skill Works

A cache sits between the application and the data store. On a request, the app checks the cache; on a miss it fetches from the DB and writes back to the cache. Invalidation rules, TTLs, and the choice of strategy (Cache-Aside, Write-Through, etc.) control consistency. HTTP caching uses Cache-Control directives and conditional requests to reuse responses, while distributed caches enable sharing across processes.

When to Use It

  • Designing a read-heavy API and selecting an appropriate cache pattern.
  • Choosing an eviction policy for a high-traffic service to balance latency and freshness.
  • Debugging stale data across user sessions or dashboards.
  • Implementing HTTP caching for APIs and static assets with Cache-Control and ETag.
  • Building a distributed cache layer with Redis or Memcached for cross-process access.

Quick Start

  1. Step 1: Identify hot read paths and select a caching pattern (start with Cache-Aside).
  2. Step 2: Implement an invalidation strategy (TTL, event-based) and wire in a cache store (Redis or Memcached).
  3. Step 3: Instrument metrics (hit rate, latency, evictions) and iterate on TTLs and eviction policies.

Best Practices

  • Start with Cache-Aside as the default pattern for general purpose caching.
  • Tune TTLs and select invalidation methods (TTL, event-based, version-based, tag-based) to balance freshness and load.
  • Design clear cache keys and namespaces to avoid collisions and accidental data leakage.
  • Choose Write-Through for strong consistency or Write-Behind for higher throughput when eventual consistency is acceptable.
  • Instrument cache metrics (hit rate, latency, evictions) and write tests to validate correctness under failure.

Example Use Cases

  • E-commerce product catalog cached with short TTLs (1–5 minutes) to reduce DB load while keeping data fresh.
  • User session data cached for the duration of the session to speed up authentication and personalization.
  • API responses cached with Cache-Control headers for CDN reuse and reduced origin fetches.
  • Inventory updates invalidated via event-based invalidation to maintain strong near-real-time accuracy.
  • Redis-based distributed cache used to share hot data across multiple application processes with TTL and pub/sub.

Frequently Asked Questions

Add this skill to your agents
Sponsor this space

Reach thousands of developers