Design and implement caching layers using Redis, Memcached, or in- memory caches with proper invalidation strategies.
"The fastest query is the one you never make."
Invoked when read-heavy paths need optimization or when database load must be reduced without architectural changes.
Steps:
Steps:
Choose the appropriate caching pattern:
| Pattern | Use When |
|---|---|
| Cache-Aside | App reads cache first, fills on miss. |
| Write-Through | App writes to cache and DB simultaneously. |
| Write-Behind | App writes to cache; async flush to DB. |
| Read-Through | Cache itself fetches from DB on miss. |
Default recommendation: Cache-Aside (simplest, most predictable).
Steps:
{service}:{entity}:{id} or {service}:{query-hash}.Steps:
Steps:
Design a layered caching architecture where each layer trades latency for capacity:
| Layer | Technology | Latency | Capacity | Scope |
|---|---|---|---|---|
| L1 | In-process (LRU) | <1ms | Small (bounded by heap) | Per-instance |
| L2 | Shared distributed (Redis/Memcached) | 1-5ms | Medium (cluster memory) | Cross-instance |
| L3 | Edge/CDN | 5-50ms | Large (global) | Public content only |
Read path: check L1 → miss? check L2 → miss? check L3 → miss? query origin. Nano: Cache Promotion — on L2 or L3 hit, promote the entry to L1 for faster subsequent access.
Write/invalidation path: Nano: Cross-Layer Invalidation — a write invalidates L1 immediately (local), then L2 (broadcast/pub-sub), then L3 (purge API). Invalidation cascades top-down.
Decision criteria for layer selection:
| Need | Recommended layers |
|---|---|
| Ultra-low latency, single instance | L1 only |
| Shared across instances, moderate latency | L1 + L2 |
| Public content, global distribution | L1 + L2 + L3 |
| Cost-sensitive, moderate traffic | L2 only (skip L1 overhead) |
Monitor per-layer hit rates. If L1 hit rate < 50%, the L1 cache is too small or the workload isn't suitable for in-process caching.
| Field | Type | Description |
|---|---|---|
cache_config | string | Cache client configuration |
implementation | string[] | Modified files with caching added |
key_schema | string | Cache key naming documentation |
metrics | string | Cache-hit/miss metric implementation |
database-design or db-tuning).{entity}? (e.g., 30s, 5m, 1h)"| Situation | Action |
|---|---|
| Read:write ratio > 10:1 and staleness tolerance ≥ 30s | Use Cache-Aside with TTL |
| Reads require strong consistency with DB | Use Read-Through with short TTL (≤ 5s) or skip caching |
| Writes are frequent but reads must reflect latest state | Use Write-Through to keep cache warm |
| Write latency is critical and eventual consistency is acceptable | Use Write-Behind with async flush |
| Multiple related keys must invalidate together | Use Tag-based invalidation |
| Single hot key with high concurrency on cache miss | Apply singleflight / lock to prevent stampede |
| Data is user-specific and varies per tenant | Segment keys with {tenant}:{entity}:{id} prefix |
| No external cache infra available | Use in-memory cache (LRU) with size cap |
| Failure | Symptom | Mitigation |
|---|---|---|
| Cache stampede (thundering herd) | Spike in DB load when a popular key expires | Implement singleflight / mutex lock on cache miss; stagger TTLs with jitter |
| Stale data served after mutation | Users see outdated values after an update | Add event-based invalidation on write; verify invalidation triggers in tests |
| Memory exhaustion | Cache node OOM, evictions spike, latency increases | Set maxmemory policy (e.g., allkeys-lru); enforce TTL on all keys |
| Cache poisoning | Incorrect or corrupt data persisted in cache | Validate data before caching; add integrity checks on deserialization |
| Silent cache bypass | Cache client fails silently, all requests hit DB | Add health checks and alerting on cache connectivity; log cache errors |
| Key collision | Different data stored/retrieved under the same key | Use deterministic, namespaced key schema; include version in key prefix |
| Serialization mismatch | Deserialization errors after code deployment changes object shape | Version cache keys or add schema version suffix; flush on deploy if needed |
Each invocation of the Caching Strategy skill records the following timestamped entries in the scratchpad:
[YYYY-MM-DDTHH:MM:SSZ] CACHE_SKILL_START — Skill invoked; target paths and context noted.[YYYY-MM-DDTHH:MM:SSZ] CANDIDATES_IDENTIFIED — List of cacheable query/endpoint candidates with read frequency and latency.[YYYY-MM-DDTHH:MM:SSZ] PATTERN_SELECTED — Chosen caching pattern per candidate with rationale.[YYYY-MM-DDTHH:MM:SSZ] IMPLEMENTATION_COMPLETE — Files modified, key schema applied, TTLs configured.[YYYY-MM-DDTHH:MM:SSZ] INVALIDATION_CONFIGURED — Invalidation strategy and triggers documented.[YYYY-MM-DDTHH:MM:SSZ] METRICS_ADDED — Cache-hit/miss instrumentation added; metric names listed.[YYYY-MM-DDTHH:MM:SSZ] TESTS_PASSED — Existing test suite executed post-change; pass/fail status.[YYYY-MM-DDTHH:MM:SSZ] CACHE_SKILL_END — Skill completed; summary of cache-hit ratio improvement (if measurable).