Why does the Scala Driver spawn a thread for each operation?

Hi @Lukasz_Myslinski,

The Connection Pool is used as the resource of connections to send operations to MongoDB. Each operation does not necessarily spawn a new connection (thread) from the pool, but will do so if required up to the max connection pool size. If the connection pool size is at maximum and all connections are in use then pending operations are added to the wait queue. If the wait queue exceeds the configured size an error is thrown.

This pool and wait queue behaviour is the same in the sync and async version of the underlying java driver. What is different is the sync driver blocks on waiting for the result of an operation, this generally makes it less likely that app code will exhaust the pool and wait queue (although certainly not impossible).

With the async driver, it is far easier to write app code that does multiple concurrent database operations in a request. Unlike the sync version each will use a connection from the pool at the same time and that can exasperate the issue of connection exhaustion and need the wait queue.

Please note in the next major release of the Scala driver the max wait queue size limitation is being removed but until then you can configure it via the waitQueueMultiple connection string setting.

To understand fully what is going on in your scenario and to ensure nothing else is exasperating the issue I’d need to see some example code.

I hope that helps,

Ross

1 Like