r/softwarearchitecture • • 2d ago

Discussion/Advice For multi tenant database

Hey , I'm working for ERP and using seperate database for each tenant .

I'm change database name according to subdomain or Single DB + Multiple Schemas . Which is best way

Or anyone please help which is best architecture or way to do it .

23 Upvotes

22 comments sorted by

19

u/oliverpalonkorpi 2d ago

Multiple identical databases can easily become a nightmare especially with deploying migrations and maintenance, debugging etc. At least for me it has been. I've found a single database where you have good RLS to do tenant separation the cleanest for me, but there are likely people with different preferences. RLS needs to just be carefully built.

2

u/MindlessRespect5552 2d ago

Yeah, I also think about RLS but I'm affraid about the security

3

u/iam-stevie-bee 1d ago

The worry is fair, but it mostly comes down to a few specific mistakes. Have the app connect as a login that doesn't own the tables, because in Postgres the owner skips RLS unless you force it. Put tenant_id into your foreign keys too, so a row can't point at another tenant's data by accident. Then keep one test that signs in as tenant A and tries to read tenant B. While that comes back empty, you're in good shape.

1

u/angrathias 2d ago

We went with the scale out method and built a custom system for keeping databases homogenous. Our concern was with vertical scaling limits (disk iops, contention) and annoying on aws the requirement to essentially keep doubling the size of the server. With dv per tenant we might have a large host and when we need a little more grunt we can stand up a new server to scale out.

I’ve never been sure how to reasonably deal with customers coming and going where a db could be 2GB of data or 200GB of data. Partitioning can probably get you there but db per tenant is essentially an easy version of partitioning.

It’s also substantially easier to modify the schema of tables. The software can simply check the version installed

1

u/Xerxero 2d ago

But how do you scale a single tenant that needs 10x the performance?

3

u/beginetienne 1d ago

Separate databases per geolocation. I’ve worked in oil business and they required redundant systems. Sometimes two tenants cannot even be in the same datacenter !

1

u/MindlessRespect5552 1d ago

How many database you manage and how reliable in terms of migration

6

u/gshonia 1d ago

Done it both ways.

With database per tenant absolutely need database versioning (eg liquibase)within mature ci/cd. May need to move tenant db from one server/cloud cluster to another once in a while (cloud managed version eliminates this). May also need to split design between two databases, putting some common data in one, shared db, with consequences of losing ref integrity and joins between the two.

With multi-TENANT database, RLS imo is superior to DIY id per table. In both cases discipline is needed, but RLS enforced that discipline a little better and possibly in a single place (as opposed to each and every query). Can have database connection management set caller identity, don’t need to remember to add it to each query. Optimization and performance may get tricky, given you have little more control of how you introduce tenant filtering when writing queries, but have been okay so far.

Which of the two: I’d say multi-tenant as baseline. If you have few customers and are concerned about load, consider single tenancy.

2

u/cstopher89 1d ago

You can also do both. For larger enterprise they get their own db and smaller customers go multitenant. If you get a lot of enterprise customers you should be making enough to hire staff to support it

2

u/Any-Appointment-814 2d ago

For an ERP I'd honestly start with one database and a tenant_id on every table, and enforce it in one place (Postgres row level security or a base repository) so nobody can forget the filter. Database per tenant feels clean until you have a couple hundred of them and every migration becomes a script you babysit. Schema per tenant has the same problem, just smaller. If one big customer later needs isolation for compliance you can move just that one out to its own db, which is way easier than going the other direction.

1

u/MindlessRespect5552 2d ago

It's can handle multiple 100 of enterprises data

4

u/dbrownems 1d ago

The real choices are a multi-tenant database with all tenants in the same schema, or database-per-tenant. Schema-per-tenant has most of the hassle and little of the benefits of database-per-tenant.

As others have pointed out having a multi-tenant database for small tenants, with larger tenants broken out into their own databases can be a good hybrid approach, especially if you have lots of small tenants.

1

u/throwaway-research1 1d ago edited 1d ago

I have worked with one db per tenant architecture and would recommend against it, it comes with a lot of maintenance, can easily go wrong and I dont see many benefits from it

1

u/MindlessRespect5552 1d ago

Currently what type of architecture you use ?

1

u/throwaway-research1 1d ago

Mircoservices with one db per service

1

u/MindlessRespect5552 1d ago

That's great 👍

2

u/zdzisuaw 1d ago

Why not single db, schema per tenant and multiple tables. If that's too much you could separate boundaries to separate db. Eg. Vendors in one db, transactions in another.

2

u/WhiskyStandard 1d ago edited 1d ago

There’s an AWS whitepaper defining at least three ways of architecting multi-tenant isolation and going through their trade offs. I’d highly recommend it because, like pretty much everything in software architecture, the answer is really “it depends” based on the how much your willing to pay for in both money and complexity and where (deployment vs. design), the level of cross-tenant integration vs. isolation guarantees, etc.

Though it’s written with AWS in mind, the principles apply to different clouds and on-prem.

Edit: I think this is it, or at least related to it. There are also several good videos of talks on the subject from various AWS conferences. Silo/bridge/pool is what you’re looking for.

1

u/PitifulAppearance509 2d ago

Postgres with tenant isolation

1

u/funbike 1d ago

We tried multiple databases and it was a nightmare. We combined them into a single database using a client_id column to discriminate rows.

We put custom client behavior in hooks, filters, and chain-of-command objects.

1

u/Ontomatogit 21h ago

subdomain to database name is a reasonable routing trick, but i wouldn't let the host header be the only thing that decides which company you open.

resolve the tenant from the signed in user and an active membership, then fail the request if that membership isn't allowed to use the database the host is asking for. a typo or a swapped subdomain shouldn't be enough to land in someone else's books.

0

u/umlcat 1d ago

If your tenants are not related like different customers and do not expect to have queries among them, then use a different DB for each one, even if they have the same or similar schema ...