QA
Senior LevelJavaScript & TypeScript

How do you implement custom async retry logic with exponential backoff and jitter in TypeScript?

❓ Interview Prompt: Explain exponential backoff timing calculations, full jitter randomization, and resilient API wrappers.

When communicating with network services, retrying failed operations blindly can overwhelm recovering servers (thundering herd problem).

Resilient Retry Pattern:

1
Exponential Backoff: Multiply delay after each failure (delay = initial * 2^attempt).
2
Jitter Randomization: Add random variance to prevent concurrent client retries from synchronizing.
3
AbortSignal Support: Allow cancellation of pending retries.

Code Implementation Example

typescript
interface RetryOptions {
  maxRetries?: number;
  initialDelayMs?: number;
  maxDelayMs?: number;
}

async function retryWithBackoff<T>(
  fn: () => Promise<T>,
  options: RetryOptions = {}
): Promise<T> {
  const { maxRetries = 3, initialDelayMs = 300, maxDelayMs = 5000 } = options;

  for (let attempt = 0; attempt <= maxRetries; attempt++) {
    try {
      return await fn();
    } catch (error) {
      if (attempt === maxRetries) throw error;
      
      // Calculate exponential delay with full jitter
      const expDelay = Math.min(maxDelayMs, initialDelayMs * Math.pow(2, attempt));
      const jitteredDelay = Math.random() * expDelay;
      
      await new Promise(res => setTimeout(res, jitteredDelay));
    }
  }
  throw new Error('Retry limit reached');
}
💡Production-ready exponential backoff algorithm with full jitter randomization.
🎯 Interview Key Takeaway

Use exponential backoff with randomized jitter when retrying failed network requests to prevent server thundering herds.

#Retry Logic#Async#Resilience#Senior
Back to all JavaScript & TypeScript Questions→

More JavaScript & TypeScript Interview Questions

Beginner LevelJavaScript & TypeScript

What are the differences between var, let, and const in JavaScript?

Beginner LevelJavaScript & TypeScript

What are the JavaScript data types and the difference between primitives and objects?

Beginner LevelJavaScript & TypeScript

What is the difference between == (loose equality) and === (strict equality)?