SteelReed

Search

Search posts, questions…

7. System Design

Timeouts, retries, circuit breakers and bulkheads for third-party calls

Keeping a slow or dead third party from taking your system down: three kinds of timeout, safe retries with idempotency keys, backoff and jitter, retry storms, circuit breakers, bulkheads, business-driven fallbacks and Resilience4j in Spring Boot 3.

18 Nov 20267 min readSenior

–

A third party getting ten times slower at peak hour is enough to take down an unprepared system: threads get stuck and retries at every layer multiply the load. This post is the "defensive" half of calling out: timeouts, retries, circuit breakers, bulkheads, fallbacks and their Spring Boot 3 configuration. Choosing a provider and webhooks are in part 1.

1. The problem

The SMS provider hangs for 2 minutes. RestTemplate has no timeout configured, so all 200 Tomcat threads get stuck one by one. The whole service stops serving, including the payment API, which has nothing to do with SMS.

2. Key idea

Like a house's breakers: each room has its own (the bulkhead), so one short doesn't black out the whole house.

Timeout Measures Suggested value
Connect TCP connect and TLS handshake 1–2 s
Read waiting for the response the other side's real p99 + margin, not "30 s to be safe"
Deadline every attempt, including retries within your own API's promise, e.g. 3 s

2.1 When to retry?

Question Answer
Is the error worth retrying? yes: timeouts, connection errors, 429, 502, 503, 504; no: 400, 401, 403, 404, 422
Is resending safe? GET is; POST only with an idempotency key; without one, query the status first
How long to wait? exponential backoff with a cap + jitter; for 429, follow Retry-After

Add a retry budget (~10% of requests) so retries don't become a storm.

2.2 Fallbacks by business need

Fallback Use when Don't use when
Fail fast (a clear 503) payments
Stale data from cache exchange rates, estimated shipping fees numbers must be exact to the cent
Backup provider SMS, email the two behave differently and you haven't tested

A wrong fallback is worse than none: showing "payment successful" when the gateway timed out is an unpaid order.

3. In Java

JAVA
HttpClient http = HttpClient.newBuilder()
        .connectTimeout(Duration.ofSeconds(1))
        .build();
HttpRequest req = HttpRequest.newBuilder(URI.create(url))
        .timeout(Duration.ofSeconds(3))                    // read timeout: without it, wait forever
        .header("Idempotency-Key", key)                    // the same key for every attempt
        .POST(body).build();
 
long sleep = ThreadLocalRandom.current().nextLong(         // full jitter
        Math.min(capMs, baseMs << attempt));
// a mock server returning 503 twice: attempt 1 sleeps 60 ms, attempt 2 sleeps 133 ms, attempt 3 succeeds,
// and the server sees exactly one idempotency key across all three

In Spring Boot use Resilience4j: @CircuitBreaker(name = "sms", fallbackMethod = "sendViaBackup"), @Retry, @Bulkhead. By default Retry wraps outermost, so every attempt passes through the circuit breaker.

Timeouts elsewhere
  • orTimeout on a CompletableFuture only completes the future; the underlying call keeps running. Real timeouts go on the client.
  • Many provider SDKs wrap the default clients without setting timeouts: review timeouts as part of the integration.
  • gRPC propagates deadlines across hops, so downstream services know how much time remains.
  • With Spring's RestClient: build a JdkClientHttpRequestFactory from an HttpClient with a connect timeout, then setReadTimeout.

4. Working with AI

Give the AI: client configuration with timeouts, slow and failing mock servers for tests, Resilience4j config.

Check in AI-written code:

  • Every outbound client has explicit connect and read timeouts, with a test against a slow server.
  • Retries only for transient errors, with backoff and jitter, at most 2–3 attempts.
  • Retried POSTs carry an idempotency key; no retries at two layers for the same call.
  • Fallbacks match the business; payments never "fake success".

5. Exercises

Exercise 1: An SMS client

An OTP service uses new RestTemplate(); on errors a for loop resends up to 5 times immediately; the gateway in front also retries 3 times; the provider doesn't support idempotency keys. Point out the problems and fix them.

Solution to Exercise 1
  1. No timeouts: a hanging provider drains every thread. Connect 1 s, read from their p99.
  2. Immediate retries without backoff or jitter: piling on while they're weak.
  3. Retries at two layers: 3 × 5 = 15 calls per OTP. Drop gateway retries for this endpoint.
  4. No idempotency key: a read timeout may already have sent the message. Retry only on connection errors (definitely not sent), not read timeouts.
  5. A circuit breaker with a backup provider as fallback, and a dedicated bulkhead for SMS.

Exercise 2: A breaker that keeps opening

The payment gateway's circuit breaker keeps reopening even after the gateway recovered. Config: 50% threshold over a 10-call window, waitDurationInOpenState 60 s, 1 trial call in half-open, read timeout 500 ms while the gateway's p99 is 900 ms. Diagnose.

Solution to Exercise 2

A 500 ms read timeout is below the 900 ms p99: even normally more than 1% of calls time out, and while the gateway is a bit slow after an incident most of them do. The single half-open trial easily lands in the slow group, and the breaker opens for another 60 s.

Fix: read timeout at p99 + margin (e.g. 1.5 s), several half-open trial calls (e.g. 5), a larger window so a few errors don't flip the state. Measure the timeout rate before and after.

Summary

  • Every outbound call has connect and read timeouts and an overall deadline.
  • Retry transient errors with backoff and jitter; POSTs need an idempotency key.
  • Retry at one layer only.
  • Circuit breakers fail fast; bulkheads isolate each dependency.
  • Fallbacks follow the business; never "fake success".

Read more: Calling LLMs from the backend.

Practice interview questions on this topic

All Microservices questions →All AI & LLM questions →

Loading comments…
Next on the roadmapRate limiting: five algorithms, a token bucket on Redis, and designing a distributed limiter