Skip to content

Commit 31c881e

Browse files
authored
Default reconnect strategy uses exponential backoff and jitter (#2736)
* Default reconnect strategy uses exponential backoff and jitter Both are recommended parts of client reconnect strategies to prevent thundering herd problems when many clients lose their connection at once (for example, during a Redis upgrade). * Move default retry strategy to constant * Plain english explanation of default 'socket.reconnectStrategy' * Extract default connect strategy into helper function
1 parent f925235 commit 31c881e

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

docs/client-configuration.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
| socket.keepAlive | `true` | Toggle [`keep-alive`](https://nodejs.org/api/net.html#socketsetkeepaliveenable-initialdelay) functionality |
1414
| socket.keepAliveInitialDelay | `5000` | If set to a positive number, it sets the initial delay before the first keepalive probe is sent on an idle socket |
1515
| socket.tls | | See explanation and examples [below](#TLS) |
16-
| socket.reconnectStrategy | `retries => Math.min(retries * 50, 500)` | A function containing the [Reconnect Strategy](#reconnect-strategy) logic |
16+
| socket.reconnectStrategy | Exponential backoff with a maximum of 2000 ms; plus 0-200 ms random jitter. | A function containing the [Reconnect Strategy](#reconnect-strategy) logic |
1717
| username | | ACL username ([see ACL guide](https://redis.io/topics/acl)) |
1818
| password | | ACL password or the old "--requirepass" password |
1919
| name | | Client name ([see `CLIENT SETNAME`](https://redis.io/commands/client-setname)) |
@@ -35,12 +35,19 @@ When the socket closes unexpectedly (without calling `.quit()`/`.disconnect()`),
3535
2. `number` -> wait for `X` milliseconds before reconnecting.
3636
3. `(retries: number, cause: Error) => false | number | Error` -> `number` is the same as configuring a `number` directly, `Error` is the same as `false`, but with a custom error.
3737

38-
By default the strategy is `Math.min(retries * 50, 500)`, but it can be overwritten like so:
38+
By default the strategy uses exponential backoff, but it can be overwritten like so:
3939

4040
```javascript
4141
createClient({
4242
socket: {
43-
reconnectStrategy: retries => Math.min(retries * 50, 1000)
43+
reconnectStrategy: retries => {
44+
// Generate a random jitter between 0 – 200 ms:
45+
const jitter = Math.floor(Math.random() * 200);
46+
// Delay is an exponential back off, (times^2) * 50 ms, with a maximum value of 2000 ms:
47+
const delay = Math.min(Math.pow(2, retries) * 50, 2000);
48+
49+
return delay + jitter;
50+
}
4451
}
4552
});
4653
```

packages/client/lib/client/socket.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,12 @@ export default class RedisSocket extends EventEmitter {
9797
return retryIn;
9898
} catch (err) {
9999
this.emit('error', err);
100-
return Math.min(retries * 50, 500);
100+
return this.defaultReconnectStrategy(retries);
101101
}
102102
};
103103
}
104104

105-
return retries => Math.min(retries * 50, 500);
105+
return this.defaultReconnectStrategy;
106106
}
107107

108108
#createSocketFactory(options?: RedisSocketOptions) {
@@ -333,4 +333,13 @@ export default class RedisSocket extends EventEmitter {
333333
this.#isSocketUnrefed = true;
334334
this.#socket?.unref();
335335
}
336+
337+
defaultReconnectStrategy(retries: number) {
338+
// Generate a random jitter between 0 – 200 ms:
339+
const jitter = Math.floor(Math.random() * 200);
340+
// Delay is an exponential back off, (times^2) * 50 ms, with a maximum value of 2000 ms:
341+
const delay = Math.min(Math.pow(2, retries) * 50, 2000);
342+
343+
return delay + jitter;
344+
}
336345
}

0 commit comments

Comments
 (0)