You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-08-03 04:01:40 +03:00
* Default reconnect strategy uses exponential backoff and jitter Both are recommended parts of client reconnect strategies to prevent thundering herd problems when many clients lose their connection at once (for example, during a Redis upgrade). * Move default retry strategy to constant * Plain english explanation of default 'socket.reconnectStrategy' * Extract default connect strategy into helper function
10 KiB
10 KiB
createClient
configuration
Property | Default | Description |
---|---|---|
url | redis[s]://[[username][:password]@][host][:port][/db-number] (see redis and rediss IANA registration for more details) |
|
socket | Socket connection properties. Unlisted net.connect properties (and tls.connect ) are also supported |
|
socket.port | 6379 |
Redis server port |
socket.host | 'localhost' |
Redis server hostname |
socket.family | 0 |
IP Stack version (one of 4 | 6 | 0 ) |
socket.path | Path to the UNIX Socket | |
socket.connectTimeout | 5000 |
Connection timeout (in milliseconds) |
socket.noDelay | true |
Toggle Nagle's algorithm |
socket.keepAlive | true |
Toggle keep-alive functionality |
socket.keepAliveInitialDelay | 5000 |
If set to a positive number, it sets the initial delay before the first keepalive probe is sent on an idle socket |
socket.tls | See explanation and examples below | |
socket.reconnectStrategy | Exponential backoff with a maximum of 2000 ms; plus 0-200 ms random jitter. | A function containing the Reconnect Strategy logic |
username | ACL username (see ACL guide) | |
password | ACL password or the old "--requirepass" password | |
name | Client name (see CLIENT SETNAME ) |
|
database | Redis database number (see SELECT command) |
|
modules | Included Redis Modules | |
scripts | Script definitions (see Lua Scripts) | |
functions | Function definitions (see Functions) | |
commandsQueueMaxLength | Maximum length of the client's internal command queue | |
disableOfflineQueue | false |
Disables offline queuing, see FAQ |
readonly | false |
Connect in READONLY mode |
legacyMode | false |
Maintain some backwards compatibility (see the Migration Guide) |
isolationPoolOptions | See the Isolated Execution Guide | |
pingInterval | Send PING command at interval (in ms). Useful with "Azure Cache for Redis" |
Reconnect Strategy
When the socket closes unexpectedly (without calling .quit()
/.disconnect()
), the client uses reconnectStrategy
to decide what to do. The following values are supported:
false
-> do not reconnect, close the client and flush the command queue.number
-> wait forX
milliseconds before reconnecting.(retries: number, cause: Error) => false | number | Error
->number
is the same as configuring anumber
directly,Error
is the same asfalse
, but with a custom error.
By default the strategy uses exponential backoff, but it can be overwritten like so:
createClient({
socket: {
reconnectStrategy: retries => {
// Generate a random jitter between 0 – 200 ms:
const jitter = Math.floor(Math.random() * 200);
// 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);
return delay + jitter;
}
}
});
TLS
To enable TLS, set socket.tls
to true
. Below are some basic examples.
For configuration options see tls.connect and tls.createSecureContext, as those are the underlying functions used by this library.
Create a SSL client
createClient({
socket: {
tls: true,
ca: '...',
cert: '...'
}
});
Create a SSL client using a self-signed certificate
createClient({
socket: {
tls: true,
rejectUnauthorized: false,
cert: '...'
}
});