Lesson 12 / 18
Cache-Aside Pattern
Put Redis in front of a slower database to absorb repeat reads.
Check cache, then the DB
The app checks Redis first. On a hit, it returns instantly. On a miss, it queries the real database, then writes the result into Redis for next time.
The flow in commands
A miss followed by a warm cache for the next request.
GET product:501 # (nil) -> cache miss
# app queries the database for product 501 ...
SET product:501 "{...json...}" EX 300
GET product:501 # cache hit next timeAlways set a TTL
A cached entry without an expiry can quietly go stale forever. Even a generous TTL like an hour keeps data self-healing.