Get the FREE Ultimate OpenClaw Setup Guide →
w

caching

Verified

@wpank

npx machina-cli add skill @wpank/caching --openclaw
Files (1)
SKILL.md
7.7 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

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://clawhub.ai/wpank/cachingView on GitHub

Overview

This skill covers caching strategies such as cache-aside, read-through, write-through, write-behind, and refresh-ahead, plus invalidation techniques, eviction policies, and HTTP caching rules. It helps you design robust cache layers, choose the right eviction and TTLs, and avoid common anti-patterns while boosting read-heavy workloads.

How This Skill Works

Cache layers sit between your app and data stores, using a chosen pattern to manage reads and writes. For example, in a Cache-Aside flow the app checks cache, reads the DB on a miss, and stores the result back in the cache. TTLs, invalidation methods, and HTTP directives then govern data freshness and distribution across processes.

When to Use It

  • Designing a cache layer for a read-heavy service to reduce database load
  • Choosing and tuning an eviction policy for limited-memory services
  • Debugging stale data or data drift in caches
  • Implementing distributed caching with Redis or Memcached
  • Applying HTTP caching rules to APIs and static assets

Quick Start

  1. Step 1: Inventory data by access patterns and choose a caching strategy (start with Cache-Aside).
  2. Step 2: Implement TTL or event-based invalidation and set HTTP caching as needed.
  3. Step 3: Instrument metrics (hit rate, latency, evictions) and tune accordingly.

Best Practices

  • Start with Cache-Aside as the default pattern and measure gains before layering in others
  • Align TTLs and invalidation strategies with data freshness requirements
  • Use appropriate HTTP caching directives (Cache-Control, ETag, Last-Modified) to enable CDN and browser caching
  • Monitor cache hit rate, eviction count, and stale reads; watch for cold starts and stampedes
  • Avoid caching sensitive data or tokens; segment caches and enforce access controls

Example Use Cases

  • Product catalog cached with short TTL to balance freshness and load
  • Static assets served with long TTL and hashed filenames for immutability
  • API responses cached with s-maxage and stale-while-revalidate for CDN freshness
  • User session data stored in Redis with defined TTL and isolation
  • Cache-Aside flow demonstrated in a web app: miss -> DB read -> cache write

Frequently Asked Questions

Add this skill to your agents
Sponsor this space

Reach thousands of developers