How does Caching Works?
A cache is a hardware or software component that stores data to serve future requests for that data faster. The data stored in a cache might result from an earlier computation or a copy of data stored elsewhere. So the cache is temporary storage for data.
Caching (pronounced “cashing”) is the process of storing data in a cache.
Caching is an important topic to learn as a back-end developer, especially in regard to databases. Caching can be done in many ways, but we’ll be mainly focusing on database caching in this blog.
We all know that databases store data in the ROM (Read-only memory). It could be an HDD(Hard Disk Drive) or SSD(Solid State Drive). We don’t store the data in the RAM (Random Access Memory) because once we cut off the power to the computer, the data in the RAM is lost permanently while it can be stored on the ROM. So now the data storing part is clear, then why do we need Caching.
There are two reasons why accessing the data from the ROM is always not good:
1) Lookup is slow
This means that the time taken by the database to fetch the required data from the ROM is slow, and we don’t want to keep our users waiting as this could lead to poor performance of your application.
2) Computation Cost is High
Let’s think that your database is handling 10,000 rows. Then you’ll require a lot of computation to process the data and show it every time the user requests the complete data. So if you are using a cloud instance, you’ll burn up your credits much faster in this case.
How does Redis Caching System solve these problems?
Redis is an in-memory data structure store, used as a distributed, in-memory key-value database, cache and message broker, with optional durability.
Well, that is a lot to take in. In short, Redis is a simple software that is a wrapper around your RAM that can store, get and expire things of the RAM. So when the same query comes again, instead of performing the lookup in the database, Redis queries the RAM and get’s the data and returns it to your client or the back-end script.
So basically, with the help of your RAM, Redis can optimize your database lookups and send the data faster. Why use some Redis software? You can do it manually using a script, but Redis offers more functionalities like setting the expiration date for your data and setting up keys based on its existence.
Learn about Redis on our Codedamn Course.
Ideal system for Redis Caching System
So an ideal system should look like something similar to the below image.
So when a client sends a request to your API, the Redis should check the cached data. If found, it should return the data. If the data is not found in the cache, then the data should be queried from the database. Again, the database will fetch the data from the hard drive and return to the Redis. It then returns the data to the API and caches the data for further use.
Redis here acts as a query booster than as a critical system because if it cannot cache the query, it will automatically request the database, fetch the data, and then send it to the API.
Even the database engine itself caches some data based on the frequency of the query to the RAM. Still, it is always good to have a caching mechanism as it reduces the computation that has to be performed on the queried data.
You can watch the YouTube video here.
Sharing is caring
Did you like what Mehul Mohan wrote? Thank them for their work by sharing it on social media.
- Reactjs fetch API example
- Why use Next.js with Apollo