1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【JavaScript】Exponential Backoff And Jitter

Last updated at Posted at 2024-03-15

方法1

// 待機時間を1秒, 2秒, 4秒, 8秒...と増加させる。(最大32秒)
const backoff = Math.min(Math.pow(2, retryCount) * 1000, 32000)
// 待機時間にばらつきを出す
const jitter = Math.random() * 1000  
const jitteredBackoff = backoff + jitter

await new Promise((resolve) => setTimeout(resolve, jitteredBackoff))

待機時間の例

1回目 1541.7146030710737ms
2回目 2808.2306075308397ms
3回目 4565.012911744703ms
4回目 8919.909323193897ms
5回目 16987.425867053946ms

参考ページ

方法2

Full Jitter

const backoff = Math.min(Math.pow(2, retryCount) * 1000, 32000)  
const jitteredBackoff = backoff * Math.random()  

await new Promise((resolve) => setTimeout(resolve, jitteredBackoff))

待機時間の例

1回目 794.2005031744484ms
2回目 1627.3717991131734ms
3回目 780.8109128801161ms
4回目 2029.0619185410499ms
5回目 10193.744317147111ms

参考ページ

方法3

Equal Jitter

const backoff = Math.min(Math.pow(2, retryCount) * 1000, 32000) 
const jitteredBackoff = backoff / 2 + Math.random() * backoff

await new Promise((resolve) => setTimeout(resolve, jitteredBackoff))

待機時間の例

1回目 889.4631476869306ms
2回目 2791.704999468895ms
3回目 3734.710652151566ms
4回目 7685.752867284689ms
5回目 13774.910498958634ms

参考ページ

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?