1

I am beginner-level developer and I was looking for multi-tenancy app design with NodeJS.

In General, I have understood big picture, how to design database, how to create DB for new accounts and etc. The only thing that is not clear to me is how to handle connections to each database when the number of the customers is very big.

I could find couple of beginner tutorials for multi-tenancy and what all they were doing was to create all DB Connection instances (For example with Knex) and store it in object which is then stored in CLS and accessed by all other callbacks. I though that what if the customers are more than 10K and storing all those connection instances should not be efficient. I wrote sample code where for each request I create a new connection and destroy it at the end but then while surfing in StackOverflow, I saw it being mentioned as very cost effective way.

I wonder what is general idea of handling different database connection for each request?

I only want to have brief explanation and refence documents to read if possible. Thank you!

1 Answer 1

0

This is a job for connection pooling.

When you use a connection pool for connections to your RDBMS, getting and releasing database connections becomes very cheap and you no longer have to worry about caching them. You get to use fully-debugged connection pooling code to do that.

Ordinarily in multitenant apps like yours served by single nodejs servers, you use

  1. only one set of database credentials to access the database, no matter which tenant you're serving with each request.

  2. only one set of tables to hold data for all your tenants.

  3. some sort of tenant_id column in each table containing tenant data, so you can tell which data belongs to each tenant. So your app queries filter on WHERE tenant_id = ###whatever## as well as your other filter criteria.

If you create new tables (either in new databases or all in the same database) for each new tenant, that presents a scalability problem far worse than managing your connections in your nodejs server. Each separate table has significant overhead. If you add new tables for each new customer, your RDBMS server will become overwhelmed just as your app is getting popular. Not a good plan!

2
  • 1
    Multi-tenancy usually means a different database per customer; not just because it's standard practise, there are a lot of regulatory frameworks where this is explicitly required. -1 on the WHERE tenant_id = x solution. Sep 20, 2021 at 13:25
  • 1
    @nicholaswmin Certainly some multitenant setups require separation of data. But I'm not sure about your use of the word "usually." I've worked on successful multitenant apps for both health care and banking that used the approach in my answer. They passed ISO 27001 and every regulatory requirement the customers could imagine to throw at us. And those customers have very vivid imaginations.
    – O. Jones
    Sep 20, 2021 at 13:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.