From a6f635a7e043c22190e4de2984648c2d4b834b6c Mon Sep 17 00:00:00 2001 From: Andy Stark Date: Thu, 8 May 2025 17:05:49 +0100 Subject: [PATCH] DOC-5219 implemented suggested node-redis updates for v5.0.0 --- content/develop/clients/nodejs/connect.md | 33 +++++++++----- content/develop/clients/nodejs/queryjson.md | 6 +-- content/develop/clients/nodejs/transpipe.md | 48 +++++++++++++-------- content/develop/clients/nodejs/vecsearch.md | 6 ++- 4 files changed, 60 insertions(+), 33 deletions(-) diff --git a/content/develop/clients/nodejs/connect.md b/content/develop/clients/nodejs/connect.md index 9857f47878..6537a2f954 100644 --- a/content/develop/clients/nodejs/connect.md +++ b/content/develop/clients/nodejs/connect.md @@ -94,7 +94,7 @@ await cluster.set('foo', 'bar'); const value = await cluster.get('foo'); console.log(value); // returns 'bar' -await cluster.quit(); +await cluster.close(); ``` ## Connect to your production Redis with TLS @@ -123,15 +123,21 @@ await client.set('foo', 'bar'); const value = await client.get('foo'); console.log(value) // returns 'bar' -await client.disconnect(); +await client.destroy(); ``` You can also use discrete parameters and UNIX sockets. Details can be found in the [client configuration guide](https://github.com/redis/node-redis/blob/master/docs/client-configuration.md). ## Reconnect after disconnection -By default, `node-redis` doesn't attempt to reconnect automatically when -the connection to the server is lost. However, you can set the +`node-redis` can attempt to reconnect automatically when +the connection to the server is lost. By default, it will retry +the connection using an +[exponential backoff](https://en.wikipedia.org/wiki/Exponential_backoff) +strategy with some random "jitter" added to avoid multiple +clients retrying in sync with each other. + +You can also set the `socket.reconnectionStrategy` field in the configuration to decide whether to try to reconnect and how to approach it. Choose one of the following values for `socket.reconnectionStrategy`: @@ -159,19 +165,18 @@ from the function can be any of the following: no attempt was made to reconnect. The example below shows a `reconnectionStrategy` function that implements a -custom [exponential backoff](https://en.wikipedia.org/wiki/Exponential_backoff) -strategy: +custom exponential backoff strategy: ```js createClient({ socket: { reconnectStrategy: retries => { - // Generate a random jitter between 0 – 200 ms: - const jitter = Math.floor(Math.random() * 200); + // Generate a random jitter between 0 – 100 ms: + const jitter = Math.floor(Math.random() * 100); - // Delay is an exponential back off, (times^2) * 50 ms, with a - // maximum value of 2000 ms: - const delay = Math.min(Math.pow(2, retries) * 50, 2000); + // Delay is an exponential backoff, (2^retries) * 50 ms, with a + // maximum value of 3000 ms: + const delay = Math.min(Math.pow(2, retries) * 50, 3000); return delay + jitter; } @@ -193,6 +198,12 @@ related to connection: parameter. This is usually a network issue such as "Socket closed unexpectedly". - `reconnecting`: (No parameters) The client is about to try reconnecting after the connection was lost due to an error. +- `sharded-channel-moved`: The cluster slot of a subscribed + [sharded pub/sub channel]({{< relref "/develop/interact/pubsub#sharded-pubsub" >}}) + has been moved to another shard. Note that when you use a + [`RedisCluster`](#connect-to-a-redis-cluster) connection, this event is automatically + handled for you. See + [`sharded-channel-moved` event](https://github.com/redis/node-redis/blob/master/docs/pub-sub.md#sharded-channel-moved-event) for more information. Use code like the following to respond to these events: diff --git a/content/develop/clients/nodejs/queryjson.md b/content/develop/clients/nodejs/queryjson.md index 5d5873a959..085a44a19d 100644 --- a/content/develop/clients/nodejs/queryjson.md +++ b/content/develop/clients/nodejs/queryjson.md @@ -34,9 +34,9 @@ Add the following dependencies: ```js import { createClient, - SchemaFieldTypes, - AggregateGroupByReducers, - AggregateSteps, + SCHEMA_FIELD_TYPE, + FT_AGGREGATE_GROUP_BY_REDUCERS, + FT_AGGREGATE_STEPS, } from 'redis'; ``` diff --git a/content/develop/clients/nodejs/transpipe.md b/content/develop/clients/nodejs/transpipe.md index 27d341a4cd..b396ddca98 100644 --- a/content/develop/clients/nodejs/transpipe.md +++ b/content/develop/clients/nodejs/transpipe.md @@ -156,27 +156,39 @@ console.log(updatedPath); ``` In an environment where multiple concurrent requests are sharing a connection -(such as a web server), you must wrap the above transaction logic in a call to -`client.executeIsolated` to get an isolated connection, like so: +(such as a web server), you must use a connection pool to get an isolated connection, +as shown below: ```js -await client.executeIsolated(async (client) => { - await client.watch('shellpath'); - // ... -}) +import { createClientPool } from 'redis'; + +const pool = await createClientPool() + .on('error', err => console.error('Redis Client Pool Error', err)); + +try { + await pool.execute(async client => { + await client.watch('key'); + + const multi = client.multi() + .ping() + .get('key'); + + if (Math.random() > 0.5) { + await client.watch('another-key'); + multi.set('another-key', await client.get('another-key') / 2); + } + + return multi.exec(); + }); +} catch (err) { + if (err instanceof WatchError) { + // the transaction aborted + } +} ``` This is important because the server tracks the state of the WATCH on a per-connection basis, and concurrent WATCH and MULTI/EXEC calls on the same -connection will interfere with one another. - -You can configure the size of the isolation pool when calling `createClient`: - -```js -const client = createClient({ - isolationPoolOptions: { - min: 1, - max: 100, - }, -}) -``` +connection will interfere with one another. See +[`RedisClientPool`](https://github.com/redis/node-redis/blob/master/docs/pool.md) +for more information. diff --git a/content/develop/clients/nodejs/vecsearch.md b/content/develop/clients/nodejs/vecsearch.md index 798d13d21c..be3de0084a 100644 --- a/content/develop/clients/nodejs/vecsearch.md +++ b/content/develop/clients/nodejs/vecsearch.md @@ -50,7 +50,11 @@ In your JavaScript source file, import the required classes: ```js import * as transformers from '@xenova/transformers'; -import {VectorAlgorithms, createClient, SchemaFieldTypes} from 'redis'; +import { + VectorAlgorithms, + createClient, + SCHEMA_FIELD_TYPE, +} from 'redis'; ``` The `@xenova/transformers` module handles embedding models. This example uses the