DEV Community

vinayak
vinayak

Posted on • Originally published at itsvinayak.hashnode.dev on

Redis Pipelining in NodeJS

Redis Pipelining is a technique that helps to reduce the round-trip time between the Redis server and the client. In this article, we will explore the Redis Pipelining in Node.js.

itsvinayak

Before we start, let's first understand the time between round trips involved in executing Redis commands between the server and client.

The client sends a Redis query to the Redis server.

The Redis server processes this query and responds to the client.

Redis server processes

For each Redis command, this request-response cycle continues.

MULTI Command In Redis

The MULTI command in the Redis allows the execution of multiple Redis commands in a single step.

Syntax

Syntax

Using Redis Multi and Pipeline in NodeJS

In Node.js, Redis clients have two main methods for batching commands and creating transactions. One is the Redis multi command, mentioned above, and the other is the pipeline method.

In this article, we will be using the node-redis package.

Install



npm i redis


Enter fullscreen mode Exit fullscreen mode

Multi with node-redis

The MULTI command in Redis executes a group of commands as atomic transactions to ensure data integrity. This means that all commands in the block are either executed or none are.

Redis example of multi



const redis = require("redis");

const client = redis.createClient();

const execMulti = () => {
  const multi = client.multi();
  multi.set("key1", "value1");
  multi.set("key2", "value2");
  multi.get("key1");
  multi.get("key2");
  const multiExec = multi.exec();

  multiExec
    .then((res) => {
      console.log("Multi commands executed successfully");
      console.log(res);
      client.quit();
    })
    .catch((err) => {
      console.log("Error executing multi commands");
      console.log(err);
    });
};

client.on("error", (err) => {
  console.log("Redis Client Error", err);
});

client
  .connect()
  .then(() => {
    console.log("Connected to Redis");
    execMulti();
  })
  .catch((err) => {
    console.log(err);
  });


Enter fullscreen mode Exit fullscreen mode

Multi commands

Pipeline with node-redis

The pipeline feature of the node-redis client allows batching of multiple commands but does not guarantee atomicity.

Node Redis can automatically group multiple requests made during the same "tick, " meaning if commands are in a single cycle of the event loop, the node-redis client can combine them into a single pipeline.

Auto-Pipelining example in Node Redis



const redis = require("redis");
const client = redis.createClient();

client.on("error", (err) => {
  console.error("Redis client error", err);
});

client
  .connect()
  .then(() => {
    console.log("Connected to Redis");
    Promise.all([
      client.set("key", "value"),
      client.get("key"),
      client.set("key1", 1),
      client.incr("key1"),
      client.get("key1"),
    ])
      .then((results) => {
        console.log("SET", results[0]);
        console.log("GET", results[1]);
        console.log("SET", results[2]);
        console.log("INCR", results[3]);
        console.log("GET", results[4]);
        client.quit();
      })
      .catch((err) => {
        console.error("Error setting/getting from Redis", err);
      });
  })
  .catch((err) => {
    console.error("Error connecting to Redis", err);
  });


Enter fullscreen mode Exit fullscreen mode

Output



Connected to Redis
SET OK
GET value
SET OK
INCR 2
GET 2


Enter fullscreen mode Exit fullscreen mode

Node-redis client also contains execAsPipeline() method for manual batching of redis commands

Example using execAsPipeline method



const redis = require("redis");
const client = redis.createClient();

client.on("error", (err) => {
  console.error("Redis client error", err);
});

client
  .connect()
  .then(async () => {
    console.log("Connected to Redis");
    let pipeline = client.multi();
    pipeline.set("key1", "value1");
    pipeline.set("key2", "value2");
    pipeline.get("key1");
    pipeline.get("key2");
    let result = await pipeline.execAsPipeline();
    console.log(result);
    client.quit();
  })
  .catch((err) => {
    console.error("Error connecting to Redis", err);
  });


Enter fullscreen mode Exit fullscreen mode

output



Connected to Redis
[ 'OK', 'OK', 'value1', 'value2' ]


Enter fullscreen mode Exit fullscreen mode

Top comments (0)