6 ms·
In my experience, a lot of projects start off with shared tenancy (using a tenant id discriminator in the database) and eventually migrate to completely separat
by ComputerGuru 7y ago
In my experience, a lot of projects start off with shared tenancy (using a tenant id discriminator in the database) and eventually migrate to completely separate schema for each tenant (i.e. a separate “installation” of a single-tenancy app for each tenant) to avoid security and architectural pitfalls.
Once you’ve done that, you don’t have to worry about forgetting a `WHERE tenantId = foo` somewhere and leaking data. The actual code is shared (i.e. not installed separately or in separate processes) and you would simply use your dependency injection or equivalent to resolve the DB connection contingent on $user’s validated claims.
“Networking” or sharing entities across tenants becomes harder this way.. but purer/cleaner. You essentially create a federation layer and communicate across instances via a hard code wall (via network requests to yourself and an API) rather than “in memory” which is less efficient and requires a lot of boiler plate, but it’s going to be what you’re going to end up doing anyway.
- smileysteve 7y ago> you don’t have to worry about forgetting a `WHERE tenantId = foo I found a few tools early on that work for this in Rails. You can guarantee all of your sql includes the tenant. - Apartment - Tenancy There's also several for the PHP ecosystem.
- gfodor 7y agoInterestingly, I've only seen the opposite, going back to the 90s. Eventually managing N schemas ends up being difficult, and you're also fighting against the database.
- awinder 7y agoYeah 100%. Multi-tenant as implemented this way with separate infrastructure works ok for very small values on N. Once you’re into N > 100s or 1000s then you’re absolutely not reducing complication.
- TekMol 7y agoWHERE tenantId = foo You only need to do that if you keep multiple tennants in one table. But who does this? Usually in a multi tennant application you have only one line which connects to the tennants DB and thats it. The rest of the code does not have to think about tennants.
- hombre_fatal 7y ago> But who does this Everyone below a certain scale. Or when it just makes more sense, like Proboards' free tier.
- JamesBarney 7y agoThe approach I've used and seen the most often is enforcing the tenancy at the ORM level so someone can't forget a where. And then providing enterprise service for large companies that want their own installation.
- Joeri 7y agoOn the enterprise software I worked on (IWMS system) we did both. Customers got their own schema, but big customers would give each branch their own virtual tenant within that schema. We started out with only the multiple schemas approach and ended up retrofitting a bunch of multitenancy features.
- thrownaway954 7y agousing an ORM it's very simple. once you setup the relationships between models, you always use the highest level model as the entry point of your query and the ORM will scope the query for you. ie in Rails, if your highest level model is Account you just always use @account.customer.find(18). the Rails ORM will automatically scope the query to the current account. using multiple schemas becomes a nightmare when you want to perform a migration to alter the database schema. you have to somehow perform a migration across every single schema without breaking anything. god forbid you need to rollback. have everything in one database makes it really easy. as please, don't talk how using a single database restricts scaling later on. with how far databases have come and how cheap cloud computing is, scaling isn't an issue anymore.
- HatchedLake721 7y agoThe highest level entry point makes so much sense. I’ve been building multitenant saas for last 3 years enforcing “where tenant = x” and you’ve just blown my mind.
- hackerm0nkey 7y agoOn more than one occasion I've started with a simple table for all tenants (where tenant = x) because we didn't know at the time if we will have many. Then progressively when we understood how the service will be used, refactored to keep separate tenant's data separate. Ended up with each tenant in a given schema, we were using Java and relied on Hibernate (ORM) to help connecting to the correct DB (it does support multi-tenancy) as well as on a DB migration tool (Liquibase) so any changes we made to the schema get replicated over all tenants.
- thrownaway954 7y agothat's why we're here... learning something new everyday is a great things. now imagine how cool it will be implementing this is your projects from here out :)
- hackerm0nkey 7y agoI come more from a JVM world. But aren't there any DB migration/refactoring tools in Rails to allow for changes to propagate across tenants ? E.g for the JVM, there's Liquibase and Flyway, and I agree with you, setting that up took a good part of a couple of weeks of dedicated effort.
- gitgud 7y agoIn my limited experience, companies start with single-tenancy apps. Get a client interested in their own private version, so they spin-up another instance. Then they realise they've got a growing number of databases and webservers to maintain and account for, so they combine them all into a multi-tenant system.
- forgingahead 7y agoAre separate schemas really the right way to handle this? The folks who wrote the Apartment gem for Rails, a multi-tenancy solution that works on separate schemas, wrote about some troubles when scaling: https://influitive.io/our-multi-tenancy-journey-with-postgres-schemas-and-apartment-6ecda151a21f https://influitive.io/our-multi-tenancy-journey-with-postgre...
- mguerville 7y agoThanks for this clear explanation. I’m a non developer currently experimenting with no code apps, I’m using userid filtering to segregate data between users and it feels “wrong” but it’s reassuring to see that it’s a pretty normal starting point before having a more mature architecture
- Roritharr 7y agoI'm contemplating writing an article about "things that are bad but you should absolutely do anyway when you start" to help with this analysis paralysis. Interested?
- mguerville 7y agoSorry I just saw this (usually just lurk HN so not up to speed with my comment responses), yep interested to help if you'd like
- mattmanser 7y agoIts a while ago now and I have no idea what post it was for, but there was a discussion here on HN about how this is a terrible idea as it turns into a complete mess and a nightmare to manage. I've only done this at one company, and they self-hosted, and it was indeed a complete nightmare to manage, but part of that was the self-hosting part.