-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Open
Description
Problem
When using WATCH with MULTI/EXEC in cluster mode, the cluster client doesn't have a WATCH method because WATCH requires connection-level state on a specific node. The current workaround requires using internal APIs and calculating slots manually:
const calculateSlot = require("cluster-key-slot");
const slot = calculateSlot(key);
const shard = cluster.slots[slot];
const nodeClient = await cluster.nodeClient(shard.master);
await nodeClient.WATCH(key);
// ... GET, calculate new value ...
const multi = nodeClient.MULTI();
multi.addCommand(["SET", key, value]);
await multi.EXEC();This works but has some issues:
- Requires importing
cluster-key-slotseparately, even though it's already a dependency of@redis/client - The
slotsarray andnodeClientmethod are somewhat poorly documented
Proposed solution
A convenience method on the cluster client that returns the node client for a given key:
const nodeClient = await cluster.getNodeClientForKey(key);
await nodeClient.WATCH(key);
// ...Use case
Atomic read-modify-write operations where the calculation happens in application code:
await client.WATCH(key);
const oldValue = await client.GET(key);
const newValue = calculateNewValue(oldValue); // app logic
const multi = client.MULTI();
multi.SET(key, newValue);
await multi.EXEC(); // returns null if key changed, retryThis is a common pattern that currently requires workarounds in cluster mode.
Environment
@redis/clientversion: 5.11.0- Node.js version: 22.x
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels