Concurrency in real systems question 132 of 180 mid
HttpClient, RestTemplate or WebClient: what concurrency questions do you ask before choosing?
- Which one has the nicest API
- Correct: Is the instance shareable and thread safe, what is its connection pool limit, and are both connect and read timeouts set
- Whether it supports HTTP/2
- How many threads the JVM has
All three are thread safe and meant to be shared. Creating one per request is the common mistake: it allocates a fresh connection pool, discards keep-alive, and in the worst case leaks threads, which looks like a slow memory leak and a rising thread count.
The two numbers that actually decide behaviour under load are the client's own connection pool size, which caps concurrency independently of your thread pool, and the timeouts. A missing read timeout is the single most common cause of a service that hangs with no CPU load: every worker ends up waiting forever on a dependency that will never answer.
Add a circuit breaker or a bulkhead per dependency if one slow service must not consume the capacity
the others need. That bulkhead is usually a Semaphore, which is the same primitive from two topics
back, applied where it belongs.
Quiz yourself on Concurrency in real systems Suggest a correction