NodeJS Express with separate databases

Hi community,

We have a NodeJS + Express Rest API application that manages requests for multiple clients. We have separated our customers data in own databases. Based on the incoming request, we can easily decide which database we have to use.

Now let’s consider how we can aggregate database connections to avoid high memory consumption:

  1. The simplest but, in our opinion, least performant solution: We create a new connection to the customer database per REST call in order to access customer-specific data.

  2. We create a Mongoose instance and switch databases using .useDb() as follows:

const db = mongoose.connection.useDb("company")
const collection = db.model('...

Is it ensured that the data does not mix if there are two incoming connections from different customers at the same time and the database is changed in between?

  1. The same database is used but different collection prefixes are used. However, there is no easy way to determine data consumption per customer (compared to database size).

  2. Another consideration was to introduce a “customer key” and store all data in the same collection. However, a level of security feels lost there. Same loss of consumption reporting feature.

Are there any better approaches? Any ideas about this?

I’m looking forward to a swarm of knowledgeable reactions :slight_smile:

Greetings
Timo

Hello @tkempken, Welcome to the MongoDB community forum,

I think you can use useDb method to switch the database because it uses a single connection pool, it does not mix data because nodejs is built on a non-blocking, event-driven architecture, which allows it to handle multiple concurrent client requests without conflicts.

I am writing an article about multiple Mongodb connections in a single application, but currently, I have this example in GitHub.

You’ve refactored your application into a multi-tenant setup, mainly to ensure that each client has access only to their specific data. While implementing multi-tenancy with a “customer key” is relatively straightforward, the crucial aspect is how you plan to utilize and manage this data.

As your user base grows, you’ll likely encounter challenges related to data and schema merging. Ensuring proper data segregation and access control from the outset is essential to avoid complications as your system scales.

For example if you pan to provide data for using in some power BI - different data makes your life easer.