Skip to content

Commit 2c1a03f

Browse files
DOC-5291 added retries and timeouts to go-redis prod usage
1 parent c7cf7ab commit 2c1a03f

File tree

3 files changed

+59
-2
lines changed

3 files changed

+59
-2
lines changed

content/develop/clients/go/produsage.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ progress in implementing the recommendations.
2828
{{< checklist-item "#health-checks" >}}Health checks{{< /checklist-item >}}
2929
{{< checklist-item "#error-handling" >}}Error handling{{< /checklist-item >}}
3030
{{< checklist-item "#monitor-performance-and-errors">}}Monitor performance and errors{{< /checklist-item >}}
31+
{{< checklist-item "#retries" >}}Retries{{< /checklist-item >}}
32+
{{< checklist-item "#timeouts" >}}Timeouts{{< /checklist-item >}}
3133
{{< /checklist >}}
3234

3335
## Recommendations
@@ -68,3 +70,55 @@ you trace command execution and monitor your server's performance.
6870
You can use this information to detect problems before they are reported
6971
by users. See [Observability]({{< relref "/develop/clients/go#observability" >}})
7072
for more information.
73+
74+
### Retries
75+
76+
`go-redis` will automatically retry failed connections and commands. By
77+
default, the number of attempts is set to three, but you can change this
78+
using the `MaxRetries` field of `Options` when you connect. The retry
79+
strategy starts with a short delay between the first and second attempts,
80+
and increases the delay with each attempt. The initial delay is set
81+
with the `MinRetryBackoff` option (defaulting to 8 milliseconds) and the
82+
maximum delay is set with the `MaxRetryBackoff` option (defaulting to
83+
512 milliseconds):
84+
85+
```go
86+
client := redis.NewClient(&redis.Options{
87+
MinRetryBackoff: 10 * time.Millisecond,
88+
MaxRetryBackoff: 100 * time.Millisecond,
89+
MaxRetries: 5,
90+
})
91+
```
92+
93+
You can use the observability features of `go-redis` to monitor the
94+
number of retries and the time taken for each attempt, as noted in the
95+
[Monitor performance and errors](#monitor-performance-and-errors) section
96+
above. Use this data to help you decide on the best retry settings
97+
for your application.
98+
99+
### Timeouts
100+
101+
`go-redis` supports timeouts for connections and commands to avoid
102+
stalling your app if the server does not respond within a reasonable time.
103+
The `DialTimeout` field of `Options` sets the timeout for connections,
104+
and the `ReadTimeout` and `WriteTimeout` fields set the timeouts for
105+
reading and writing data, respectively. The default timeout is five seconds
106+
for connections and three seconds for reading and writing data, but you can
107+
set your own timeouts when you connect:
108+
109+
```go
110+
client := redis.NewClient(&redis.Options{
111+
DialTimeout: 10 * time.Second,
112+
ReadTimeout: 5 * time.Second,
113+
WriteTimeout: 5 * time.Second,
114+
})
115+
```
116+
117+
You can use the observability features of `go-redis` to monitor the
118+
frequency of timeouts, as noted in the
119+
[Monitor performance and errors](#monitor-performance-and-errors) section
120+
above. Use this data to help you decide on the best timeout settings
121+
for your application. If timeouts are set too short, then `go-redis`
122+
might retry commands that would have succeeded if given more time. However,
123+
if they are too long, your app might hang unnecessarily while waiting for a
124+
response that will never arrive.

content/develop/clients/nodejs/produsage.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,7 @@ const client = createClient({
7474
}
7575
});
7676
client.on('error', error => console.error('Redis client error:', error));
77-
```
77+
```
78+
79+
### Retries
80+

content/develop/clients/redis-py/produsage.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,5 +195,5 @@ r = Redis(
195195
Take care to set the timeouts to appropriate values for your use case.
196196
If you use timeouts that are too short, then `redis-py` might retry
197197
commands that would have succeeded if given more time. However, if the
198-
timeouts are too long, your app might spend too long waiting for a
198+
timeouts are too long, your app might hang unnecessarily while waiting for a
199199
response that will never arrive.

0 commit comments

Comments
 (0)