Redis Cache Invalidation Strategy Design Prompt
Design a correct cache-invalidation strategy for Redis — write-through vs write-behind vs cache-aside, versioned keys, and event-driven busting — so stale reads never outlive the source of truth.
- Target user
- Backend engineers designing cache coherence
- Difficulty
- Advanced
- Tools
- Claude, ChatGPT, Cursor
The prompt
You are a senior engineer who designs cache-coherence and invalidation strategies for Redis in front of a database. I will provide: - The read/write ratio and the source of truth (SQL, document store, upstream API) - How stale a read is allowed to be (hard consistency vs eventual, and the max tolerable staleness window) - Current key structure and how writes currently touch the cache Your job: 1. **Pick the pattern**: - **Cache-aside (lazy)**: app reads cache, on miss loads from DB and populates. Invalidate by `DEL`ing the key on write. Simple; risks the read-populates-stale race (a concurrent write between DB read and cache `SET`). - **Write-through**: writes go to cache and DB together — cache is always warm but every write pays the cache cost. - **Write-behind**: writes hit cache and flush to DB asynchronously — fast writes, but a crash can lose un-flushed data. 2. **Choose the invalidation mechanism**: explicit `DEL` on write, short TTL as a safety net, **versioned keys** (`user:42:v7` — bump the version instead of deleting, so no reader ever sees a half-written key), or **event-driven** busting via keyspace notifications / a change stream. 3. **Kill the stale-read race**: for cache-aside, prefer *delete* over *update* on write, and consider delayed double-delete (delete, write DB, delete again after a short delay) to catch in-flight populates. 4. **Bound worst case with TTL**: even with explicit invalidation, always set a TTL so a missed bust self-heals within the tolerable staleness window. 5. **Handle multi-key / derived caches**: list caches, aggregates, and denormalised views need tag-based or versioned group invalidation. 6. **Define signals**: stale-read reports, hit ratio, and `keyspace_misses` after a deploy. Mark DESTRUCTIVE or risky: `KEYS pattern` for bulk invalidation on prod (use `SCAN`), `FLUSHDB`/`FLUSHALL` as an invalidation hammer, and write-behind on data you cannot afford to lose. --- Read/write ratio + source of truth: [DESCRIBE] Staleness tolerance: [DESCRIBE] Current key structure: [PASTE]
Run this prompt with AI
Test it, get an AI-improved version, or compare models — live in the Prompt Workspace. No copy-paste.
Why this prompt works
Most “cache bug” incidents are really invalidation bugs — a write updated the DB but the cache kept serving the old value, or a concurrent read repopulated a key with stale data. This prompt makes you name the consistency requirement first, then pick a pattern whose failure mode you can live with, and always adds a TTL backstop so a missed bust self-heals.
How to use it
- State the staleness budget in seconds — it decides whether a TTL backstop alone is enough.
- Prefer delete-on-write over update-on-write for cache-aside to avoid the populate race.
- Use versioned keys for hot, race-prone entities so readers never observe a partially written value.
Useful commands
# Versioned-key invalidation: bump the version pointer, old key ages out via TTL
redis-cli INCR user:42:ver
redis-cli SET user:42:v$(redis-cli GET user:42:ver) '<json>' EX 300
# Safe bulk invalidation — never KEYS on prod
redis-cli --scan --pattern 'user:42:*' | xargs -r redis-cli DEL
# Event-driven busting via keyspace notifications
redis-cli CONFIG SET notify-keyspace-events KEA
Common findings this catches
- Update-on-write cache-aside → stale populate race under concurrency.
- No backstop TTL → one missed bust serves stale data forever.
KEYSfor bulk invalidation → blocks the server under load.- Write-behind on durable data → acknowledged writes lost on crash.
- Derived/list caches never invalidated → aggregates drift from rows.
When to escalate
- Hard consistency is required end-to-end — caching may be the wrong tool for that field.
- Invalidation fan-out is large (one write busts thousands of keys) — move to versioned group keys.
Related prompts
-
Redis Client-Side Caching (Tracking) Design Prompt
Design a RESP3 client-side caching layer with server-assisted invalidation — default vs broadcast tracking, BCAST prefixes, OPTIN/OPTOUT — without serving stale data.
-
Redis Cache Stampede Prevention Design Prompt
Design defenses against cache stampede (dogpile/thundering herd) when a hot Redis key expires and many clients recompute it at once — locks, early recompute, and jittered TTLs.
-
Redis Caching Strategy Design Prompt
Design a Redis caching layer — cache-aside, write-through, write-behind patterns, TTLs, and stampede protection for read-heavy services.
-
Redis Session Store Design Prompt
Design a Redis session store with correct TTLs, serialization, and a decision between sticky sessions and shared session state.
More Redis prompts & error guides
Browse every Redis prompt and troubleshooting guide in one place.
Reading prompts? Get all 500 in one free PDF
500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.
- 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
- Instant PDF download — yours free, forever
- Plus one practical AI-workflow email a week (no spam)
Single opt-in · unsubscribe anytime · no spam.