Node.js Redis Module - GeeksforGeeks

08 Oct, 2021
Redis is an Open Source store for storing data structures. It is used in multiple ways. It is used as a database, cache, and message broker. It can store data structures such as strings, hashes, sets, sorted sets, bitmaps, indexes, and streams.
Redis is very useful for Node.js developers as it reduces the cache size which makes the application more efficient. However, it is very easy to integrate Redis with Node.js applications. 
Prerequisites: Before starting with our application, make sure the following installation in your system:
An IDE of your choice.Redis installed in your system. Check the version that you are using the following command:redis cli --versionNode.js, NPM installed and configured in your system.Basic knowledge of Node.js and Redis.
An IDE of your choice.
Redis installed in your system. Check the version that you are using the following command:redis cli --version
redis cli --version
Node.js, NPM installed and configured in your system.
Basic knowledge of Node.js and Redis.
Getting Started: First initialize the application with package.json file. Type the following command:
npm init
Now, install the redis module by the following command:
npm install redis --save
Now, create a new file and name it as app.js. You can name your file whatever you want. In app.js file, write the following code:
Filename: app.js
var redis = require("redis");var client = redis.createClient();  client.on("connect", function() {  console.log("Connection Successful!!");});
Here, we are importing the redis module and then creating a client for redis. The client is then used to manipulate the module. In the above code, we are creating a server. To run the application just type the following command:
node app.js
The output for the above code will be logged in the console as:
Connection Successful!!
Storing of String: To store a string in Redis, write the following code in app.js file:
var redis = require("redis");var client = redis.createClient();  client.on("connect", function() {  console.log("Connection Successful!!");});  client.set("Intern", "gfg", (err, stu) => {    if (err) console.log(err);    else console.log(stu);});
The client.set() function takes the key-value format. Here, Intern is the key and gfg is the value. Further, it takes a callback function which will return error (if any) or it will log the value as OK if string is successfully stored. The output for the above code will be:
Connection Successful!!
OK
To get the value of the key stored in the redis database, we will use the get function as shown below:
var redis = require("redis");var client = redis.createClient();  client.on("connect", function() {  console.log("Connection Successful!!");});  client.set("Intern", "gfg", (err, stu) => {    if (err) console.log(err);    else console.log(stu);});  client.get('Intern', (err, stu) => {    if (err) console.log(err);    else console.log(stu); });
Here, client.get() method will get the value of the key Intern and log it in the console. The output for the above code will be:
Connection Successful!!
OK
gfg

Storing Objects: To store objects in redis database, write the following code:
var redis = require("redis");var client = redis.createClient();  client.on("connect", function() {  console.log("Connection Successful!!");});  client.set("Intern", "gfg", (err, stu) => {    if (err) console.log(err);    else console.log(stu);});  client.get('Intern', (err, reply) => {    console.log(reply); });  client.hmset("Interns", { pos: "Tech Writer", Org: "GFG" });
Now, the key will be Interns and its value will be { pos: “Tech Writer”, Org: “GFG” }.
To get the value from the redis database, write the following code:
var redis = require("redis");var client = redis.createClient();  client.on("connect", function() {  console.log("Connection Successful!!");});  client.set("Intern", "gfg", (err, stu) => {    if (err) console.log(err);    else console.log(stu);});  client.get('Intern', (err, reply) => {    console.log(reply); })  client.hmset("Interns", { pos: "Tech Writer", Org: "GFG" });  client.hgetall("Interns", (err, stu) => {    if (err) console.log(err);    else console.log(stu);  });
Here, hgetall method is used to get all values of the key and log it in the console. The output for the above code will be:
Connection Successful!!
OK
gfg
{ pos: 'Tech Writer', Org: 'GFG' }

Conclusion: Redis can make Node.js applications fast, powerful, and efficient. Redis can be called as database storage for various data structures.
nidhi_biet
Node.js-Misc
Node.js
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between dependencies, devDependencies and peerDependencies
Mongoose find() Function
How to connect Node.js with React.js ?
Node.js Export Module
Mongoose Populate() Method
Remove elements from a JavaScript Array
Convert a string to an integer in JavaScript
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?
Difference between var, let and const keywords in JavaScript