r/softwarearchitecture • u/MindlessRespect5552 • 3d 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 .
25
Upvotes
19
u/oliverpalonkorpi 3d 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.
3
u/beginetienne 3d 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 !
6
u/gshonia 3d 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/Any-Appointment-814 3d 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.
4
u/dbrownems 3d 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 3d ago edited 3d 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
2
u/zdzisuaw 3d 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 2d ago edited 2d 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 3d ago
Postgres with tenant isolation
1
u/funbike 3d ago
We tried multiple databases and it was a nightmare. We combined them into a single database using a
client_idcolumn to discriminate rows.We put custom client behavior in hooks, filters, and chain-of-command objects.
1
u/Ontomatogit 2d 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 2d 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 ...