Fasten your Node JS Application with a Powerful Caching Mechanism using Redis
Why caching is so important:
Caching is the process of storing copies of files and data in a cache or some temporary space. By the use of caching, we fetch data very quickly. The best example of a caching tool is Cloudflare. I know Cloudflare is not the only caching tool, it is having other functionality also.
There are two main reasons for using caching in the application:
- Save the cost. Less amount of data sent over the network.
- Less response time.
Process of Caching:
The process of caching is so simple. Suppose you want to get some data from an API. In the first call, you will hit the API and get the data after that you will set or save that data in cache. In the future, if you want to get the same data, you don’t need to hit the API. First, you will check the data available in cache or not. If data available in the cache, then you will fetch data from the cache otherwise you will hit the API.
In this tutorial, first, we will get the data from fake API without Redis and then with Redis and check the difference of response time with visual representation.
Create a simple server in Express JS:
If you are new to the NodeJS world then please read my tutorial. In this tutorial, you will learn how to create an express js server and CRUD operations. Otherwise, follow the below steps to create a simple server in express js.
Create the package.json file:
npm init -y
The above command will create the package.json file.
Installing required packages:
npm install --save express axios
The Express JS is the framework of Node JS and Axios is a promise-based HTTP client for the browser and node.js
Create a simple server by using the below code snippet:
Create an index.js file in the root folder and write the below code in the file.
Before running the server, please add the start script inside under scripts section in the package.json file
Run the below command to boot up the server
Fetching data from Fake API:
In this section, we will fetch data from a fake API called: JSONPlaceholder. This API is very useful for fetching fake data.
In this API call, we will fetch user data by supplying the email address. Please refer to the below code:
In the above code, we require the Axios package and create MOCK_API for fake API URL. In the API call, we are passing email as params and filter out the details of the user and get it in the response.
I’m using the POSTMAN tool from calling the REST API calls. In the above screenshot, we are clearly able to see the response time which is 1164 ms
Introduction to Redis:
Redis is an open-source in-memory datastore, used as a database, message broker, and cache. Redis is well-known for its powerful cache management due to that it is very fast.
Other features of the Redis database are:
- Automatic failover
tar xvzf redis-stable.tar.gz
After that, you can run the command
Creating the API with Redis:
First, we need to install the redis package by running the below command:
npm install --save redis
After installing the Redis package, we need to install and create the Redis client.
const redis = require('redis');
const redisClient = redis.createClient(6379);
The default port of Redis is 6379
After that, we created one route with the Redis database. In this route first, we will check the data is available in Redis or not. If data is available in Redis then it's fetching from Redis otherwise it's fetching from API.
Please note that this is only a small piece of work on how to cache the API result in Node JS with Redis. If you want to increase your knowledge about Redis, please read the documentation of Redis.
If you want to access the whole code please check my Github repo and give me a start and fork it for using the code.
Any freelance work, please give me a chance
If you have any freelance work or projects. Please connect with me or drop an email at email@example.com.