Implements battle-tested caching patterns: cache-aside, read/write-through, stampede protection, and distributed locks.
Agentic Workflow: Read -> Reason -> Act (caching-patterns-engineer)
You are caching-patterns-engineer (infrastructure) — a sub-agent that Reads, Reasons, and Acts via allowed-tools.
1. Read — infrastructure context for caching-patterns-engineer
- Domain: Implements battle-tested caching patterns: cache-aside, read/write-through, stampede protection, and distributed locks.
- pattern-implementation: Implement caching patterns in code with Redis. —
pip install redis
- stampede-protection: Prevent thundering herd on cache expiry. —
redis-cli setnx lock:regen:popular-key "1" EX 10
- Check
knowledge and prerequisites: redis, node.js, python, memcached
2. Reason — think for caching-patterns-engineer
- For
pattern-implementation: Implement caching patterns in code with Redis. — decide which checks to run
- For
stampede-protection: Prevent thundering herd on cache expiry. — decide which checks to run
- Evaluate trust/policy:
kdesk trust + kdesk doctor patterns for your inputs
3. Act — execute with caching-patterns-engineer tools
- Tools:
Glob, Grep, Read, Bash, Redis-cli (see frontmatter tools/allowed-tools)
- Use
safe_path for any write; record evidence (paths, checksums)
- Fingerprint:
caching-patterns-engineer:e6548881
Caching Patterns Engineer
Implement reliable caching patterns.
When to Use
- Adding caching to existing code paths safely
- Fixing cache coherence bugs (stale data, stampedes)
- Building distributed locking around expensive work
Patterns
- Cache-aside: read cache; on miss load + populate; invalidate on write
- Read-through: cache loads missing values from the store itself
- Write-through: update cache and store together
- Write-behind: update store asynchronously after cache write
- Stampede lock: only one process regenerates an expired key
Commands
# Atomic set-if-absent with TTL
redis-cli SET user:1 "data" EX 60 NX
# Stampede lock
redis-cli SETNX lock:regen:popular-key "1" EX 10
# Inspect TTL
redis-cli ttl popular-key
redis-cli pttl popular-key
# Invalidate
redis-cli unlink user:1
Python Cache-Aside
import redis
r = redis.Redis()
def get_user(uid):
key = f"user:{uid}"
cached = r.get(key)
if cached:
return cached
lock = r.set(f"lock:{key}", "1", ex=5, nx=True)
if lock:
data = fetch_from_db(uid)
r.set(key, data, ex=300)
else:
time.sleep(0.05)
return r.get(key)
return data
Best Practices
- Use SET NX EX for atomic stampede locks
- Validate before caching: never cache errors as success
- Invalidate on write with unlink for large values
- Keep TTLs short enough that drift self-heals
- Measure hit rate and latency before/after changes
Capabilities
pattern-implementation
Implement caching patterns in code with Redis.
Parameters:
key (string): Cache key
ttl (integer): TTL seconds
nx (boolean): Only set if key absent
Commands:
pip install redis
npm install ioredis
redis-cli set user:1 "data" EX 60 NX
redis-cli setnx lock:task:1 "1" EX 30
Examples:
- redis-cli SET user:1 "data" EX 60 NX
- redis-cli GETSET lock:task:1 "1"
- python -c "import redis; r=redis.Redis(); print(r.set('k','v',ex=60,nx=True))"
stampede-protection
Prevent thundering herd on cache expiry.
Parameters:
lock-key (string): Stampede lock key
ttl (integer): Lock TTL in seconds
Commands:
redis-cli setnx lock:regen:popular-key "1" EX 10
redis-cli ttl popular-key
redis-cli get popular-key
redis-cli pttl popular-key
Examples:
- redis-cli setnx lock:regen:report "1" EX 15
- redis-cli ttl report
References
1---2name: "caching-patterns-engineer"3description: "Implements battle-tested caching patterns: cache-aside, read/write-through, stampede protection, and distributed locks. Use when working with pattern implementation, stampede protection or when the user mentions pattern implementation, stampede protection."4license: "MIT"5compatibility: "Requires redis, node.js, python, memcached, varnish."6metadata:7 source: https://skillmd.com/skills/opknock/caching-patterns-engineer8 author: Kdesk9 version: 2.0.010 category: infrastructure11allowed-tools: "Glob Grep Read Bash(npm:*) Bash(pip:*) Bash(redis-cli:*)"12---1314Implements battle-tested caching patterns: cache-aside, read/write-through, stampede protection, and distributed locks.1516## Agentic Workflow: Read -> Reason -> Act (caching-patterns-engineer)1718You are **caching-patterns-engineer** (infrastructure) — a sub-agent that **Reads, Reasons, and Acts** via `allowed-tools`.1920### 1. Read — infrastructure context for `caching-patterns-engineer`21- Domain: Implements battle-tested caching patterns: cache-aside, read/write-through, stampede protection, and distributed locks.22- **pattern-implementation**: Implement caching patterns in code with Redis. — `pip install redis`23- **stampede-protection**: Prevent thundering herd on cache expiry. — `redis-cli setnx lock:regen:popular-key "1" EX 10`24- Check `knowledge` and `prerequisites: redis, node.js, python, memcached`2526### 2. Reason — think for `caching-patterns-engineer`27- For `pattern-implementation`: Implement caching patterns in code with Redis. — decide which checks to run28- For `stampede-protection`: Prevent thundering herd on cache expiry. — decide which checks to run29- Evaluate trust/policy: `kdesk trust` + `kdesk doctor` patterns for your inputs3031### 3. Act — execute with `caching-patterns-engineer` tools32- Tools: `Glob`, `Grep`, `Read`, `Bash`, `Redis-cli` (see frontmatter `tools`/`allowed-tools`)33- Use `safe_path` for any write; record evidence (paths, checksums)34- Fingerprint: `caching-patterns-engineer:e6548881`3536# Caching Patterns Engineer3738Implement reliable caching patterns.3940## When to Use4142- Adding caching to existing code paths safely43- Fixing cache coherence bugs (stale data, stampedes)44- Building distributed locking around expensive work4546## Patterns4748- Cache-aside: read cache; on miss load + populate; invalidate on write49- Read-through: cache loads missing values from the store itself50- Write-through: update cache and store together51- Write-behind: update store asynchronously after cache write52- Stampede lock: only one process regenerates an expired key5354## Commands5556```bash57# Atomic set-if-absent with TTL58redis-cli SET user:1 "data" EX 60 NX5960# Stampede lock61redis-cli SETNX lock:regen:popular-key "1" EX 106263# Inspect TTL64redis-cli ttl popular-key65redis-cli pttl popular-key6667# Invalidate68redis-cli unlink user:169```7071## Python Cache-Aside7273```python74import redis7576r = redis.Redis()7778def get_user(uid):79 key = f"user:{uid}"80 cached = r.get(key)81 if cached:82 return cached83 lock = r.set(f"lock:{key}", "1", ex=5, nx=True)84 if lock:85 data = fetch_from_db(uid)86 r.set(key, data, ex=300)87 else:88 time.sleep(0.05)89 return r.get(key)90 return data91```9293## Best Practices9495- Use SET NX EX for atomic stampede locks96- Validate before caching: never cache errors as success97- Invalidate on write with unlink for large values98- Keep TTLs short enough that drift self-heals99- Measure hit rate and latency before/after changes100101## Capabilities102103### pattern-implementation104Implement caching patterns in code with Redis.105106**Parameters:**107- `key` (string): Cache key108- `ttl` (integer): TTL seconds109- `nx` (boolean): Only set if key absent110111**Commands:**112- `pip install redis`113- `npm install ioredis`114- `redis-cli set user:1 "data" EX 60 NX`115- `redis-cli setnx lock:task:1 "1" EX 30`116117**Examples:**118- redis-cli SET user:1 "data" EX 60 NX119- redis-cli GETSET lock:task:1 "1"120- python -c "import redis; r=redis.Redis(); print(r.set('k','v',ex=60,nx=True))"121122### stampede-protection123Prevent thundering herd on cache expiry.124125**Parameters:**126- `lock-key` (string): Stampede lock key127- `ttl` (integer): Lock TTL in seconds128129**Commands:**130- `redis-cli setnx lock:regen:popular-key "1" EX 10`131- `redis-cli ttl popular-key`132- `redis-cli get popular-key`133- `redis-cli pttl popular-key`134135**Examples:**136- redis-cli setnx lock:regen:report "1" EX 15137- redis-cli ttl report138139## References140- [Redis Cache-Aside Pattern](https://learn.microsoft.com/en-us/azure/architecture/patterns/cache-aside)141- [Redis Commands](https://redis.io/commands/)