redis / node-redis

Redis Node.js client
https://redis.js.org/
MIT License
16.9k stars 1.89k forks source link

Broadcast commands in the Cluster client #2431

Open uglide opened 1 year ago

uglide commented 1 year ago

Motivation

The Cluster client should provide overrides for specific Redis Core and module commands to make them easy to use with OS Cluster API.

const cluster = createCluster({ rootNodes: [ // ... ] });

cluster.on('error', (err) => console.log('Redis Cluster Error', err));

await cluster.connect();

await cluster.json.set('doc1', '$', { name: 'Alice', age: 32, coins: 100, email: 'alice@nonexist.com' })

await cluster.json.set('doc2', '$', { name: 'Bob', age: 23, coins: 15, email: 'bob@somewhere.gov' })

// Both mget commands should return all documents from cluster console.log(await cluster.json.mGet(['doc1', 'doc2'], '$..name')) console.log(await cluster.json.mGet(['doc2', 'doc1'], '$..name'))



### Basic Code Example

_No response_
leibale commented 1 year ago

https://github.com/redis/node-redis/blob/master/docs/clustering.md#command-routing

uglide commented 1 year ago

@leibale JSON.MGET is a different beast compared to MGET. We don't know the key of documents where the path return us any results. That's why it's helpful to have a high-level wrapper that merges results from all primaries.

leibale commented 1 year ago

Then we need a wrapper like in Graph... executing client.ft.search (or any other command) should be as close as possible to the redis-cli version (with some "type mapping"). "Automagically" running multiple commands when the user executed only one feels wrong.

@chayim @itamarhaber @guyroyse @simonprickett WDUT?

itamarhaber commented 1 year ago

Idk about the implementation, but I'm just saying that where a command should run is a part of the command's docs and modules should use the same API: https://redis.io/docs/reference/command-tips/#request_policy

leibale commented 1 year ago

so something like

const REQUEST_POLICIES = {
  ...
};

const RESPONSE_POLICES = {
  ...
};

// will default to the policy defined in redis
cluster.ping();

// can be overridden
cluster.withPolicy({
  request: ...,
  response: ...
}).ping();