⌨️ Codingintermediaterediscachingperformancebackend

Add a Redis Caching Layer

Implement Redis caching with TTL strategies, cache invalidation, and cache-aside pattern.

The Prompt

prompt.txt
Add Redis caching to the following code. Implement:
1. Cache-aside pattern (try cache first, fallback to DB, populate cache)
2. Appropriate TTL for each type of data
3. Cache key naming convention
4. Cache invalidation on writes (update or delete the cache entry)
5. Stampede protection (prevent many simultaneous DB reads on cache miss)
6. Circuit breaker — fall back gracefully if Redis is down

Code to add caching to:
```
[PASTE YOUR DATA FETCHING CODE]
```

Redis client: [IOREDIS / UPSTASH / NODE-REDIS]
Language: [NODE.JS / PYTHON]

Example Output

Added cache-aside with a 5-minute TTL for user profiles, 30-second TTL for feed data. Cache keys follow `user:${userId}:profile` convention. Added a mutex lock using SETNX for stampede protection, and a try/catch wrapper that falls through to the database if Redis throws.

FAQ

Which AI model is best for Add a Redis Caching Layer?

Claude Sonnet 4 — thinks carefully about cache invalidation edge cases.

How do I use the Add a Redis Caching Layer prompt?

Copy the prompt, replace the [BRACKETED] placeholders with your specific information, and paste into your preferred AI assistant (ChatGPT, Claude, Gemini, etc.). Added cache-aside with a 5-minute TTL for user profiles, 30-second TTL for feed data. Cache keys follow `user:${userId}:profile` convention. Added a mutex lock using SETNX for stampede protection, and a try/catch wrapper that falls through to the database if Redis throws.