Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NestJs Design Problem: How can I avoid creating a Nodejs Instance for each team?

I made a CRM app using NestJs with Nodejs. I designed it in a way that each team has its own database because every teams data is difference and has no relation with other teams and also it made the process of back up much easier.

However, Now that I want to deploy my service I noticed that for each team I must create a separate nodejs Instance which makes ram usage very high. Imagine just for 10 teams I may need around ~500MB ram which will hurt me economically even in short run.

Solutions

I used TypeORM in NestJs so the first thought I had was to find a way to have multiple databases (not multiple connections) having them sharing same schema but dynamicly use one of them based on request's scope and details. Which seems the best solution so I can avoid creating another NodeJs instance and in same time I now have seperate database for each team.

I read nestJs and TypeORM documents but didn't found any way to accomplish that. So my other solution was to just use one database for everone and add something like team_id column to each table to make a filter data for each team.

Is it a good way?

Is there any other solutions to use one nestJs instance but with same schema for multiple databases?

like image 313
omidh Avatar asked Jul 28 '20 18:07

omidh


1 Answers

I recommend to use one database.

The database can have a table saving all of the teams and other tables will have a new team_id column as you think.

One database for each team has disadvantages.

  • Multiple DB Connections

Since you need to use same Entities for all of the databases for the teams, you cannot use Single Database Connection. According to every incoming API request, the server will have to switch db connections.

  • DB Configuration in TypeORM

For multiple databases, the configuration will be looking like below:


imports: [
    ...,
    TypeOrmModule.forRoot({
        name
        type
        host
        port
        username
        password
        ...
    }),
    TypeOrmModule.forRoot({
        name
        type
        host
        port
        username
        password
    }),
    ...
]

If you need to add a new team, you have to update your code base for adding a new db for the team and have to redeploy your application. (maybe you will create a new database and perform migration too?)

  • Backup

I agree with you that it's better to backup a single team with multiple databases. But how about when you want to backup all teams? In most of cases, I believe it will need to backup all teams, not just a specific team.

  • Teams Management

Where do you save a team's information? How to know what team has what db? Maybe you saved teams somewhere(in a separated db?). To know which database connection should be used in each request, it needs to make a new query?

  • Cost

If there are 100 teams, you are gonna make 100 databases? Also each application has development and production environment. In some cases, there can be more environments like staging. 2 envs will double the number of dbs.


Conclusion

Of course there will be a way to automate some of the items in the above list and it's still possible to use multipe databases in NestJS + TypeORM for your project but it looks not a good way and not a worth effort for your project.

I have seen some big multi-tenant applications (like grafana) and they weren't using multiple databases strategy.

like image 159
topmoon Avatar answered Sep 17 '22 14:09

topmoon