Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default retry strategy uses exponential backoff and jitter #1854

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -761,9 +761,12 @@ using the `retryStrategy` option:
```javascript
const redis = new Redis({
// This is the default value of `retryStrategy`
retryStrategy(times) {
const delay = Math.min(times * 50, 2000);
return delay;
retryStrategy: function (times) {
// Generate a random jitter between 0 – 200 ms:
const jitter = Math.floor(Math.random() * 200);
// Delay is an exponential back off, (times^2) * 50 ms, with a maximum value of 2000 ms:
const delay = Math.min(Math.pow(2, times) * 50, 2000);
return delay + jitter;
},
});
```
Expand Down
6 changes: 5 additions & 1 deletion lib/redis/RedisOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,11 @@ export const DEFAULT_REDIS_OPTIONS: RedisOptions = {
connectTimeout: 10000,
disconnectTimeout: 2000,
retryStrategy: function (times) {
return Math.min(times * 50, 2000);
// Generate a random jitter between 0 – 200 ms:
const jitter = Math.floor(Math.random() * 200);
// Delay is an exponential back off, (times^2) * 50 ms, with a maximum value of 2000 ms:
const delay = Math.min(Math.pow(2, times) * 50, 2000);
return delay + jitter;
},
keepAlive: 0,
noDelay: true,
Expand Down