# Caching Strategy

> 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.

- Skill: `opknock/caching-strategy` (Agent Skill)
- Install (CLI): `npx get-skill add opknock/caching-strategy`
- Raw SKILL.md: https://api.skillmd.com/api/skills/opknock/caching-strategy/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- License: MIT
- Author: OpKnock (https://skillmd.com/u/opknock)
- Updated: 2026-11-23
- Page: https://skillmd.com/skills/opknock/caching-strategy

---


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

```bash
# 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

```python
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
- [Redis Docs](https://redis.io/docs/)
- [Memcached Wiki](https://github.com/memcached/memcached/wiki)

