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
fix(cluster): hash autopipelined keys by their bytes under a Buffer k…
…eyPrefix

keyPrefix is typed as a string, but a Buffer prefix is supported: keys are
sent prefixed with its bytes. Auto-pipelining joined a Buffer prefix and a
string key with a template string, which decodes the prefix as UTF-8, so a
prefix that isn't valid UTF-8 could put commands for different nodes in
one pipeline. Hash the bytes whenever the key or the prefix is a Buffer.

Ported from redis/ioredis#2243.

Signed-off-by: Jacob Ley <jacobtley@gmail.com>
  • Loading branch information
JacobLey committed Oct 22, 2026
commit e732339710c30f9944070cbf721b47c88d808d55
16 changes: 11 additions & 5 deletions lib/autoPipelining.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ export function getFirstValueInFlattenedArray(
return undefined;
}

function toBytes(value: string | Buffer | number | null | undefined): Buffer {
return Buffer.isBuffer(value) ? value : Buffer.from(String(value));
}

export function executeWithAutoPipelining(
client,
functionName: string,
Expand Down Expand Up @@ -149,11 +153,13 @@ export function executeWithAutoPipelining(
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}`);
// Hash Buffer keys and prefixes by their bytes, as Command#getSlot does;
// decoding them as UTF-8 can change the slot (ported from
// redis/ioredis#2208 and redis/ioredis#2243).
const slot =
Buffer.isBuffer(key) || Buffer.isBuffer(prefix)
? calculateSlot(Buffer.concat([toBytes(prefix), toBytes(key)]))
: calculateSlot(`${prefix}${key}`);
slotKey = client.slots[slot].join(",");
}

Expand Down
35 changes: 35 additions & 0 deletions test/unit/autoPipelining.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { expect } from "chai";
import * as sinon from "sinon";
import Pipeline from "../../lib/Pipeline";
import Redis from "../../lib/Redis";
import { getFirstValueInFlattenedArray } from "../../lib/autoPipelining";

describe("autoPipelining", function () {
Expand Down Expand Up @@ -26,4 +29,36 @@ describe("autoPipelining", function () {
// @ts-expect-error
expectGetFirstValueIs([createArguments("")], "");
});

it("should bucket string keys under a Buffer keyPrefix by their bytes", async function () {
const redis = new Redis(30000, {
enableAutoPipelining: true,
lazyConnect: true,
// keyPrefix is typed as a string, but a Buffer is supported
keyPrefix: Buffer.from([0xff]) as unknown as string,
});
Object.defineProperty(redis, "isCluster", { value: true });
// The prefixed keys' bytes hash to slots 12000 (node a) and 7811 (node
// b). Every other slot is node b's, so only hashing those bytes splits
// them.
(redis as any).slots = new Array(16384).fill(["b"]);
(redis as any).slots[12000] = ["a"];
(redis as any)._groupsBySlot = new Array(16384);
// Only the buckets are read here; the pipelines themselves are not run.
const exec = sinon.stub(Pipeline.prototype, "exec").callsFake(function () {
return Promise.resolve([]);
});

try {
(redis as any).get("k0").catch(() => {});
(redis as any).get("k3").catch(() => {});
// One pipeline per node.
expect([...(redis as any)._autoPipelines.keys()]).to.have.lengthOf(2);
// Let the pipelines scheduled above run while exec is still stubbed.
await new Promise((resolve) => setImmediate(resolve));
} finally {
exec.restore();
redis.disconnect();
}
});
});