Which resilience patterns do you apply to service-to-service calls, and how do you configure them?
Failure is normal
Every remote call can be slow, fail, or fail partially. Without protection, a slow dependency exhausts the caller's threads and connections and the failure cascades.
The patterns (Resilience4j in Spring Boot)
- Timeouts on every call: connect timeout (about 1 s) and a read/response timeout derived from the dependency's p99 plus margin and your own SLO budget. Missing timeouts are the most common cause of cascading failures.
- Retries only for transient errors (connection resets, 503, 429 with
Retry-After), only for idempotent operations (or with idempotency keys), a small number of attempts (2–3), exponential backoff with jitter so clients do not retry in lockstep. - Circuit breaker: tracks the failure and slow-call rate over a sliding window. CLOSED (normal) → OPEN when, for example, 50% of the last 50 calls failed: calls fail fast without touching the dependency → after a wait (30 s) HALF_OPEN: a few trial calls decide whether to close or reopen. Protects both sides and gives the dependency time to recover.
- Bulkhead: limit concurrent calls per dependency (semaphore or thread-pool bulkhead) so one slow dependency cannot consume all request threads.
- Rate limiter: protect a dependency's quota.
- Fallbacks: cached data, default values, degraded responses ("recommendations unavailable"), or a queued request. Never a silent wrong answer for critical data such as prices or balances.
Order of decorators (outermost first) in Resilience4j: Retry → CircuitBreaker → RateLimiter → TimeLimiter → Bulkhead.
Example
resilience4j:
circuitbreaker:
instances:
inventory:
sliding-window-size: 50
failure-rate-threshold: 50
slow-call-duration-threshold: 800ms
slow-call-rate-threshold: 60
wait-duration-in-open-state: 30s
permitted-number-of-calls-in-half-open-state: 5
retry:
instances:
inventory:
max-attempts: 3
wait-duration: 200ms
enable-exponential-backoff: true
enable-randomized-wait: true
retry-exceptions: [java.io.IOException, org.springframework.web.client.HttpServerErrorException$ServiceUnavailable]@CircuitBreaker(name = "inventory", fallbackMethod = "cachedAvailability")
@Retry(name = "inventory")
public Availability availability(String sku) { return inventoryClient.get(sku); }
Availability cachedAvailability(String sku, Throwable t) { return cache.lastKnown(sku).orElse(Availability.unknown()); }Follow-up questions interviewers ask
Try answering each one before you open it.
What is a retry storm and how do you prevent it?
When a dependency degrades, every layer retries: three layers with three attempts each multiply traffic 27-fold, overloading the struggling service so it cannot recover. Prevent it by retrying at only one layer (usually closest to the failure, or at the edge), using retry budgets (cap retries at, say, 10% of traffic), backoff with jitter, circuit breakers that stop retries when the dependency is down, and honouring Retry-After.
How do you choose timeout values?
Start from the user-facing SLO and work down the call chain as a deadline budget: if the API must answer in 1 s and calls two services sequentially, each gets well under 500 ms, minus retries. Use the dependency's measured p99 (plus margin) as a floor. Propagate deadlines (gRPC does this natively) so downstream services stop work the caller has already abandoned.