Caching
A stores the answer to an expensive question so you don't have to ask again. If your homepage runs the same heavy query for every visitor, cache the result in Redis for 60 seconds and serve it instantly.
Caching is a performance optimization, not a source of truth. The classic beginner trap is forgetting cache invalidation — the cached copy goes stale and users see old data. Rule of thumb: cache only when you've measured a real bottleneck, and always have a clear rule for when the cached value expires or gets refreshed.
There are two clean strategies, and mixing them up causes most cache bugs. Time-based expiry (TTL — Time To Live, like an expiry date stamped on the cached value) says "this value is good for 60 seconds, then throw it away" — simple, and fine when slightly stale data is harmless (a view count, a homepage feed). Event-based invalidation says "delete the cached value the moment the underlying data changes" — correct, but you must remember to do it on every write path, which is exactly where people slip. When in doubt, prefer a short TTL: a value that's at most a minute stale is a smaller bug than a value that's stale forever because you forgot one invalidation.