Skip to content

Commit c4eba44

Browse files
committed
Document reconnection behavior with examples
1 parent d42f62e commit c4eba44

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,51 @@ end
220220

221221
If no message is received after 5 seconds, the client will unsubscribe.
222222

223+
## Reconnections
224+
225+
The client allows you to configure how many `reconnect_attempts` it should
226+
complete before declaring a connection as failed. Furthermore, you may want
227+
to control the maximum duration between reconnection attempts with
228+
`reconnect_delay` and `reconnect_delay_max`.
229+
230+
```ruby
231+
Redis.new(
232+
:reconnect_attempts => 10,
233+
:reconnect_delay => 1.5,
234+
:reconnect_delay_max => 10.0,
235+
)
236+
```
237+
238+
The delay values are specified in seconds. With the above configuration, the
239+
client would attempt 10 reconnections, exponentially increasing the duration
240+
between each attempt but it never waits longer than `reconnect_delay_max`.
241+
242+
This is the retry algorithm:
243+
244+
```ruby
245+
attempt_wait_time = [(reconnect_delay * 2**(attempt-1)), reconnect_delay_max].min
246+
```
247+
248+
**By default**, this gem will only **retry a connection once** and then fail, but with the
249+
above configuration the reconnection attempt would look like this:
250+
251+
#|Attempt wait time|Total wait time
252+
:-:|:-:|:-:
253+
1|1.5s|1.5s
254+
2|3.0s|4.5s
255+
3|6.0s|10.5s
256+
4|10.0s|20.5s
257+
5|10.0s|30.5s
258+
6|10.0s|40.5s
259+
7|10.0s|50.5s
260+
8|10.0s|60.5s
261+
9|10.0s|70.5s
262+
10|10.0s|80.5s
263+
264+
So if the reconnection attempt #10 succeeds 70 seconds have elapsed trying
265+
to reconnect, this is likely fine in long-running background processes, but if
266+
you use Redis to drive your website you might want to have a lower
267+
`reconnect_delay_max` or have less `reconnect_attempts`.
223268

224269
## SSL/TLS Support
225270

0 commit comments

Comments
 (0)