Redis Caching Strategies for APIs
Cache-aside, TTL policies, and stampede protection for high-read endpoints.
Nestlancer Editorial

Caching turns read-heavy endpoints from database bottlenecks into millisecond responses—when TTLs, invalidation, and stampede protection are deliberate.
Cache-aside pattern
async getPost(slug: string): Promise<Post> {
const cached = await this.redis.get(`post:${slug}`);
if (cached) return JSON.parse(cached);
const post = await this.prisma.read.post.findUnique({ where: { slug } });
await this.redis.set(`post:${slug}`, JSON.stringify(post), 'EX', 300);
return post;
}
TTL policy matrix
| Data | TTL | Invalidation |
|---|---|---|
| Public blog post | 5–15 min | On publish/update event |
| User session permissions | 1–5 min | On role change |
| Config flags | 30–60 sec | Pub/sub on update |
Stampede protection
Use probabilistic early expiration or request coalescing (single-flight) when hot keys expire. A viral post should not trigger 10,000 simultaneous database queries.
When not to cache
- Rapidly mutating personalized dashboards
- Financial balances requiring strong consistency
- Responses larger than 1MB—consider CDN instead
Monitor hit ratio per endpoint. Below 60% on intentionally cached routes means TTL or key design needs rework.
Redis caching is a product decision as much as an infrastructure choice—stale data tolerance must be explicit.
Comments
Loading comments…
Related posts

Case Studies
Cutting Deploy Time from 45 Minutes to Five
CI caching, smaller artifacts, and service-level pipelines after monolith split.

Case Studies
Scaling a Freelance Marketplace Architecture
Matching algorithms, escrow flows, and dispute resolution at growing GMV.

Case Studies
GDPR Compliance Platform Rebuild
Data maps, deletion workflows, and consent logging across microservices.

Case Studies
Migrating from WebSockets to SSE
Simpler infra, CDN friendliness, and trade-offs for one-way realtime feeds.