ral-facilities / inventory-management-system-api

Apache License 2.0
0 stars 0 forks source link

Reduce MongoClient max connection pool size and use multiple workers in production #258

Open joelvdavies opened 6 months ago

joelvdavies commented 6 months ago

Current default maxPoolSize for MongoClient is 100. We may wish to reduce this amount to limit the number of active connections per worker. Additionally we only use one worker currently, but in the production dockerfile we could use multiple via the --workers setting.

See here for info on maxPoolSize: https://pymongo.readthedocs.io/en/stable/faq.html#how-does-connection-pooling-work-in-pymongo

joelvdavies commented 6 months ago

It sounds like using async means that one request will be processed at a time with one worker, but we dont use that https://stackoverflow.com/questions/76524933/how-does-uvicorn-fastapi-handle-concurrency-with-1-worker-and-synchronous-endp

https://stackoverflow.com/questions/72374634/how-many-uvicorn-workers-do-i-have-to-have-in-production

If we go for --workers 4 for example using the default maxPoolSize of 100, we could have a total of 400 open database connections at once. In practice however as we can only process 4 requests at the same time, I would assume that ignoring any async behaviour we would only have 4 open connections at once.

Its possible to investigate using

docker exec -i mongodb_container mongosh --username 'root' --password 'example' --authenticationDatabase=admin --eval "db.serverStatus()"

For which I found a total of 9 connections which increased from an initial 6 by navigating around the site (with a single worker), but then never went further. So it appears it can differ in practice still if there are more connections available in the pool. Increasing the workers to 4, I found a total of 17 connections after navigation without changing the maxPoolSize in either case. Setting maxPoolSize still maintained 16 connections, so not sure what is going on there.