Redis is based on a typical client-server model. In other words, it uses a request-response approach to handle requests. With Redis Pipeline, we can improve upon the request processing aspects. In this post, we will learn how to create a Redis Pipeline in NodeJS using the popular IORedis package.

In case you are new to Redis, it is an open-source and in-memory key-value store data store. Redis is also a very popular caching solution.

1 – What is a Redis Pipeline?

Redis Pipeline helps transmit multiple commands to the Redis server. By using a Redis Pipeline, we don’t need to wait for a reply from the server. Instead, we can continue sending more requests. In Redis Pipelining approach, all the requests are sent to the server in one transmission or network request. This approach is very convenient for batch processing. It also saves network bandwidth and reduces RTT or Round Trip Time.

A Redis Pipeline is atomic. In other words, each command in the Redis pipeline is executed individually. Basically, Redis does not stop execution of a particular command to execute another command. Every command finishes without interleaving with other commands.

beenhere

INFO

A Redis client uses pipelining to group several commands. However, the server has to queue the responses in memory. Therefore, if we need to send a very large number of commands, it is better to batch the commands in chunks. We can send a batch of commands, read the replies and then send the next batch. This will help manage the memory efficiently without affecting the overall processing speed.

2 – What is IORedis?

IORedis is a popular Redis client for NodeJS. Using IORedis, we can connect our NodeJS applications to a Redis instance. The NodeJS IORedis client supports all the Redis features such as Pipelining, PubSub, Functions and so on.

To get started with IORedis, we first need to have NodeJS installed on our machine. You can download NodeJS from the official website and install the same.

Once the NodeJS installation is done, we need to create a project and install the IORedis NPM package.

$ mkdir nodejs-redis-pipeline
$ npm init -y
$ npm install ioredis

While installation of packages, if you face NPM write access issue, check out this post that describes the solution.

3 – NodeJS Redis Pipeline Example using IORedis

To demo NodeJS Redis Pipeline, we will create a file named index.js and paste the below contents within the file.

const Redis = require('ioredis');

const redis = new Redis()

async function main() {
    const pipeline = redis.pipeline()

    pipeline.set('foo', 'bar')
    pipeline.get('foo')
    pipeline.set('count', 1)
    pipeline.incr('count')
    pipeline.get('count')
    pipeline.del('foo')

    const results = await pipeline.exec()

    console.log(results)
}

(async () => {
    await main()
})()

In line 3, we use IORedis package to connect to a Redis instance. By default, it will connect to Redis instance on localhost port 6379. You can use Docker to create a Redis instance or install it locally.

In line 6, we create a NodeJS Redis pipeline instance by calling the pipeline() function.

Once we have the instance, we can append commands to the pipeline. For example, below are the commands.

pipeline.set('foo', 'bar')
pipeline.get('foo')
pipeline.set('count', 1)
pipeline.incr('count')
pipeline.get('count')
pipeline.del('foo')

As you can see, each command performs a specific action. However, they are not sent to the Redis server as yet.

Finally, we can send the commands by using the exec() function. Since it is an async operation, we use await to make the call.

Redis will execute the commands within the pipeline and return the results in an array. See below the contents of the results array.

[
  [ null, 'OK' ],
  [ null, 'bar' ],
  [ null, 'OK' ],
  [ null, 2 ],
  [ null, '2' ],
  [ null, 1 ]
]

For the set() operations, we get the response code of OK. For the other operations such as get() and incr(), we get the value of the key.

Conclusion

Redis Pipelining is a great approach to improve the performance of your application. NodeJS Redis Pipeline using IORedis makes it extremely easy to create Redis pipelines and execute commands.

If you have any comments or queries about this post, please feel free to mention them in the comments section below.


Saurabh Dashora

Saurabh is a Software Architect with over 12 years of experience. He has worked on large-scale distributed systems across various domains and organizations. He is also a passionate Technical Writer and loves sharing knowledge in the community.

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *