# Redis Caching

> Implementation playbook for using Redis as a cache and for common data-structure patterns. Use when asked to: add caching to a service, choose a caching strategy (cache-aside / write-through / write-behind), set TTLs and eviction, invalidate or warm a cache, implement rate limiting, a distributed lock, sessions, or a leaderboard. Triggers: "Redis", "cache", "caching", "cache-aside", "write-through", "write-behind", "TTL", "eviction", "LRU", "LFU", "cache invalidation", "cache stampede", "thundering herd", "rate limit", "distributed lock", "Redlock", "session store", "leaderboard", "pub/sub", "Lettuce", "Jedis", "@Cacheable".

- Skill: `amitsharma1994/redis-caching` (Agent Skill, multi-file: 6 files)
- Install (CLI): `npx skillmds@latest add amitsharma1994/redis-caching`
- Raw SKILL.md: https://api.skillmd.com/api/skills/amitsharma1994/redis-caching/raw
- Safety review: pending (external: skill-scanner PASS, skillspector PASS)
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- Author: AmitSharma1994 (https://skillmd.com/u/amitsharma1994)
- Updated: 2026-09-22
- Page: https://skillmd.com/skills/amitsharma1994/redis-caching

---


# Redis Caching Skill

A practical playbook for adding Redis caching that actually cuts latency without
serving stale or inconsistent data. Examples lean on Spring Data Redis / common
clients, but the patterns (cache-aside, TTL discipline, stampede protection,
distributed locks) apply to any language.

Work in order: pick a **strategy**, set **TTL + eviction**, get **invalidation**
right (the hard part), protect against **stampedes**, then layer on **data-structure
patterns** (rate limiting, locks, sessions, leaderboards).

---

## When to Use

- Adding a cache in front of a database or expensive computation
- Choosing between cache-aside, write-through, and write-behind
- Deciding TTLs, eviction policy, and keyspace naming
- Fixing stale reads or a cache-stampede / thundering-herd incident
- Implementing rate limiting, distributed locks, sessions, or leaderboards

---

## The Workflow

```mermaid
flowchart LR
    S[1. Strategy] --> T[2. TTL & Eviction]
    T --> I[3. Invalidation]
    I --> P[4. Stampede Protection]
    P --> D[5. Data-Structure Patterns]
```

---

### 1. Strategy

Choose how reads and writes flow through the cache. **Cache-aside** is the default;
reach for write-through/write-behind only when you need them.

See [Caching Strategies](./references/strategies.md).

---

### 2. TTL & Eviction

Every cached key needs a TTL — caches are not source of truth. Match the eviction
policy (`allkeys-lru`, `volatile-ttl`, …) to your access pattern and memory budget.

See [TTL & Eviction](./references/ttl-eviction.md).

---

### 3. Invalidation

The hard part. Prefer short TTLs + write-time invalidation over clever schemes.
Decide between deleting vs. updating keys on write, and namespace keys for bulk busts.

See [Invalidation](./references/invalidation.md).

---

### 4. Stampede Protection

When a hot key expires, concurrent misses can hammer the database. Defend with
locking/single-flight, jittered TTLs, or early recomputation.

See [Stampede Protection](./references/stampede.md).

---

### 5. Data-Structure Patterns

Redis is more than a cache: rate limiters, distributed locks, sessions, leaderboards,
and pub/sub — each maps to a specific data structure and command set.

See [Data-Structure Patterns](./references/patterns.md).

---

## Quick Checklist

- [ ] Strategy chosen (cache-aside by default; justify write-through/behind)
- [ ] Every key has a TTL; no unbounded keys
- [ ] Eviction policy matches access pattern + memory budget
- [ ] Invalidation defined at write time; keys namespaced for bulk busts
- [ ] Stampede protection on hot keys (lock / jitter / early refresh)
- [ ] Null/negative results cached briefly to prevent penetration
- [ ] Distributed locks have a TTL + ownership token (safe release)
- [ ] Keyspace naming convention documented (`entity:id:field`)

---

## Reference Files

- [Caching Strategies](./references/strategies.md)
- [TTL & Eviction](./references/ttl-eviction.md)
- [Invalidation](./references/invalidation.md)
- [Stampede Protection](./references/stampede.md)
- [Data-Structure Patterns](./references/patterns.md)

