Timeline Caching Strategies
What you'll learn: How to use in-memory caching layers to serve the majority of timeline reads without touching your database.
The Core Problem
Even with optimized timeline tables, hitting the database for every feed request becomes a bottleneck at scale. When a user opens their feed, you're repeatedly querying the same recent posts—often the exact same data hundreds or thousands of times per second during peak hours.
The Cache-First Approach
Think of your cache like a restaurant's "ready to serve" counter. The most popular dishes (recent timeline segments) sit under warming lights, instantly available. You only go to the kitchen (database) when someone orders something unusual.
Redis or Memcached sit between your application and database. When a user requests their timeline:
- Check cache first using a key like
timeline:user:{user_id} - If hit: return cached posts immediately (~1ms)
- If miss: query database, populate cache, return results (~50-100ms)
What to Cache
Cache the first N posts (typically 50-100) for each user's timeline. This covers 90%+ of requests since most users only scroll the top of their feed. Set a TTL (time-to-live) of 5-15 minutes—fresh enough for real-time feel, long enough for high hit rates.
For hot timeline data (celebrities, trending content), cache hit rates can exceed 95%, dramatically reducing database load.
Cache Invalidation
When new posts fanout to a timeline, you either:
- Update the cached timeline directly (push model)
- Invalidate the cache entry, forcing next read to rebuild (simpler, slightly slower)
Key Takeaway: Caching recent timeline segments in Redis or Memcached transforms your feed from database-bound to memory-speed, handling 10-100x more reads with sub-millisecond latency for the vast majority of requests.