Caching in Express App

Piyush Dubey
Nerd For Tech
Published in
4 min readMar 20, 2021

--

Photo by Maria Bobrova on Unsplash

Do you ever feel that you should be fast, better in performance but something you can’t control, right? but what if you remove your data processing from the endpoint, it’s definitely going to save your time. Here comes caching as a rescue, you can store data in ram and whenever it needed it prompt up quickly, the reason is simple, caching serves from the RAM and not from the disk memory. But the question is, should we cache everything? Let’s discuss this thing in detail.

What is Caching?

Whenever we use any dataset/database for any kind of application, it serves from the disk memory and usage internal sorting mechanism named as indexing for faster response time but there may be a scenario where the data amount gets huge and the disk process will take up some time. Then as a software person, we should recommend caching as one of the solutions, It stores the data in RAM and whenever required it serves from there instead of the disk. Here is a visual representation of the concept.

Caching Architecture

Where to use?

It depends upon the use-case, but the majority of the caching relays over static or the same data across the user. Personalized content is not preferred to store in the cache mechanism because it would change on every request.

How to use it?

I am using the node to demonstrate the caching flow along with the Redis, there are several other options such as Memcached, Mongo, or any key-value pair datasets and more. I am going to use this repo for the starter, you can switch the branch to go into my last article about role-based access like a pro. The entire code for the article can be found in the branch caching on the same repo. You can turn on the server by running npm run dev/ node index.

To give you a demo of this functionality, I am creating a class named as MockDatabase which will give us some data in 3 seconds of delay whenever we called the get method of the class

mock database method

here is a code for the mock class we are using, this will help us to maintain the singularity into the base. The get method will give us the data and the wait method will let us create the delay we want here.

Next, we will update the routes, I have created 2 routes for this article's purpose /with-cache and /without-cache which will help us to know the time delay with each.

Don’t worry, you will find the entire source code at the top and the bottom of the article with the repo link.

Now, updating the route's controller to call the DB to get which we created earlier.

We are calling the exact same code to both the function for now.

controller function for the routes

Now, if you move to any route you will see the following result but with the 3-second delay.

Note the highlighted area with the red box, you can see the delay.

Now going to the integration of caching in our small app. We will be using the Redis package to cache our data.

First, you need to download the Redis and install it locally and to use it in the local install Redis package, write command npm i redis

and update them with cache controller like this,

Do not forget to import the Redis at the top.

In the snippet, if you see, we are clearly making code for the diagram I used at the very beginning of the article. First, we are checking if Redis has the data or not, If yes then sending it right to the client, and If no, then calling the DB and storing it and then send the data to the client with the same key we are using to get the data. Note- Redis do not store direct object, first we need to stringify it or making it buffer and If you want to do some processing you can use JSON parse to make it JSON again.

Once you did that you will see the changes again when you refresh the server and hit the request again.

With that being said, We will wrap up here, you can find the entire source code at this branch. Hope you guys like this article and gain some knowledge, Take care.

--

--