Skip to content

Instantly share code, notes, and snippets.

@pradishb
Last active January 30, 2024 01:59
Show Gist options
  • Save pradishb/7ef2edebbaaed09c429c461f607af708 to your computer and use it in GitHub Desktop.
Save pradishb/7ef2edebbaaed09c429c461f607af708 to your computer and use it in GitHub Desktop.
Exponential backoff with jitter in python
from random import randint
def get_expo_backoff(
base: int, attempts: int = 1, cap: int = 100_000_000, jitter: bool = True
):
"""
Returns a backoff value https://aws.amazon.com/blogs/architecture/exponential-backoff-and-jitter/
:param base: The time to sleep in the first attempt.
:param attempts: The number of attempts that have already been made.
:param cap: The maximum value that can be returned.
:param jitter: Whether or not to apply jitter to the returned value.
"""
# Full jitter formula
# https://aws.amazon.com/blogs/architecture/exponential-backoff-and-jitter/
if jitter:
return randint(base, min(cap, base * 2 ** (attempts - 1)))
else:
return min(cap, base * 2 ** (attempts - 1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment