1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-09 00:22:08 +03:00
Files
node-redis/docs/client-configuration.md
Samuel CHEMLA 6642278f96 Add 'Network error handling' section to documentation (#2250)
* Add 'Network error handling' section to documentation

* Merge 'Network error handling' section with existing doc

* typo

* Update README.md

* typos

Co-authored-by: Samuel CHEMLA <samuel.chemla@orange.com>
Co-authored-by: Leibale Eidelman <me@leibale.com>
2023-01-24 09:15:14 -05:00

9.6 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 5000 Toggle keep-alive functionality
socket.tls See explanation and examples below
socket.reconnectStrategy retries => Math.min(retries * 50, 500) A function containing the Reconnect Strategy logic
username ACL username (see ACL guide)
password ACL password or the old "--requirepass" password
name Connection 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 a network error occurs the client will automatically try to reconnect, following a default linear strategy (the more attempts, the more waiting before trying to reconnect).

This strategy can be overridden by providing a socket.reconnectStrategy option during the client's creation.

The socket.reconnectStrategy is a function that:

  • Receives the number of retries attempted so far.
  • Returns number | Error:
    • number: wait time in milliseconds prior to attempting a reconnect.
    • Error: closes the client and flushes internal command queues.

The example below shows the default reconnectStrategy and how to override it.

import { createClient } from 'redis';

const client = createClient({
    socket: {
        reconnectStrategy: (retries) => Math.min(retries * 50, 500)
    }
});

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: '...'
  }
});