Random jitter is a technique used in conjunction with exponential backoff to introduce a random delay within a defined range. Instead of a fixed, exponentially increasing delay, the actual waiting time is randomized. For example, if the calculated exponential backoff delay is 4 seconds, a random jitter algorithm might choose a delay anywhere between 0 and 4 seconds. This approach helps to prevent a “thundering herd” problem, where multiple clients, after experiencing a failure, all retry at the exact same moment.
Pseudo-code
define max_retries
define initial_delay
function retry(operation, max_retries, initial_delay)
// Initialize the attempt counter and the current delay
attempt <- 0
delay <- initial_delay
while attempt < max_retries do
try
// Attempt to perform the operation
operation()
// If successful, exit the function
return
catch
// If the operation fails, increment the attempt counter
attempt <- attempt + 1
// Calculate the delay with a random jitter component
jittered_delay <- delay + random(0, delay)
// Pause for the jittered delay period
sleep jittered_delay
// Double the base delay for the next retry (exponential backoff)
delay <- delay * 2
end while
// If all retries fail, the function will finish here
end function