# Production usage

```json metadata
{
  "title": "Production usage",
  "description": "Get your Node.js app ready for production",
  "categories": ["docs","develop","stack","oss","rs","rc","oss","kubernetes","clients"],
  "tableOfContents": {"sections":[{"id":"checklist","title":"Checklist"},{"children":[{"id":"handling-errors","title":"Handling errors"},{"id":"handling-reconnections","title":"Handling reconnections"},{"id":"timeouts","title":"Timeouts"},{"id":"command-execution-reliability","title":"Command execution reliability"},{"id":"smart-client-handoffs","title":"Smart client handoffs"}],"id":"recommendations","title":"Recommendations"}]}

,
  "codeExamples": []
}
```
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.

```checklist {id="nodeprodlist"}
- [ ] [Handling errors](#handling-errors)
- [ ] [Handling reconnections](#handling-reconnections)
- [ ] [Connection timeouts](#connection-timeouts)
- [ ] [Command execution reliability](#command-execution-reliability)
- [ ] [Smart client handoffs](#seamless-client-experience)
```

## Recommendations

### Handling errors

Node-Redis provides [multiple events to handle various scenarios](https://github.com/redis/node-redis?tab=readme-ov-file#events), 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](https://redis.io/docs/latest/develop/clients/nodejs/error-handling#error-events)
for more information and an example of setting an error handler.

### 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](https://en.wikipedia.org/wiki/Exponential_backoff) strategy
for reconnection is enabled by default, but you can replace this with your
own custom strategy. See
[Reconnect after disconnection](https://redis.io/docs/latest/develop/clients/nodejs/connect#reconnect-after-disconnection)
for more information.

### Timeouts

To set a timeout for a connection, use the `connectTimeout` option
(the default timeout is 5 seconds):

```js
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`:

```javascript
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);
}
```

### Command execution reliability

By default, `node-redis` reconnects automatically when the connection is lost
(but see [Handling reconnections](#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](https://en.wikipedia.org/wiki/Idempotence) 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:

```js
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

*Smart client handoffs (SCH)* is a feature of Redis Cloud and
Redis Software 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](https://redis.io/docs/latest/develop/clients/sch)
for more information about SCH and
[Connect using Smart client handoffs](https://redis.io/docs/latest/develop/clients/nodejs/connect#connect-using-smart-client-handoffs-sch)
for example code.

