System Design Intermediate
02 Caching
The fastest query is the one you never run. A cache buys you latency and shields the database -- and it charges you back in staleness, invalidation bugs, and brand-new failure modes.
A product page runs 12 database queries and takes 800 ms; the site does 100 reads for every write, and product data changes only when a merchant edits it. Add caching: say what you cache, where the cache lives, the TTL you pick, and exactly what happens when a merchant updates a price.
Hints
- Cache the assembled result, not the 12 queries separately.
- Static assets belong on the CDN; the product JSON belongs in an application cache such as Redis, keyed by product id.
- For invalidation pick one: delete-on-write or write-through. Deleting the key on update, with a modest TTL as a safety net, is the standard answer.