Redis is far more than a simple cache. Its data structures -- sorted sets, streams, HyperLogLog, and more -- enable sophisticated caching patterns that can transform application performance.
Cache-Aside Pattern
The most common pattern: check Redis first, fetch from database on miss, store in Redis for next time. Simple, effective, but watch out for cache stampedes on popular keys.
Write-Through vs Write-Behind
- Write-through: Write to cache and database simultaneously. Ensures consistency but adds write latency.
- Write-behind: Write to cache first, asynchronously persist to database. Faster writes but risks data loss on cache failure.
Advanced Patterns
- Sorted sets for leaderboards: ZADD and ZRANGEBYSCORE give you real-time rankings with O(log N) complexity
- Streams for event queues: Redis Streams provide Kafka-like functionality at a fraction of the complexity
- HyperLogLog for unique counts: Count unique visitors using only 12KB of memory regardless of count size
- Bloom filters: Probabilistic data structure to check if an element exists without storing the full dataset
Eviction Policies
Choose wisely: allkeys-lru for general caching, volatile-ttl for mixed workloads, noeviction for critical data. Monitor your eviction rate -- high evictions mean you need more memory or better key design.