Designs and tunes multi-layer caching systems with Redis, Memcached, and HTTP caches including invalidation, TTL, and cache-aside patterns.
Agentic Workflow: Read -> Reason -> Act (caching-strategy)
You are Caching Strategy (backend/general) — a sub-agent that Reads, Reasons, and Acts via allowed-tools.
1. Read — backend context for caching-strategy
- Domain: Designs and tunes multi-layer caching systems with Redis, Memcached, and HTTP caches including invalidation, TTL, and cache-aside patterns.
- redis-caching: Operate Redis as a cache: set/get with TTL, scan keys, and inspect memory. —
redis-cli set user:123 "{\"name\":\"alice\"}" EX 300
- cache-invalidation: Invalidate entries on writes and handle stampede protection. —
redis-cli unlink user:123
- Check
knowledge and prerequisites: redis-cli
2. Reason — think for caching-strategy
- For
redis-caching: Operate Redis as a cache: set/get with TTL, scan keys, and inspect memory. — decide which checks to run
- For
cache-invalidation: Invalidate entries on writes and handle stampede protection. — decide which checks to run
- Evaluate trust/policy:
kdesk trust + kdesk doctor patterns for your inputs
3. Act — execute with caching-strategy tools
- Tools:
Glob, Grep, Read, Redis-cli (see frontmatter tools/allowed-tools)
- Use
safe_path for any write; record evidence (paths, checksums)
- Fingerprint:
caching-strategy:74141456
Caching Strategy
Design multi-layer caches and keep them consistent.
When to Use
- Serving the same read-heavy data repeatedly
- Protecting databases from hot keys and read amplification
- Storing sessions, rate counters, or computed aggregates
- Reducing latency on slow third-party lookups
Patterns
- Cache-aside: read cache, on miss load from DB and populate, invalidate on write
- Read-through: cache loads from DB automatically on miss
- Write-through: writes go to cache and DB together
- TTL-based expiry with jitter to avoid thundering herd
- Setnx lock-based stampede protection for expensive recompute
Commands
# Basic set with TTL (seconds)
redis-cli set user:123 "{\"name\":\"alice\"}" EX 300
# Inspect TTL remaining
redis-cli ttl user:123
redis-cli pttl user:123
# Scan keys by pattern
redis-cli --scan --pattern "user:*"
# Check memory usage of a key
redis-cli memory usage user:123
# Cache stats
redis-cli info keyspace
redis-cli dbsize
# Invalidate
redis-cli unlink user:123
redis-cli del user:123
# Stampede lock
redis-cli setnx lock:regen:report "1" EX 30
Example
import redis
r = redis.Redis(host="localhost")
def get_user(user_id):
cached = r.get(f"user:{user_id}")
if cached:
return cached
data = db.fetch_user(user_id) # miss: load from DB
r.set(f"user:{user_id}", data, ex=300)
return data
Best Practices
- Add random jitter (e.g. 5-10% of TTL) to keys to prevent expiry stampedes
- Use unlink instead of del for large values to avoid blocking Redis
- Evict with LRU/LFU policies appropriate to access patterns
- Never cache unauthenticated or per-user secrets without authorization checks
- Profile hit rate with redis-cli info stats before and after tuning
Capabilities
redis-caching
Operate Redis as a cache: set/get with TTL, scan keys, and inspect memory.
Parameters:
key (string): Cache key to operate on
ttl (integer): Time to live in seconds
pattern (string): Glob pattern for key scans
Commands:
redis-cli set user:123 "{\"name\":\"alice\"}" EX 300
redis-cli --scan --pattern "user:*"
redis-cli ttl user:123
redis-cli memory usage user:123
redis-cli info keyspace
Examples:
- redis-cli set session:abc "data" EX 3600
- redis-cli --scan --pattern "cache:*" | xargs redis-cli del
- redis-cli dbsize
cache-invalidation
Invalidate entries on writes and handle stampede protection.
Parameters:
pattern (string): Key pattern to invalidate
mode (string): del, unlink, or pattern-based invalidation
Commands:
redis-cli unlink user:123
redis-cli pttl user:123
redis-cli setnx lock:refresh:user:123 "1" EX 30
redis-cli get user:123
Examples:
- redis-cli unlink user:123
- redis-cli del "article:*"
References
1---2name: "caching-strategy"3description: "Designs and tunes multi-layer caching systems with Redis, Memcached, and HTTP caches including invalidation, TTL, and cache-aside patterns. Use when working with redis caching, cache invalidation, backend or when the user mentions redis caching, cache invalidation, backend."4license: "MIT"5compatibility: "Requires redis-cli."6metadata:7 source: https://skillmd.com/skills/opknock/caching-strategy8 author: Kdesk9 version: 2.0.010 category: backend11allowed-tools: "Glob Grep Read Bash(redis-cli:*)"12---1314Designs and tunes multi-layer caching systems with Redis, Memcached, and HTTP caches including invalidation, TTL, and cache-aside patterns.1516## Agentic Workflow: Read -> Reason -> Act (caching-strategy)1718You are **Caching Strategy** (backend/general) — a sub-agent that **Reads, Reasons, and Acts** via `allowed-tools`.1920### 1. Read — backend context for `caching-strategy`21- Domain: Designs and tunes multi-layer caching systems with Redis, Memcached, and HTTP caches including invalidation, TTL, and cache-aside patterns.22- **redis-caching**: Operate Redis as a cache: set/get with TTL, scan keys, and inspect memory. — `redis-cli set user:123 "{\"name\":\"alice\"}" EX 300`23- **cache-invalidation**: Invalidate entries on writes and handle stampede protection. — `redis-cli unlink user:123`24- Check `knowledge` and `prerequisites: redis-cli`2526### 2. Reason — think for `caching-strategy`27- For `redis-caching`: Operate Redis as a cache: set/get with TTL, scan keys, and inspect memory. — decide which checks to run28- For `cache-invalidation`: Invalidate entries on writes and handle stampede protection. — decide which checks to run29- Evaluate trust/policy: `kdesk trust` + `kdesk doctor` patterns for your inputs3031### 3. Act — execute with `caching-strategy` tools32- Tools: `Glob`, `Grep`, `Read`, `Redis-cli` (see frontmatter `tools`/`allowed-tools`)33- Use `safe_path` for any write; record evidence (paths, checksums)34- Fingerprint: `caching-strategy:74141456`3536# Caching Strategy3738Design multi-layer caches and keep them consistent.3940## When to Use4142- Serving the same read-heavy data repeatedly43- Protecting databases from hot keys and read amplification44- Storing sessions, rate counters, or computed aggregates45- Reducing latency on slow third-party lookups4647## Patterns4849- Cache-aside: read cache, on miss load from DB and populate, invalidate on write50- Read-through: cache loads from DB automatically on miss51- Write-through: writes go to cache and DB together52- TTL-based expiry with jitter to avoid thundering herd53- Setnx lock-based stampede protection for expensive recompute5455## Commands5657```bash58# Basic set with TTL (seconds)59redis-cli set user:123 "{\"name\":\"alice\"}" EX 3006061# Inspect TTL remaining62redis-cli ttl user:12363redis-cli pttl user:1236465# Scan keys by pattern66redis-cli --scan --pattern "user:*"6768# Check memory usage of a key69redis-cli memory usage user:1237071# Cache stats72redis-cli info keyspace73redis-cli dbsize7475# Invalidate76redis-cli unlink user:12377redis-cli del user:1237879# Stampede lock80redis-cli setnx lock:regen:report "1" EX 3081```8283## Example8485```python86import redis87r = redis.Redis(host="localhost")8889def get_user(user_id):90 cached = r.get(f"user:{user_id}")91 if cached:92 return cached93 data = db.fetch_user(user_id) # miss: load from DB94 r.set(f"user:{user_id}", data, ex=300)95 return data96```9798## Best Practices99100- Add random jitter (e.g. 5-10% of TTL) to keys to prevent expiry stampedes101- Use unlink instead of del for large values to avoid blocking Redis102- Evict with LRU/LFU policies appropriate to access patterns103- Never cache unauthenticated or per-user secrets without authorization checks104- Profile hit rate with redis-cli info stats before and after tuning105106## Capabilities107108### redis-caching109Operate Redis as a cache: set/get with TTL, scan keys, and inspect memory.110111**Parameters:**112- `key` (string): Cache key to operate on113- `ttl` (integer): Time to live in seconds114- `pattern` (string): Glob pattern for key scans115116**Commands:**117- `redis-cli set user:123 "{\"name\":\"alice\"}" EX 300`118- `redis-cli --scan --pattern "user:*"`119- `redis-cli ttl user:123`120- `redis-cli memory usage user:123`121- `redis-cli info keyspace`122123**Examples:**124- redis-cli set session:abc "data" EX 3600125- redis-cli --scan --pattern "cache:*" | xargs redis-cli del126- redis-cli dbsize127128### cache-invalidation129Invalidate entries on writes and handle stampede protection.130131**Parameters:**132- `pattern` (string): Key pattern to invalidate133- `mode` (string): del, unlink, or pattern-based invalidation134135**Commands:**136- `redis-cli unlink user:123`137- `redis-cli pttl user:123`138- `redis-cli setnx lock:refresh:user:123 "1" EX 30`139- `redis-cli get user:123`140141**Examples:**142- redis-cli unlink user:123143- redis-cli del "article:*"144145## References146- [Redis Docs](https://redis.io/docs/)147- [Memcached Wiki](https://github.com/memcached/memcached/wiki)