Production usage
Get your Node.js app ready for production
This guide offers recommendations to get the best reliability and performance in your production environment.
Checklist
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.
Recommendations
Handling errors
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.
It is crucial to listen for error events.
If a client does not register at least one error listener and an error occurs, the system will throw that error, potentially causing the Node.js process to exit unexpectedly. See the EventEmitter docs for more details.
const client = createClient({
// ... client options
});
// Always ensure there's a listener for errors in the client to prevent process crashes due to unhandled errors
client.on('error', error => {
console.error(`Redis client error:`, error);
});
Handling reconnections
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.
Timeouts
To set a timeout for a connection, use the connectTimeout
option:
const client = createClient({
socket: {
// setting a 10-second timeout
connectTimeout: 10000 // in milliseconds
}
});
client.on('error', error => console.error('Redis client error:', error));