Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix(cluster): hash autopipelined Buffer keys by their bytes
Auto-pipelining built the slot key with a template string, which
decodes a Buffer key as UTF-8. Bytes that are not valid UTF-8 become
U+FFFD, so different binary keys could share an auto pipeline even when
their slots are served by different nodes, and Pipeline#exec rejected
the batch. Hash Buffer keys from their bytes (with keyPrefix prepended),
as Command does.

Ported from redis/ioredis#2208.

Signed-off-by: Jacob Ley <jacobtley@gmail.com>
  • Loading branch information
JacobLey committed Oct 22, 2026
commit cadff3f8d2b46ffe55b92142096f4e96e417fdf0
17 changes: 11 additions & 6 deletions lib/autoPipelining.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,17 @@ export function executeWithAutoPipelining(
// If we have slot information, we can improve routing by grouping slots served by the same subset of nodes
// Note that the first value in args may be a (possibly empty) array.
// ioredis will only flatten one level of the array, in the Command constructor.
const prefix = client.options.keyPrefix || "";
const slotKey = client.isCluster
? client.slots[
calculateSlot(`${prefix}${getFirstValueInFlattenedArray(args)}`)
].join(",")
: "main";
let slotKey = "main";
if (client.isCluster) {
const prefix = client.options.keyPrefix || "";
const key = getFirstValueInFlattenedArray(args);
// Hash Buffer keys by their bytes, as Command#getSlot does; decoding
// them as UTF-8 can change the slot (ported from redis/ioredis#2208).
const slot = Buffer.isBuffer(key)
? calculateSlot(Buffer.concat([Buffer.from(prefix), key]))
: calculateSlot(`${prefix}${key}`);
slotKey = client.slots[slot].join(",");
}

if (!client._autoPipelines.has(slotKey)) {
const pipeline = client.pipeline();
Expand Down
26 changes: 26 additions & 0 deletions test/functional/cluster/autopipelining.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ describe("autoPipelining for cluster", () => {
return "bar6";
}

// Buffer.from([0xfe]) hashes to slot 3793.
if (argv[0] === "get" && argv[1] === "\ufffd") {
return "binary-fe";
}

if (argv[0] === "get" && argv[1] === "baz:foo10") {
return "bar10";
}
Expand All @@ -53,6 +58,11 @@ describe("autoPipelining for cluster", () => {
if (argv[0] === "get" && argv[1] === "foo4") {
return "bar4";
}

// Buffer.from([0xff]) hashes to slot 7920.
if (argv[0] === "get" && argv[1] === "\ufffd") {
return "binary-ff";
}
});

new MockServer(30003, (argv) => {
Expand Down Expand Up @@ -219,6 +229,22 @@ describe("autoPipelining for cluster", () => {
cluster.disconnect();
});

it("should bucket binary Buffer keys by their raw bytes", async () => {
const cluster = new Cluster(hosts, { enableAutoPipelining: true });
await new Promise((resolve) => cluster.once("connect", resolve));

// Both keys decode to "\ufffd" as UTF-8 strings, but their bytes hash to
// slots served by different nodes.
expect(
await Promise.all([
cluster.get(Buffer.from([0xff])),
cluster.get(Buffer.from([0xfe])),
])
).to.eql(["binary-ff", "binary-fe"]);

cluster.disconnect();
});

it("should support commands queued after a pipeline is already queued for execution", (done) => {
const cluster = new Cluster(hosts, { enableAutoPipelining: true });

Expand Down