Java and Spring Level 3 This level · 50 of 56
Hard java spring

Service calls an external API with no timeout and the API hangs?

The 30 second answer

Request threads block, often indefinitely depending on the client defaults, which exhausts the Tomcat thread pool and connection pools so unrelated requests stop getting answers. If this call sits inside a database transaction, you also hold a DB connection open. Set explicit connect and read timeouts, add a circuit breaker and bulkhead, retry only with backoff on idempotent calls, and never call remote APIs inside a DB transaction.

Could you say this in an interview?

Request threads block (potentially indefinitely, depending on the client's defaults) → Tomcat thread pool / connection pool exhaustion → your service stops responding to unrelated requests: cascading failure. If inside a transaction, a DB connection is held too. Fix: explicit connect + read timeouts (Spring Boot spring.http.client.* settings / ClientHttpRequestFactorySettings for RestClient/RestTemplate), circuit breaker + bulkhead (Resilience4j), retries only with backoff and on idempotent calls, never call remote APIs inside a DB transaction. Editor's note: default timeout values differ per HTTP client and Spring Boot version — check the property appendix for your version.

Alternate interview scenarios

"A downstream service takes 10 seconds to respond. How would you protect your API?"

In this case the dependency still answers, just slowly, so a timeout alone is not enough if it is set too high. I set the read timeout from my own latency budget. For example, if my endpoint must answer in one second, the call gets whatever time is left, not 10 seconds. Concurrency is the hidden cost: at 50 requests per second, a 10-second call holds about 500 threads at once. So I use a bulkhead to cap concurrent calls and fail fast when it is full. If the result does not have to be fresh, I serve cached data or move the call off the request path to async processing.

Sources (4)
  1. Spring Boot — Calling REST services (HTTP client settings)docs.spring.io
  2. Resilience4j CircuitBreakerresilience4j.readme.io
  3. AWS Builders' Library: Timeouts, retries and backoff with jitteraws.amazon.com
  4. Resilience4j: Bulkheadresilience4j.readme.io
Previous
Next