2 / 2
Oct 2024

Hey, we are currently creating users dynamically for our multi tenant system. When a new tenant is created, we create his databases and collections. Then we create a new user that will be have read/write access only these databases. But sometimes we can not connect with the created credentials in timely manner.

Since we can not create users directly (unsupported command), we use the API. When the user is created he is not immediately able to connect with his credentials. I figured its because the created user is being propagated to the whole cluster - as can be seen from the blue notification about applying changes. So we have added a timeout and we try to connect with the created credentials before we continue - this has 10 tries every 3 seconds.

But, when we create 4 tenants either in parallel or sequentially - the third and fourth user can not connect - it takes more than a minute before the credentials can be used to connect to the database.

Is there some safer, ideally bulletproof method we can use to handle this use case?

Hi Lukas,

Great question! As you’ve noticed it takes a bit of time for those users to be applied. In order to help you manage this programmatically we have an endpoint in the Atlas Admin API here - MongoDB Atlas Administration API As the apply is happening the state will be PENDING but once the users are ready it should move to APPLIED. This can replace the timeout and should give you a consistent connection flow.

Let us know if you have any other questions.

Best,
Melissa