Skip to content

randomize: timeouts are sorted after jitter, so retry i doesn't follow the README formula #103

Open

Description

@ritabratamaiti

The README says randomize multiplies each timeout by a factor between 1 and 2, and gives the formula for each one:

Math.min(random * minTimeout * Math.pow(factor, attempt), maxTimeout)

(README.md#L123-L129)

retry.timeouts() draws one random factor per retry and then sorts the whole array ascending (retry.js#L32-L44). The sort was added for factor < 1 (#3). With randomize: true it also reorders the jittered values whenever the ranges of neighboring retries overlap, which is any factor < 2. Retry i then waits the i-th smallest of all the draws, not random * minTimeout * factor^i.

The clearest case is factor: 1, constant backoff with jitter. By the formula every retry waits a value spread evenly over [1000, 2000] ms. After the sort, the first retry waits the minimum of all the draws and the last one waits the maximum, so the waits always go up and the early retries bunch up near minTimeout.

Environment: retry 0.13.1 (latest on npm, same code as master at 11efd6e), Node 18.19.1, Ubuntu 24.04 x86_64.

Reproduction

const retry = require('retry');
const N = 200000;
let below = 0, sum = 0;
for (let i = 0; i < N; i++) {
  const first = retry.timeouts({retries: 10, factor: 1, minTimeout: 1000, randomize: true})[0];
  if (first < 1100) below++;
  sum += first;
}
console.log('mean', (sum / N).toFixed(1), 'P(<1100)', (below / N).toFixed(4));
// mean 1090.7 P(<1100) 0.6502

Expected

Each retry waits Math.min(random * minTimeout * factor^i, maxTimeout) with its own random factor. For the sample above, the first wait is spread evenly over [1000, 2000]: mean 1500 ms, and P(first < 1100) = 0.0995 (0.1 before Math.round).

What happens instead

first wait, factor: 1, retries: 10 formula measured (1,000,000 runs) smallest of 10 draws, exact
P(first < 1100 ms) 0.0995 0.6493 (99.9% CI 0.6478 to 0.6509) 1 - 0.9005^10 = 0.6494
mean 1500 ms 1090.8 ms 1090.9 ms
standard deviation 288.7 ms 82.8 ms 83.0 ms

In all 1,000,000 runs the ten waits came out in ascending order.

With factor: 1.5 and retries: 2, the first retry uses the second draw instead of the first in 8.3% of runs (exact 499001/6000000, about 1/12; measured 0.0830 to 0.0834 in three runs of 1,000,000).

Possible fix

Sort the base timeouts before applying the random factor, or skip the sort when randomize is true and factor >= 1. If ascending order is the intent (test/integration/test-timeouts.js checks that randomized timeouts go up, with the default factor: 2, where the sort changes nothing), the README could say that the jittered values are sorted, so retry i gets the i-th smallest.

I can send a PR for this.

Notes

Only matters with randomize: true and factor < 2. factor: 1 with randomize is how you get a jittered constant delay from the options, and there the effect is large: the first retries come much sooner than the formula says, and the spread of the first wait is about 3.5 times narrower (standard deviation 83 ms instead of 289 ms). With many clients failing at once, their first retries bunch up more, which works against the point of jitter.

#3 is why the sort exists. I searched issues and PRs for randomize, sort and order and didn't find this. The open PR #98 changes timeouts() for retries: Infinity but leaves the sort as it is.


Checking the jitter distribution with Haze

I used Haze to compare the first draw with the first timeout after sorting. The complete model assumes independent uniform draws on [0, 1) and includes Math.round. This is the rounded draw and the two queries for factor: 1, ten retries and minTimeout: 1000:

def draw(base : Num) : Num := base + floor((uniform(0, 2 * base - 1) + 1) / 2)

#prob Factor1 |= after[10] formula_short
#prob Factor1 |= after[10] first_short

formula_short means the attempt-0 draw is below 1,100 ms. first_short means the smallest of the ten draws is below it.

Event Model probability
Attempt-0 draw is below 1,100 ms 0.0995
First timeout after sorting is below 1,100 ms 1 - 0.9005^10, approximately 0.64938

For factor: 1.5 and two retries, the model also checks:

theorem retry0_follows_formula : Factor15 |= after[2] first == t0

Haze rejects it with t0 = 1501 and first = 1500: sorting selects the later draw. The package measurements above agree with these results. The model establishes the distribution change; whether to retain sorting and clarify the docs is a separate API decision.

Run the Haze model and compare its output

The model, checker and recorded output are pinned to Haze commit 452a5993. With elan installed, these commands build and run that revision in a Unix shell:

git clone https://github.com/ritabratamaiti/haze-lang
cd haze-lang
git checkout --detach 452a5993a68962653afd3eb5382e9c4abcfcd421
lake build haze
.lake/build/bin/haze examples/Found/node_retry.hz

Exit status 1 is expected: the file deliberately includes rejected claims. The comparison variants and full diagnostics are in the same file.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions