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

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.

Target user
Backend engineers cutting Redis read load
Difficulty
Advanced
Tools
Claude, ChatGPT, Cursor

The prompt

You are a senior Redis architect who designs client-side caching (server-assisted, a.k.a. "tracking") to eliminate hot read traffic while guaranteeing correctness.

I will provide:
- Read/write ratio and hottest key patterns
- Client language/library and whether it speaks RESP3
- Number of app instances and acceptable staleness window
- Memory budget on the client side

Your job:

1. **Explain the model plainly**:
   - Client-side caching keeps a local copy of values in the application process; Redis **tracks** which keys each client cached and pushes an **invalidation** message when a tracked key changes. This turns repeated reads of stable keys into local memory hits.
   - It requires RESP3 (`HELLO 3`) for push invalidation on the same connection, or RESP2 with a second connection in redirect mode.
2. **Choose a tracking mode** and justify it:
   - **Default (per-key) tracking** (`CLIENT TRACKING ON`): Redis remembers the exact keys each client read and invalidates only those. Precise, but costs server memory for the tracking table.
   - **Broadcast tracking** (`BCAST` with `PREFIX`): Redis sends invalidations for any key matching registered prefixes, without per-key bookkeeping. Cheaper server memory, but noisier — the client caches only what it wants and ignores the rest.
   - **OPTIN / OPTOUT**: `OPTIN` caches only keys read right after `CLIENT CACHING YES` (fine-grained); `OPTOUT` caches everything except keys after `CLIENT CACHING NO`.
3. **Handle RESP2 clients**: use `CLIENT TRACKING ON REDIRECT <client-id>` to send invalidation messages to a second connection subscribed to `__redis__:invalidate`.
4. **Bound staleness and correctness**:
   - There is a race between a value changing and the invalidation arriving. Define a short local TTL as a backstop, and treat the local cache as best-effort.
   - On reconnect, Redis flushes the tracking state — clients must invalidate their entire local cache (a `flush` / null-key invalidation) to avoid serving stale data.
5. **Size it**:
   - Estimate server tracking-table memory (default mode) vs. invalidation-message volume (broadcast). Set `tracking-table-max-keys` to cap default-mode memory; when it fills, Redis evicts and sends invalidations.
   - Set a client-side cache size + eviction (LRU) and a max local TTL.
6. **Design the invalidation handler**: on each invalidation message, drop the key(s) from the local cache; on a null/flush message, clear everything.
7. **Provide a rollout plan**: start with broadcast + narrow prefixes on read-heavy config keys, measure hit ratio and Redis `keyspace_hits`/`ops`, then expand.

Mark RISKY: any correctness gap serves stale data; connection drops require full local invalidation; default-mode tracking table can grow large; broadcast prefixes that are too broad flood clients with invalidations.

---

Read/write ratio & hot keys: [DESCRIBE]
Client & RESP version: [DESCRIBE]
Instances / staleness budget / memory: [DESCRIBE]

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

Client-side caching can wipe out the hottest read traffic in a system, but only if invalidation is correct. The failure everyone hits is stale reads after a reconnect or a missed push. This prompt makes you pick the right tracking mode for your memory and correctness budget, then nails down the two edge cases that cause bugs: the write-to-invalidation race and the reconnect flush.

How to use it

  1. Describe the read/write ratio and the specific hot keys you want to cache locally.
  2. State your client library and whether it speaks RESP3 — this decides push vs. redirect.
  3. Give the staleness budget and instance count so mode and TTL are sized correctly.
  4. State the client memory budget for the local cache.

Useful commands

# Enable RESP3 and per-key tracking on this connection
redis-cli -3 HELLO 3
redis-cli -3 CLIENT TRACKING ON

# Broadcast mode with narrow prefixes (cheap server memory)
redis-cli -3 CLIENT TRACKING ON BCAST PREFIX config: PREFIX feature:

# OPTIN: cache only keys read right after CLIENT CACHING YES
redis-cli -3 CLIENT TRACKING ON OPTIN
redis-cli -3 CLIENT CACHING YES

# RESP2 redirect: invalidations go to another client id's connection
redis-cli CLIENT ID
redis-cli CLIENT TRACKING ON REDIRECT 42

# Observe / cap
redis-cli INFO clients | grep -i tracking
redis-cli CONFIG GET tracking-table-max-keys
redis-cli CONFIG SET tracking-table-max-keys 1000000

Example invalidation handling (pseudocode)

on push message "invalidate" [keys]:
    if keys is null:        # server flushed tracking (reconnect / table full)
        localCache.clear()
    else:
        for k in keys: localCache.delete(k)

read(key):
    v = localCache.get(key)            # local hit, honor short TTL
    if v is fresh: return v
    v = redis.GET(key)                 # miss -> Redis, now tracked
    localCache.put(key, v, ttl=short)
    return v

Common findings this catches

  • No reconnect flush → stale values after a blip; add null-invalidation handling.
  • Default mode on a huge keyspace → tracking table bloat; switch to BCAST + prefixes.
  • Broad broadcast prefix → invalidation storms; narrow the prefixes.
  • No local TTL → a single missed push serves stale data forever.
  • RESP2 client with no redirect connection → invalidations never arrive.

When to escalate

  • Strong-consistency requirements — client-side caching is best-effort; use a different pattern.
  • Invalidation volume approaching read volume — the data is not stable enough to cache.
  • Cross-region clients where push latency exceeds the staleness budget.

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.