Creating blazing fast search API with NodeJS and Redis
By: Coding In Depth
Tags: Search, Cache, Nodejs, Redis

I am working on a project where user information is very important and shared across different projects. This created user data availability very high. For high availability, I implemented a Redis cache. For the key-value search, Redis works like charm.

Redis local server up and running:

Install Redis server locally, Just follow the instruction on the below link download server and then run the redis-server command. redis

Make sure your system is updated. Also, install make. Make is very helpful specially when you want to test Redis build. Also check the command make test to check the build is successful or not. It will show that all test is passed.

Open Redis-cli and you can play around in console. Do ping <message> and get the message in response. Also you can set key-value pairs directly from cli command line. It is very helpful for the developer.

2. Installing Redis and Create NodeJS API

Install redis sdk for your microservices. Do npm i redis. OR use yarn in case of yarn. Now you can use redis sdk using require.

//command line

npm i redis //redis.js const redis = require("redis"),

client = redis.createClient({});

Create a redis.js file in the microservices utility folder(optional step). So here is the code. This code will create a client for NodeJs service that will connect with the Redis local server. If there is no parameter passed in the connection, meaning if it is empty object then it will be connected with local Redis server. Redis support local 6379 port. However, you can pass AWS credentials host, port details, etc for connecting to AWS cloud. Below is the code snippet for redis.js

Now we are creating a user api that adds user details i.e. userid, first name, last name, email id, and phone number. Import redisClient from redis-cli.js above. redisClient.hmset with id.

Now we can search for users based on their id. And this search is blazing fast even after 10K records. Import redisClient from redis-cli.js. redisClient.hgdetails with the key that we have added.

Create a frontend module, I have created a user component in my angular application. You can use postman, another client, or Redis client for these CRUD operations. I will explain how to perform these simple operations directly from redis cli in the later part of the article. First, add userid with user details here. Remember user id is very important. This is the user id which we use to invoke, search using search api. After save, search with the same key and you will get the desired result. That means our redis is up and working like a charm.

Now the other operation updates and delete. The update is very similar and can be done similarly to save. I am writing here delete api. Now below code snippet import redisClient from redis-cli.js. Then call redisClient.del and pass id to delete.

If your frontend is not ready just follow step 1 and run the redis-cli command and directly add an array of user objects using the command line. Redis command line is good, it provides very good IntelliSense. As soon as you start writing command it keeps informing you about the key to adding and a bunch of other information. The below screen is useful please check it.

Conclusion: We have seen in this article how to run a simple user API with CRUD operations. We have started with installing redis locally. After installation running server and client. We have seen how to create redis client for NodeJS and connecting to local as well as cloud server. We have also created create, search and delete API’s for redis. At last, we have also checked how to query similar function using cli.

You can download the complete project from github.

This article was written in Express Js framework. Check The complete article of creating NodeJS using TypeScript.