Sumi Sharma
Sumi Sharma

Reputation: 51

Connection pooling in spring WebClient

I want to use Spring WebClient in a project to consume some external web service.

  1. Can WebClient object be singleton or shared among all threads (requests)?
  2. If my application is going to get millions of requests per second, then do I need to pool WebClient Objects? If yes, I am unable to find any documentation or examples.
  3. Does mono.block() internally work similar to future.get() or latch.await()?

Upvotes: 1

Views: 6706

Answers (1)

Nikolas
Nikolas

Reputation: 44368

The WebClient is a non-blocking implementation of a REST client built on the Reactive Stack, so I guess the only issue you should focus on is to complete a non-blocking call.

  1. Can WebClient object be a singleton or shared among all threads (requests)?

A standard way I have seen everywhere is to inject WebClient as a bean. I find no reason to do any different.

@Autowired
WebClient webClient;
  1. If my application is going to get millions of requests per second, then do I need to pool WebClient Objects?

That's a lot! This should definitely need to be solved with service replication, load-balancers, bulkhead, etc. In terms of the client itself, see the following performance of reactive client using newer versions of Spring: WebFlux Reactive Programming Performance Test. Moreover, that is the expected maximal throughput?

  1. Does mono.block() internally work similar to future.get() or latch.await()?

Yes, it does.

Upvotes: 2

Related Questions