Skip to content

calculateBackoff

function calculateBackoff(
   failCount, 
   baseInterval?, 
   maxDelay?, 
   jitterFactor?): Date;

Defined in: packages/core/src/shared/utils/backoff.ts:78

Calculate the next run time using exponential backoff with jitter.

Formula: nextRunAt = now + jitter(2^failCount × baseInterval)

Jitter (±25% by default) is applied to prevent thundering-herd retries when multiple jobs fail at the same time with the same fail count.

ParameterTypeDefault valueDescription
failCountnumberundefinedNumber of previous failed attempts
baseIntervalnumberDEFAULT_BASE_INTERVALBase interval in milliseconds (default: 1000ms)
maxDelay?numberundefinedMaximum delay in milliseconds (optional)
jitterFactor?numberDEFAULT_JITTER_FACTORJitter spread factor, 0–1 (default: 0.25 = ±25%). Set to 0 to disable.

Date

The next run date

// First retry (failCount=1): ~2000ms ±25% delay
const nextRun = calculateBackoff(1);

// With no jitter (deterministic)
const nextRun = calculateBackoff(1, 1000, undefined, 0);