All posts
Article · 1 min ·

System Design: Caching Strategies

There are only two hard things in Computer Science: Cache Invalidation and naming things.

Cache-Aside (Lazy Loading)

  1. App asks Cache for Key X.
  2. Cache says "Miss".
  3. App asks Database.
  4. App writes result to Cache.
  5. App returns result.

Pros: Only caches what is needed.
Cons: Initial latency (Cache Miss).

Write-Through

  1. App writes to Cache AND Database at the same time.

Pros: Cache is always fresh.
Cons: Slow writes.

TTL (Time To Live)

When do you delete the cache?
Set a TTL (e.g., 5 minutes). After 5 minutes, Redis deletes it automatically.
This leads to "Eventually Consistent" data.

Conclusion

Start with Cache-Aside + TTL. It solves 90% of performance problems.

Related posts