The context : I want to break down a module onto sub modules. Each one with a responsability. After the breakdown, I would like to compose / orchestrate the services provided by each submodules to build aggregate objects.
My current solution : The composition used RxJava. First I load a list of ObjectA then inside a flatMap I call asynchronously (using defer and Schedulers.io) multiple services and finally inside the flatMap I use a zip operator to build a new Object.
objectsA.flatMap {
Observable<A> oa = loadAAsync();
Observable<B> ob = loadBAsync();
Observable<C> oc = loadCAsync();
return Observable.zip(oa, ob, oc, (a, b, c) -> { build() };
}
The methods loadXAsync call a service that load an object from the Databse.
My problem : This solution is currently slow. x5 times the original one (a single SQL request). I was a bit dissapointed. In fact the solution is slower each time I add a new loading part. With only A, quite good, B decreases performance, C etc etc. My first feeling is that the connection pool used (HiakriCP) cannot provide enough database connection, becoming a bottleneck. Currently I have a pool size of 10. The HikariWiki said that large enough.
My question : How do you handle this case ? is it normal for RxJava "experts" ? do you increase the pool size ? or do you limit the number of threads ?
Update 1 @divers : Perhaps I'm totally wrong about the connection pool. I will try to explain my thinking.
The calls are made asynchronously. Each new thread will pick a connection and make a request to the DB. The Schedulers.io uses a thread pool that will grow as needed. If I got a hundreds of ObjectA emitted. Each one will make 5 sql requests.
I think (need to check this one) that flatMap isn't blocking. So I will have a lot of threads and a lot of batches of 5 requests in parallel. ex: objectA#1 emitted -> 5 requests, objectA#2 emitted before the end of objectA#1 so 5 five requests in parallel.
My pool size is fixed and contains 10 connections. That's why I thought my problem comes from here.
*Update 2 * : Here is the SO Question / code that I tried on my project
*Update 3 * : Configuring my pool size to 50. When I monitor the Hikari MBean, I saw that during a short period of time, the
- ActiveConnections = 50 (so all connections used)
- IdleConnections = 0
- ThreadsAwaitingConnection : the max value I saw was 210.
Please correct me :)