DEV Community

Shubham_Baghel
Shubham_Baghel

Posted on

Redis with Nodejs

Introduction

Redis is an in-memory data store that can be used as a database, cache and message broker. The main benefits of using Redis is its speed. Because it's an in-memory database, it can read and write data very quickly with feature like pipeline and transactions, making it a great choice for applications that need to process large volumes of data in real-time. Redis also supports a variety of data structures, including strings, hashes, lists, sets, and sorted sets. This makes it very versatile and useful for a wide range of applications.

Redis

To use Redis with node.js,you will need to install "redis" package,We can installed it using below command:

npm install redis

After this lets create Redis client using redis.createclient.This will create a new client that connects to default redis server on localhost:6379.Also we can pass configuration objectin createclient method:


const client = redis.createClient({
    socket: {
        host: '<hostname>',
        port: '<port>'
    },
    password: '<password>'
});
Enter fullscreen mode Exit fullscreen mode

Installation on windows:
Redis installation step for windows

Redis installation step for macOs

Now lets see how we can use client.set() to set a key-value pair in redis,The first argument is key which will be any filed that we need to store and will be consider as KEY and second argument here is actual value and third is callback function that gets executed or called once the execution is complete.


// Set a key-value pair in Redis
client.set('name', 'devtoUser', (error, result) => {
  if (error) {
    console.error(error);
  } else {
    console.log(result); // this will return OK
  }
});

Enter fullscreen mode Exit fullscreen mode

Finally, we use client.get() to get the value of the namekey from Redis.The first argument is the key, and the second argument is a callback function that gets called once the operation is complete. In this example, we log the result to the console if there are no errors.

// Get a value from Redis
client.get('name', (error, result) => {
  if (error) {
    console.error(error);
  } else {
    console.log(result); //this will return devtoUser
  }
});
Enter fullscreen mode Exit fullscreen mode

Redis benefits

Redis also has a number of advanced features that make it a powerful tool for developers. Some of these features include:

  1. Pub/Sub messaging: Redis can be used as a message broker, allowing applications to publish and subscribe to messages. This makes it useful for building real-time applications, such as chat apps or real-time analytics systems.

  2. Lua scripting: Redis has a built-in Lua interpreter, which allows developers to write custom scripts that can be executed on the server. This can be useful for complex data operations that can't be easily performed using built-in Redis commands.

  3. Transactions: Redis supports transactions, allowing developers to group multiple commands together into a single atomic operation. This can help ensure data consistency and prevent race conditions.

  4. Persistence: Redis can be configured to periodically save its data to disk, ensuring that data is not lost if the server crashes or is restarted.

Github Repo:
Redis repo

Additional Radis commands :
Redis commands list

Top comments (1)

Collapse
 
thomasbnt profile image
Thomas Bnt ☕

Hello ! Don't hesitate to put colors on your codeblock like this example for have to have a better understanding of your code 😎

console.log('Hello world!');
Enter fullscreen mode Exit fullscreen mode

Example of how to add colors and syntax in codeblocks