I use the OkHttp library in my Java application. This application makes a lot of API calls to different external services, but most of the calls are made on the same domains, like this:
- DOMAIN 1, about 55% of the total calls
- DOMAIN 2, about 35% of the total calls
- OTHER DOMAINS, about 10% of the total calls
At the moment I have three separate
ConnectionPool objects, with the corresponding
OkHttpClient object. If I understood correctly, the point of a
ConnectionPool is to keep connections alive in order to be reused when a second request to the same domain is made. So in my mind it makes sense to have separate connection pools for those domains where I know I make a lot of requests (I do about 1.000 calls / minute), but online I found this:
Applications should avoid creating many connection pools as doing so
prevents connection reuse. Each connection pool holds its own set of
connections alive so applications that have many pools also risk
The best practice in OkHttp 3 is to create a single OkHttpClient
instance and share it throughout the application. Requests that needs
a customized client should call OkHttpClient.newBuilder() on that
shared instance. This allows customization without the drawbacks of
separate connection pools.
So my question is: should I change my code and reuse the same object for every request, or is it okay to keep it the way I’m doing? Also, which could be a good THREADS_NUMBER to start with? I’m currently using about 60 threads for every pool and not having any issue, but I’d like to know if there is a “formula” to calculate a more precise / recommended number.
Thanks a lot!