Get your Node.js app ready for production
This guide offers recommendations to get the best reliability and performance in your production environment.
Each item in the checklist below links to the section for a recommendation. Use the checklist icons to record your progress in implementing the recommendations.
{ "$schema": "https://redis.io/schemas/checklist.json", "id": "nodeprodlist", "items": [ { "name": "Handling errors", "position": 1, "url": "#handling-errors" }, { "name": "Handling reconnections", "position": 2, "url": "#handling-reconnections" }, { "name": "Connection timeouts", "position": 3, "url": "#connection-timeouts" }, { "name": "Command execution reliability", "position": 4, "url": "#command-execution-reliability" }, { "name": "Smart client handoffs", "position": 5, "url": "#seamless-client-experience" } ], "type": "checklist" }- [ ] [Handling errors](#handling-errors) - [ ] [Handling reconnections](#handling-reconnections) - [ ] [Connection timeouts](#connection-timeouts) - [ ] [Command execution reliability](#command-execution-reliability) - [ ] [Smart client handoffs](#seamless-client-experience)
Node-Redis provides multiple events to handle various scenarios, among which the most critical is the error event.
This event is triggered whenever an error occurs within the client, and it is very important to set a handler to listen for it. See Error events for more information and an example of setting an error handler.
When the socket closes unexpectedly (without calling the quit() or disconnect() methods),
the client can automatically restore the connection. A simple
exponential backoff strategy
for reconnection is enabled by default, but you can replace this with your
own custom strategy. See
Reconnect after disconnection
for more information.
To set a timeout for a connection, use the connectTimeout option
(the default timeout is 5 seconds):
const client = createClient({
socket: {
// setting a 10-second timeout
connectTimeout: 10000 // in milliseconds
}
});
client.on('error', error => console.error('Redis client error:', error));
You can also set timeouts for individual commands using AbortController:
import { createClient, commandOptions } from 'redis';
const client = createClient({ url: 'redis://localhost:6379' });
await client.connect();
const ac = new AbortController();
const t = setTimeout(() => ac.abort(), 1000);
try {
const val = await client.get(commandOptions({ signal: ac.signal }), key);
} finally {
clearTimeout(t);
}
By default, node-redis reconnects automatically when the connection is lost
(but see Handling reconnections, if you want to
customize this behavior). While the connection is down, any commands that you
execute will be queued and sent to the server when the connection is restored.
This might occasionally cause problems if the connection fails while a
non-idempotent command
is being executed. In this case, the command could change the data on the server
without the client removing it from the queue. When the connection is restored,
the command will be sent again, resulting in incorrect data.
If you need to avoid this situation, set the disableOfflineQueue option
to true when you create the client. This will cause the client to discard
unexecuted commands rather than queuing them:
const client = createClient({
disableOfflineQueue: true,
.
.
});
Use a separate connection with the queue disabled if you want to avoid queuing only for specific commands.
Smart client handoffs (SCH) is a feature of Redis Cloud and Redis Enterprise servers that lets them actively notify clients about planned server maintenance shortly before it happens. This lets a client take action to avoid disruptions in service.
See Smart client handoffs for more information about SCH and Connect using Smart client handoffs for example code.