Video in progress
Why do you need a cache, and what data should you not cache?
A cache keeps data that is read a lot and changes rarely in a fast place, which cuts DB load and response time. Data that must always be exact, such as an account balance or a payment status, is usually not read from a cache.
📖 Answer
The benefit of a cache is simple: if a thousand people open the same product page every second, you hit the DB once every five minutes instead of a thousand times.
Good candidates:
- catalogs, categories, settings;
- results that are expensive to compute (reports, recommendations);
- external API responses (exchange rates).
Where to be careful:
- money, balances, stock counts: a stale value does real damage;
- data that differs per user and is read only once: it never gets a hit and just eats memory;
- permissions and roles: if they go stale, it's a security hole.
Before caching, ask yourself: "if this value is 5 minutes old, will anyone be hurt?" If the answer is "yes", either don't cache it or think hard about invalidation.
Sign up to mark studied questions and save your progress.