Why Redis for Caching?
Database queries are the #1 performance bottleneck in most web applications. A typical PostgreSQL query takes 5-50ms. Redis serves the same data in 0.1-0.5ms — that's a 100x improvement. By caching frequently accessed data in Redis, you reduce database load, lower latency, and serve more users with the same hardware. It's the single highest-impact performance optimization you can make.
Caching Strategies
Cache-aside (lazy loading): Application checks Redis first. Cache miss? Query the database, store the result in Redis with a TTL, then return it. This is the most common and safest pattern. Write-through: Every database write also updates the cache. Ensures consistency but adds write latency. Write-behind: Writes go to cache first, then asynchronously to the database. Fast writes but risks data loss.
// Cache-aside pattern (Node.js)
async function getUser(id) { const cached = await redis.get(`user:${id}`); if (cached) return JSON.parse(cached); const user = await db.query('SELECT * FROM users WHERE id = $1', [id]); await redis.setex(`user:${id}`, 3600, JSON.stringify(user)); return user;
}TTL and Expiration Policies
Every cached value needs a TTL (time-to-live). Set it based on data volatility: user profiles might cache for 1 hour, product listings for 5 minutes, and real-time analytics for 10 seconds. Use SETEX or SET key value EX seconds. Redis automatically evicts expired keys — but only when accessed or during periodic cleanup.
Cache Invalidation Patterns
"There are only two hard things in computer science: cache invalidation and naming things." Event-driven invalidation is the most reliable approach: when data changes, publish an event that deletes the relevant cache keys. For complex relationships, use tag-based invalidation — tag cached items with related entity IDs and invalidate by tag.
Redis Data Structures for Caching
Redis isn't just key-value. Hashes store object fields efficiently (HSET user:123 name "John" email "j@x.com"). Sorted sets power leaderboards and ranked feeds. Lists implement queues. Sets track unique items (online users, feature flags). Choosing the right data structure can reduce memory usage by 5-10x compared to serialized JSON strings.
Memory Management and Eviction
When Redis runs out of memory, its eviction policy decides what to delete. allkeys-lru evicts least-recently-used keys — the safest default for caching. volatile-lru only evicts keys with a TTL. noeviction returns errors on write — useful when Redis stores critical data. Monitor memory usage with INFO memory and set maxmemory appropriately.
Beyond Caching: Other Redis Use Cases
Redis powers rate limiting with INCR and EXPIRE. It handles session storage for stateless backends. Its Pub/Sub enables real-time notifications. SETNX implements distributed locks. Redis Streams support event sourcing. In 2026, Redis is less "just a cache" and more "the in-memory data layer."
Conclusion
Adding Redis caching to your stack is the fastest path to dramatic performance gains. Start with cache-aside on your slowest queries, set reasonable TTLs, and implement event-driven invalidation for critical data. The investment pays off immediately — fewer database queries, faster response times, and happier users.