A cache is a hardware or software component that stores data so that future requests for that data can be served faster; the data stored in a cache might be the result of an earlier computation or a copy of data stored elsewhere.
Reasons why you need cache :
Eviction Policies for Cache :
Problems with caching :
Thrashing is constantly inputting and outputting into the cache without using the results. This is expensive as well as harmful. Another problem is data consistency.
Where do you place the cache ? Closer to the database or the server ?
If you place close to server, place it in-memory. But if you do this if server fails, the in-memory cache also fails. It is for sure faster but if the information is financial data, it is not a good method.
Another way is to have distributed global caches, benefits are faster and avoid DB load. Something like redis(persistent storage which is like a cache). More accurate than in-memory cache. More resilient and scale independently as well.
Write through or Write Back cache