Hi, I am using managed MongoDB database and code is written in the Nodejs, I am planning to setup SAAS multi tenancy applicaiton. How I can setup database or how should be the in architecture?
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.
Hi there,
This is a great question! As with everything, there are multiple ways of doing this, I recently wrote an article on how to do this with Laravel and Postgres:
The Laravel ecosystem offers multiple packages and different approaches on how to achieve this. For MongoDB and Node.js, I’d be happy to provide some insights and recommendations for your architecture:
When it comes to multi-tenancy, there are usually three main approaches:
a) Database per Tenant:
b) Collection per Tenant:
c) Shared Collections with Tenant Field:
For a SaaS application on DigitalOcean’s Managed MongoDB, I could suggest the “Shared Collections with Tenant Field” approach. This method balances scalability and simplicity. But at the end it comes to personal preferences of course.
Here’s a basic architecture for your Node.js application:
App Platform makes it easy to deploy and scale your Node.js application:
App Platform can automatically handle scaling and load balancing for you.
Besides that, as with any application keep in mind the following:
Besides the above, here is an example on how you could achieve this architecture on the database side, we’ll use a single MongoDB database with shared collections and a tenant field for data isolation.
a)
users
b)books
c)tenants
a) Tenants Collection:
b) Users Collection:
c) Books Collection:
To optimize query performance, you should create indexes on frequently queried fields:
When querying the database, always include the
tenantId
to ensure data isolation:In your Node.js application, you can create a data access layer that automatically includes the
tenantId
in all queries. Here’s a simple example:When a new tenant signs up, you’d create a new document in the
tenants
collection and set up any initial data they need:If you prefer to use separate databases, here is a good article on how you could do that:
Hope that this helps!
- Bobby