DEV Community

Nick Mousavi
Nick Mousavi

Posted on • Originally published at biomousavi.com

Understanding Jitter Backoff: A Beginner's Guide

Retry Necessity

Network failures are inevitable. Client applications must implement retry mechanisms to handle these failures. there are many reasons that you might encounter network failure.

It's better to implement retries with Exponential Backoff and Jitter

What is Exponential Backoff ?

In exponential backoff, we increase the delay between retry attempts exponentially.

let delay = 5000
let timerId = setTimeout(function request() {

  const requestSuccess = Math.random() > 0.7 // simulate 30% success rate

  if (!requestSuccess) {
    delay *= 2 // increase delay exponentially
    timerId = setTimeout(request, delay) // retry request
  }
  else {
    clearTimeout(timerId) // stop retries on success
  }
}, delay)

Enter fullscreen mode Exit fullscreen mode

but, this can lead to thundering herd problem.

Thundering herd problem

In a traditional backoff strategy, when a client encounters a failure (like a server unavailability), it typically implements a retry mechanism. A naive approach might be to have all clients retry at the same time after a fixed interval, that makes Thundering herd problem.

Simple scenario:

Imagine you have 100 clients trying to access a service that temporarily goes down. With a traditional fixed backoff:

  • All 100 clients detect the service failure and wait exactly 5 seconds
  • After 5 seconds, ALL 100 clients simultaneously attempt to reconnect and it can cause further service instability and trigger another round of failure

On the other hand, Jitter can add some randomness to attempts.

What is Jitter?

Jitter adds random factor to our attempt intervals and it Prevents Synchronization(Stops multiple requests from retrying simultaneously) and Improves System Stability(Prevents recurring peak load patterns)

Let's add Jitter to our code example:

let delay = 5000
let timerId = setTimeout(function request() {

  const requestSuccess = Math.random() > 0.7 // simulate 30% success rate

  if (!requestSuccess) {
    const jitter = Math.random() * 1000; // add random factor
    delay = delay * 2 + jitter;
    timerId = setTimeout(request, delay) // retry request
  }
  else {
    clearTimeout(timerId) // stop retries on success
  }
}, delay)
Enter fullscreen mode Exit fullscreen mode

Conclusion:

Exponential backoff combined with jitter is a crucial technique for building resilient and performant distributed systems. It allows client applications to handle failures gracefully without overwhelming the system, leading to a smoother and more reliable user experience.

👋 Was this post useful to you?

Thank you for stopping by the DEV Community. Please leave your appreciation by commenting on this post!

It takes one minute and is worth it for your career.

Join now

Top comments (0)

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay