I am interested in the community's thoughts around the best practice Mongo Atlas multi-tenant approach for an app that I am currently working on. There is surprisingly little information on the approach based on my research so far.
The web app I am working on is a basic early state prototype built on nodejs, express, react, typescript and is using Mongo Atlas for the database whilst using Auth0 for authentication. I would like to get the data architecture correct before proceeding with building the app which will follow a SaaS product model.
Current State
I have built the app to allow user registration, authentication, update user profile. Users can currently register which creates the user in Auth0 and creates a user details record (name, phone number, email address etc) in a Mongo Atlas document with the following structure:
Mongo Atlas Cluster
--users (Database)
--users (Collection)
--user_record (Document)
Here is an example of a user record document:
_id:ObjectID("5ced24301914b800066d629b")
given_name:"James"
family_name:"Test"
email:"email@gmail.com"
password:"$$6ag2$xVFSPdbhekW9Dx.opGJlluytGb9Ktka5uZshUg4Hw0GmOt4N/wAi."
addressLineOne:"Line 1"
addressLineTwo:"Line 2"
city:"New York"
company:"Company Name"
country:"USA"
postCode:"10001"
telephone:"555 789 987"
Future State - Multi-Tenancy
When a new user registers I am considering creating a new database to be created on the cluster which will isolate most app data from all other tenants. Each tenant's database will include different collections and documents in future to manage their specific app settings, user permissions, project data etc.
The primary benefits will be:
- Highly Scalable
- Isolate data from other tenants
The Mongo Atlas cluster structure would look like this example where there are 3 tenants each with their own dedicated tenant database:
Mongo Atlas Cluster
--tenant_tenant1 (Database)
--tenant_tenant2 (Database)
--tenant_tenant3 (Database)
--user_management (Database)
--user_management (Collection)
--user_record_1 (Document)
--user_record_2 (Document)
--user_record_3 (Document)
The user_record document schema could be extended to identify the user's tenant database when the user logs into the application as per this example:
_id:ObjectID("5ced24301914b800066d629b")
tenant_db:"tenant_tenant1"
tenant_db_user:"dbuserid"
tenant_db_password:"%26ag2$xVFSPdbhekW9Dx.opGJlluytGb9Ktka5uZshUg4Hw0GmOt4N/wAi"
given_name:"James"
family_name:"Test"
email:"email@gmail.com"
password:"$$6ag2$xVFSPdbhekW9Dx.opGJlluytGb9Ktka5uZshUg4Hw0GmOt4N/wAi."
addressLineOne:"Line 1"
addressLineTwo:"Line 2"
city:"New York"
company:"Company Name"
country:"USA"
postCode:"10001"
telephone:"555 789 987"
In order to achieve this, I believe the Mongo Atlas API would have to be utilised as a standard Mongo connection string using Mongoose is specific to an individual database. The Mongo Atlas API allows database creation in addition to almost all Mongo Atlas features.
Each tenant database will be extended to include tenant specific collections and documents as I build out the app.
My main questions are:
- Do you think this is a good approach when setting up multi-tenancy for an app using Mongo Atlas?
- Do you think there is a better or different approach?
- Do you see any potential pitfalls with this architecture later on?
Thanks for your help and looking forward to your thoughts on this.
James