Why do we need caching?
- To speed up the system. Avoid redoing the same computationally complex operations. To reduce the latency.
- Caching does this by storing a “cached” copy of the data, in a smaller, faster data store.
- When data is immutable caching works great, but when data is mutable we need to update data in DB and in cache.
- Cache eviction policy — specify time that the data will be cached. Refresh rate — how to update the data in cache.
When to use caching?
- Slow to access
- A lot of network requests
- Computationally long operations
- Slow hardware
- Static or slow to change data
When not to use caching?
- Time to access cache = time to access real data
- When low repetition, because caching performance comes from repeated memory access patterns.
- Data changes frequently
How to implement it?
- Client level caching, no need to go to server. Example: questions list on AlgoExpert.
- Server level caching, no need to go to database
- Middleware
Cache writing policies
Made with Super