@@ -28,6 +28,8 @@ progress in implementing the recommendations.
28
28
{{< checklist-item "#health-checks" >}}Health checks{{< /checklist-item >}}
29
29
{{< checklist-item "#error-handling" >}}Error handling{{< /checklist-item >}}
30
30
{{< 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 >}}
31
33
{{< /checklist >}}
32
34
33
35
## Recommendations
@@ -68,3 +70,55 @@ you trace command execution and monitor your server's performance.
68
70
You can use this information to detect problems before they are reported
69
71
by users. See [ Observability] ({{< relref "/develop/clients/go#observability" >}})
70
72
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.
0 commit comments