Caching is used to increase the speed and and lower the latency of the system. When we discussed latency we discussed few examples of different operations or data transfers which took a different amount of time. We can imagine that caching is a method that can used to improve the latency when using certain type of methods such as the network, which took considerable amount of time. Now with caching, this latency can be significantly reduced by storing the data in cache which has considerably lower latency.
client --> server --> database
client <-- server <-- database
In this case, we can keep the cache at the client level or the server level, such most of the n/w can be avoided.
Some computationaly long request so in that case we can have cache at the server level, so that server need no do this computational request everytime.
Imagine we have multiple servers and several client communicating with the server, and all these requests are hitting the database. When there are millions of requests per second this might overload the database for instance. So here we can use caching to not having to read from the database everytime. So we can use caching to not be able to read from database everytime. In this siutation we are trying to speed up the process but rather prevent the database from overloading. Here the caching may be placed between the server and the database, or each server may have the cache.
So far we have seen examples where we read from the cache, we shall now look at examples where write to cache. If there is an application where users can read and write posts. So there is a client->server->database. So client make requests to the server to write a post, and these posts are stored in the database. So if want to cache the posts, we can do that on the server. Now we have two sources of truth, one on the cache and second on the database. How do we handle this? There are two concepts here :
Write-Through Cache - A type of caching systems where when you write a piece of data, your system will write that piece of data both in the cache and in the database, in the same operation. So in our example, where user edits the post, the data in the first overwritten on the cache and another request is made to overwrite the data in the database. So downside is that you need to still go to the database, so every single time you make a change, you need to changes twice and go to the database.
Write-Back Cache - A type of caching system where only the cache is updated with the data, and not the server. Then, the database is updated asynchronously at a later period in time. One of the downsides, if something ever happens in the cache, you lose the data before being updated to the database asynchronously.
Its also important to understand that there may be parts of the system wherein we may not care that much about the staleness/non-stateness of the data. For example, the view count on a video is not necessarily the most important thing that need to be highly accurate. This is something we need to ask the interviewer to understand the what is importance to be cached in the system.