System Design ( Day - 12 )

Manoj KumarManoj Kumar
4 min read

Distributed Caches

Cache is nothing but a small storage system which is very fast than traditional storage devices, but we don’t get much storage

With the help of this cache, we can reduct the network calls to the Database, to increase the speed of the response, and to reduce the cost.
Whenever we make a frequent calls that data has to be stored inside the cache, when ever we are calling the same data we’ll get that data inside the cache we don’t need to go to the Database for that data.
with this we can avoid load on the database, then the cost will automatically reduced,
Everything perfect if the cache is that much fast and efficient and cost effective then we can store everything in the cache right?
but no we can’t that because the cache memory is costly compared to the regular database, so we can’t buy as much as database,
if we are storing everything in the cache then the search time will increase, if that increases then there is no use of the cache, there’s no point of using a cache we can directly use the Database

The cache should be designed efficient that the we have to predict the upcoming load to the database and cache that data and store it inside the cache, main thing is when we have to make an entry inside the cache and when we have to deleted that data from the cache, and that stored data should be relevant to the requests that are coming through the network calls, for doing all these things we have cache policy which is LRU (Least recently used) we have to remove those entries which are least recently used things, and we have to keep things which are MRU (Most recently used).

if our cache policy is not good then if we make a call to the cache hey do you have this data, it’ll always return no i don’t, then it’s waste to have the cache at the first point, we can directly call the database we are wasting our another extra call to check the cache that’s not actually good.

When we have low storage in the cache, we are inserting and deleting again and again the same content whenever the user calls the same things this is called trashing. it’s more that damaging the performance then increasing it, we should avoid this type of things.

Consistency Problem: With the cache we don’t really get the consistent with the database and the cache memory, let’s say we have profile stored inside the cache then the user updates the password, but its not reflected in the cache it’s only updated on the database, that is a main issue.

Local cache
in this local cache the cache will be close to the servers, like it’ll be in the servers in memory, what if any of the severs fails, or what if server1’s cache and server 2’s cache are not consistent they are not in sync with each other. benifit’s are like it’s very faster to get the data from the in memory,

Global cache
This is not closer not closer to the servers it’s between the servers and database, it’s fast and if it contains the cache it returns or it misses then it’s going to query the database for that, example would be Redis, in this case if any of the servers crashed then the cache will not be affected. we can scale this redis independently it won’t bother about the servers

In these two methods both has its own advantages and disadvantages, but global cache has more accuracy, local cache is very fast.
What we need is Accuracy than speed, so we can choose the Global cache, we can scale this redis independently it won’t bother about the servers

Write Through cache
its like we’ll check if the data that we are updating is in the cache if yes then we’ll update that in the cache and then we’ll go to the database and update it there also, that is write through we are going through the cache.

Write Back
its like we are directly updating the Database and then we’ll come back to the cache if that data is there means then we’ll remove that data, if the user is agin requesting for that data then we’ll get that data from the database and store it in the cache and return it back to the client.

Hybrid write policy.
we can use this hybrid write policy, where we’ll update the data inside the cache but not in the database at that time, we’ll be serving the users with that data inside the cache when i got the much data in the cache then ill go and push that data into the database in bulk, no each time as in the Write through.

0
Subscribe to my newsletter

Read articles from Manoj Kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Manoj Kumar
Manoj Kumar