
- Reads from an in-memory cache often complete in under 1 ms; the same read from a typical database may take 20–30 ms or more.
- Under heavy load, those milliseconds multiply into significantly higher throughput and lower cost.
- A request arrives for a trending photo.
- The application checks Redis first:
- If Redis contains the item → cache hit: return value from Redis; skip the database.
- If Redis does not contain the item → cache miss: query the database, write the result back into Redis (often with a TTL), then return the response. Future requests will hit the cache.

- Cache items that are:
- Small in size (so the cache stays memory-efficient)
- Read frequently (high read-to-write ratio)
- Updated rarely (reduces invalidation complexity)

- Login sessions must be stored in a shared store accessible to all app servers. Redis is often used for both sessions and caching because session data is small, read on nearly every request, and requires low latency.
- Keep session keys and cached keys properly namespaced to avoid collisions (for example:
session:{user_id}vsprofile:{user_id}).
Redis is commonly used for both caching hot application data and as a session store. Use clear namespaces for session keys and cache keys to avoid accidental collisions.

Key practices
- Use clear key naming:
photo:{id},profile:{id}to make invalidation and debugging easier. - Apply sensible TTLs for items where eventual consistency is acceptable.
- Combine explicit invalidation on writes/deletes with TTLs to limit inconsistency windows.
- Test failure modes: cache node loss, partial updates, and race conditions (e.g., double writes).
- Monitor cache hit/miss rates, memory usage, and eviction count to tune policies.
Cache invalidation is one of the hardest problems in distributed systems. Choose a strategy (cache-aside vs write-through vs write-behind) based on your consistency, latency, and complexity requirements, and test failure scenarios (cache node failure, incomplete invalidation).
- Caching (e.g., Redis) dramatically accelerates reads for small, hot, infrequently changing data.
- The cache-aside pattern is simple and widely used: check cache → on miss read DB → populate cache.
- Design for cache consistency using TTLs, explicit invalidation, or read/write caching strategies that align with your correctness and latency needs.
- Redis
- Martin Kleppmann, “Designing Data-Intensive Applications” – section on caching and consistency
- Patterns: Cache-aside, Read-through, Write-through, Write-behind — search for implementation guides in your language or framework of choice