@@ -27,7 +27,8 @@ progress in implementing the recommendations.
27
27
{{< checklist "nodeprodlist" >}}
28
28
{{< checklist-item "#handling-errors" >}}Handling errors{{< /checklist-item >}}
29
29
{{< checklist-item "#handling-reconnections" >}}Handling reconnections{{< /checklist-item >}}
30
- {{< checklist-item "#timeouts" >}}Timeouts{{< /checklist-item >}}
30
+ {{< checklist-item "#connection-timeouts" >}}Connection timeouts{{< /checklist-item >}}
31
+ {{< checklist-item "#command-execution-reliability" >}}Command execution reliability{{< /checklist-item >}}
31
32
{{< /checklist >}}
32
33
33
34
## Recommendations
@@ -63,10 +64,12 @@ own custom strategy. See
63
64
[ Reconnect after disconnection] ({{< relref "/develop/clients/nodejs/connect#reconnect-after-disconnection" >}})
64
65
for more information.
65
66
66
- ### Timeouts
67
+ ### Connection timeouts
67
68
68
- To set a timeout for a connection, use the ` connectTimeout ` option:
69
- ``` typescript
69
+ To set a timeout for a connection, use the ` connectTimeout ` option
70
+ (the default timeout is 5 seconds):
71
+
72
+ ``` js
70
73
const client = createClient ({
71
74
socket: {
72
75
// setting a 10-second timeout
@@ -76,5 +79,29 @@ const client = createClient({
76
79
client .on (' error' , error => console .error (' Redis client error:' , error));
77
80
```
78
81
79
- ### Retries
82
+ ### Command execution reliability
83
+
84
+ By default, ` node-redis ` reconnects automatically when the connection is lost
85
+ (but see [ Handling reconnections] ( #handling-reconnections ) , if you want to
86
+ customize this behavior). While the connection is down, any commands that you
87
+ execute will be queued and sent to the server when the connection is restored.
88
+ This might occasionally cause problems if the connection fails while a
89
+ [ non-idempotent] ( https://en.wikipedia.org/wiki/Idempotence ) command
90
+ is being executed. In this case, the command could change the data on the server
91
+ without the client removing it from the queue. When the connection is restored,
92
+ the command will be sent again, resulting in incorrect data.
93
+
94
+ If you need to avoid this situation, set the ` disableOfflineQueue ` option
95
+ to ` true ` when you create the client. This will cause the client to discard
96
+ unexecuted commands rather than queuing them:
97
+
98
+ ``` js
99
+ const client = createClient ({
100
+ disableOfflineQueue: true ,
101
+ .
102
+ .
103
+ });
104
+ ```
80
105
106
+ Use a separate connection with the queue disabled if you want to avoid queuing
107
+ only for specific commands.
0 commit comments