Skip to content
Communication Between Services

Lesson 8 of 10 · 24 min

x
8/10

Lesson position in the course — not completion. Use Mark Complete to track finished lessons (saved in this browser).

Resilience: Retries, Timeouts & Circuit Breakers

Networks fail. Services crash. Timeouts expire. Without resilience patterns, one slow dependency cascades into a system-wide outage — the classic cascading failure. Three patterns defend against this.

Timeouts cap how long a caller waits. Retries re-attempt transient failures (network blip, 503) with exponential backoff and jitter so you do not hammer a recovering service. Circuit breakers stop calling a failing service entirely after a threshold of errors, giving it time to recover while the caller fails fast or returns a fallback. Together they turn "everything is down" into "this one feature is degraded."

Before
Fragile — retry storm on outage
1async function getRecommendations(userId) {2  // Retries immediately, 100 times, no backoff3  for (let i = 0; i < 100; i++) {4    try {5      return await fetch('/recommendations/' + userId);6    } catch (e) { /* retry */ }7  }8}
After
Resilient sketch — circuit breaker + backoff (pseudo-code)
1const breaker = new CircuitBreaker(fetchRecommendations, {2  timeout: 3000,3  errorThresholdPercentage: 50,4  resetTimeout: 30000,5});6 7async function fetchRecommendations(userId) {8  return fetchWithRetry(9    `/recommendations/${userId}`,10    { retries: 3, backoff: 'exponential' }11  );12}13 14// After 50% failures → circuit opens → fail fast for 30s

Exercise

For one outbound dependency, define timeout, max retries with exponential backoff+jitter, and a circuit-open behavior (fail fast or fallback). Write the policy as config comments even if you use a library sketch.

Previous

Progress is saved in this browser.

Next Lesson