typescript
47 lines · 7 steps
Exponential backoff retries in Angular
An Angular service retries failing HTTP requests with growing, jittered delays — but only for errors worth retrying.
Explained by
highlit
1import { HttpClient } from '@angular/common/http';
2import { Injectable } from '@angular/core';
3import { Observable, throwError, timer } from 'rxjs';
4import { mergeMap, retryWhen } from 'rxjs/operators';
5
6interface RetryConfig {
7 maxRetries: number;
8 baseDelayMs: number;
9 maxDelayMs: number;
10}
11
12@Injectable({ providedIn: 'root' })
13export class PricingApiService {
14 private readonly config: RetryConfig = {
15 maxRetries: 4,
16 baseDelayMs: 300,
17 maxDelayMs: 8000,
18 };
19
20 constructor(private readonly http: HttpClient) {}
21
22 getQuote(symbol: string): Observable<Quote> {
23 return this.http
24 .get<Quote>(`/api/quotes/${symbol}`)
25 .pipe(retryWhen((errors) => this.backoffStrategy(errors)));
26 }
27
28 private backoffStrategy(errors: Observable<any>): Observable<number> {
29 return errors.pipe(
30 mergeMap((error, index) => {
31 const attempt = index + 1;
32 const retriable = error.status >= 500 || error.status === 0;
33
34 if (!retriable || attempt > this.config.maxRetries) {
35 return throwError(() => error);
36 }
37
38 const backoff = Math.min(
39 this.config.baseDelayMs * 2 ** index,
40 this.config.maxDelayMs,
41 );
42 const jitter = Math.random() * this.config.baseDelayMs;
43 return timer(backoff + jitter);
44 }),
45 );
46 }
47}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Retry only transient failures — server errors and network drops — and let client errors fail fast.
- 2Exponential backoff with a cap prevents retries from hammering a struggling backend.
- 3Adding random jitter spreads concurrent retries so clients don't all reconnect in sync.
Related explainers
typescript
import { CallHandler, ExecutionContext, Injectable,
Wrapping responses in a NestJS interceptor
interceptors
rxjs
response-shaping
Intermediate
7 steps
typescript
type RetryOptions = { retries?: number; timeoutMs?: number; baseDelayMs?: number;
Retry with timeout and backoff in TypeScript
promises
retry
exponential-backoff
Intermediate
10 steps
typescript
import { Pipe, PipeTransform, ChangeDetectorRef, NgZone, OnDestroy } from '@angular/core'; @Pipe({ name: 'timeAgo',
A self-refreshing timeAgo pipe in Angular
impure-pipe
change-detection
timers
Advanced
10 steps
typescript
const DIVISIONS: { amount: number; unit: Intl.RelativeTimeFormatUnit }[] = [ { amount: 60, unit: "seconds" }, { amount: 60, unit: "minutes" }, { amount: 24, unit: "hours" },
Human-readable relative times with Intl
internationalization
date-formatting
lookup-table
Intermediate
7 steps
typescript
import { Component } from '@angular/core'; import { FormControl, ReactiveFormsModule } from '@angular/forms'; import { HttpClient } from '@angular/common/http'; import { AsyncPipe } from '@angular/common';
Reactive type-ahead search in Angular
rxjs
reactive-forms
debounce
Intermediate
9 steps
typescript
function throttle<T extends (...args: any[]) => void>( fn: T, limit: number ): (...args: Parameters<T>) => void {
Building a trailing-edge throttle in TypeScript
throttling
closures
generics
Intermediate
7 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/exponential-backoff-retries-in-angular-explained-typescript-9669/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.