When the system performs not upto expectation in terms of UX or/and system resources utilisation.
We need to cache data when its fetching consumes a lot of machine resources and response time doesn't meet expectation while its frequently not updated & frequently requested
It needs to be unique and concise.
Cache refresh
We must be able to refresh the cached data when its original data is updated.
When the cached data is not required or not in use.
In extrememe cases, where the memory is getting overconsumed and we don't have the luxury to increase the memory
The quickest way to implement cache at first would be to add cache and in the background update the cache, this would improve the response time but not the matchine's resources consumption but slowly and steadily, when cache refresh / delete is implement then this background cache can be disabled.
The cache ideally should be centralised and distributed (scalable) like Redis with CDN to improved upon latency due to geo location.
The following are the implementation of caching at different layers.
SQL (MySQL, Postgres, Oracle, etc) databases query caching using Prepared statements.
We can implement memoisation to cache eligible API, CPU intensive functions, etc.
Most of the ORM / ODM provides the ability to cache database query result with flexibility.
It's advisable that CDC (like Debezium) to be implemented for cache delete.
We can cache the API response in the background after the request is sent and this cache can be refereshed whenever its response source of data are updated.
When the FE makes request to BE, the response can be cached.
The FE can call a BE API when the app boots with the list of cache and its minium details and the BE can respond a list of cache needs to be re-generated at FE.