itsvinayak
itsvinayak

Reputation: 138

Has the pipeline function been removed from node-redis?

I remember using the pipeline function in node-redis to batch commands and execute them together. Is this function now deprecated? Has it been replaced with automatic pipelining within the client?

Here's an example of how I've been using the pipeline function:

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

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

async function usePipelining() {
  try {
    await client.connect();

    // Create a pipeline
    const pipeline = client.pipeline();


    pipeline.set("key1", "value1");
    pipeline.set("key2", "value2");
    pipeline.set("key3", "value3");
    pipeline.get("key1");
    pipeline.get("key2");
    pipeline.get("key3");

    // Execute the pipeline and handle results
    const results = await pipeline.exec();

    console.log("Pipeline results:");
    results.forEach((result, index) => {
      if (result[0]) {
        console.error("Pipeline Error:", result[0]);
      } else {
        console.log(`Result ${index + 1}:`, result[1]);
      }
    });
  } catch (err) {
    console.error("Pipeline Execution Error:", err);
  } finally {
    client.quit();
  }
}

usePipelining().catch((err) => {
  console.error("Unexpected Error:", err);
});
Pipeline Execution Error: TypeError: client.pipeline is not a function
    at usePipelining (/home/vinayak/test/index.cjs:13:29)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
Pipeline Execution Error: TypeError: client.batch is not a function
    at usePipelining (/home/vinayak/test/index.cjs:13:29)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

Can someone clarify if the pipeline function is still the recommended way to batch commands, or should we rely on automatic pipelining features in newer versions of node-redis?

Has client.pipeline() or client.batch() been deprecated?

I know commands that are written on the same "tick" are auto-pipelined in the node-redis package's new version:

const replies = await Promise.all([
  client.get('a'),
  client.get('b')
]);

Environment details:

Upvotes: 0

Views: 183

Answers (1)

itsvinayak
itsvinayak

Reputation: 138

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);
  });

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);
  });

Redis Pipelining in NodeJS

Upvotes: 0

Related Questions