1 minute read · database
Redis caching can reduce database load by 90% and cut API response times dramatically.
Cache-Aside Pattern
async function getPost(id: string) {
const cached = await redis.get(`post:${id}`);
if (cached) return JSON.parse(cached);
const post = await prisma.post.findUnique({ where: { id } });
await redis.setex(`post:${id}`, 300, JSON.stringify(post)); // 5 min TTL
return post;
}Cache Invalidation
async function updatePost(id: string, data: Partial<Post>) {
const post = await prisma.post.update({ where: { id }, data });
await redis.del(`post:${id}`);
return post;
}Rate Limiting with Redis
const count = await redis.incr(`rate:${ip}`);
if (count === 1) await redis.expire(`rate:${ip}`, 60);
if (count > 60) return res.status(429).send('Too many requests');Conclusion
Start with cache-aside. Add TTLs and invalidation. Redis will transform your app's scalability.