Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
joelvdavies opened this issue Apr 23, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@joelvdavies
Copy link
Collaborator

joelvdavies commented Apr 23, 2024

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 joelvdavies added the enhancement New feature or request label Apr 23, 2024
@joelvdavies
Copy link
Collaborator Author

joelvdavies commented Apr 23, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: Backlog
Development

No branches or pull requests

1 participant