Skip to content
DevOps AI ToolKit
Newsletter
All prompts
AI for Redis Difficulty: Advanced ClaudeChatGPTCursor

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

  1. State the staleness budget in seconds — it decides whether a TTL backstop alone is enough.
  2. Prefer delete-on-write over update-on-write for cache-aside to avoid the populate race.
  3. 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.
  • KEYS for 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

More Redis prompts & error guides

Browse every Redis prompt and troubleshooting guide in one place.

Free download · 368-page PDF

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.