What Is Caching & Why It Is Needed?

Photo by Andrew Neel on Unsplash

What Is Caching & Why It Is Needed?

So if you have heard any developer talking about building a system, you must have heard about caching. So in this article, I will try to explain, what is caching & why it is so important.

What is Caching?

So let’s say you want to build a blogging site like Medium. How would you go about it?

Like in any other system, there will be a client, a server serving the requests & there will be a DB where we will store all our data as shown in the below diagram.

The client makes an API call to the server, and the server fetches the data from the DB & sends the data to the client after doing some processing.

Now databases with SSD drives take on an average of 1–2 ms to read data of size 1 MB as the DB’s store all the data in the hard disk. Also, hard disks are considerably slower. So every request goes to the server, the server will request the blog content from the DB & send it to the UI.

What will happen if we do not hit the DB for every request? So we can introduce a system such that the server will store some relevant data in its memory or the memory of a machine kept for this specific purpose. As in memory lookup is 4–5 times faster than disk lookup we can considerably improve our API latency. And this whole concept is called caching.

So now according to this new architecture, whenever a request comes from the client, the server will first look up if the blog/content is present in the cache or not. If the content is present in the cache it will be served without going to the DB. If the content is not present in the cache then the server will go to the DB, will fetch it & send it to the user. Then the server needs to decide whether it should store this data in the cache or not & there are many algorithms that help the server decide that. We will not look at those in this blog but you can look it up.

So this architecture decreases our read latencies by a significant amount & it also reduces the load on our DBs. There are some disadvantages to using caching as well but they are mostly involved when we are writing in the DB.

So hope you would have got an idea of caching. If you have any suggestions feel free to add them in the comments below. Please press like if you liked the content. Do follow me on my YouTube channel where I am publishing a lot of great content.

Did you find this article valuable?

Support Abhijeet Gurle by becoming a sponsor. Any amount is appreciated!