News for hackers. Stuff that matters.
Re:Ask HN: How come there is no example code for B2B-SaaS apps? by ComputerGuru on Saturday May 02, 2020 @10:22PM (#23056395)
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.

Re:Ask HN: How come there is no example code for B2B-SaaS apps? by thrownaway954 on Sunday May 03, 2020 @04:20AM (#23057679)
using 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.

Re:Ask HN: How come there is no example code for B2B-SaaS apps? by sleepyhead on Sunday May 03, 2020 @08:34AM (#23058613)
> the Rails ORM will automatically scope the query to the current account.

While that is true for simple queries it is not a valid statement for secure multi tenancy.

For example in this scenario: Account (Id). Order (Id, AccountId, CustomerId). Customer (Id, AccountId). You need to ensure Order can only use CustomerId that belongs to Account.

Rails does not include support for restricting deep relationships (grand children) to an account. Basically an attacker can misuse foreign keys from other accounts in new records (add a CustomerId from another Account on Order) or read data in relationship queries when initiating objects (show customer info from another account in an order).

There are ways of mitigating this but it requires doing more complex manual validation methods.

> using multiple schemas becomes a nightmare

There are gems that will solve some of this complexity but it is a risk. The most popular one is somewhat unmaintained.

Re:Ask HN: How come there is no example code for B2B-SaaS apps? by hackerm0nkey on Sunday May 03, 2020 @07:26AM (#23058360)
I 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.

Re:Ask HN: How come there is no example code for B2B-SaaS apps? by HatchedLake721 on Sunday May 03, 2020 @04:51AM (#23057803)
The 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.
Re:Ask HN: How come there is no example code for B2B-SaaS apps? by hackerm0nkey on Sunday May 03, 2020 @07:22AM (#23058344)
On 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.

Re:Ask HN: How come there is no example code for B2B-SaaS apps? by thrownaway954 on Monday May 04, 2020 @12:07PM (#23069702)
that'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 :)
Re:Ask HN: How come there is no example code for B2B-SaaS apps? by mattmanser on Sunday May 03, 2020 @01:05PM (#23060283)
You can't really write reports or BI dashboards in ORMs, unless you want terrible performance.

So for simple queries, fine, but anything else this won't work.

Re:Ask HN: How come there is no example code for B2B-SaaS apps? by thrownaway954 on Monday May 04, 2020 @12:06PM (#23069678)
no kidding and you're being unreasonable... yes, what ii stated will get you 99% of the way there. what you're belly aching about is the 1% where you have to veer from this and do it yourself. big deal.

look... you wouldn't forego taking a plane across the country cause you have to take a cab the last 2 miles to the hotel. same thing here. just because something falls out at the last 1% doesn't mean it a bad way to do things.

Re:Ask HN: How come there is no example code for B2B-SaaS apps? by gfodor on Sunday May 03, 2020 @12:28AM (#23056862)
Interestingly, 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.
Re:Ask HN: How come there is no example code for B2B-SaaS apps? by awinder on Sunday May 03, 2020 @10:20AM (#23059138)
Yeah 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.
Re:Ask HN: How come there is no example code for B2B-SaaS apps? by forgingahead on Sunday May 03, 2020 @04:49AM (#23057791)
Are 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-postgre...

Re:Ask HN: How come there is no example code for B2B-SaaS apps? by gitgud on Sunday May 03, 2020 @04:47AM (#23057781)
In 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.

Re:Ask HN: How come there is no example code for B2B-SaaS apps? by smileysteve on Sunday May 03, 2020 @12:21AM (#23056838)
> 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.

Re:Ask HN: How come there is no example code for B2B-SaaS apps? by mattmanser on Sunday May 03, 2020 @01:03PM (#23060265)
Its 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.

Re:Ask HN: How come there is no example code for B2B-SaaS apps? by Joeri on Sunday May 03, 2020 @03:28AM (#23057472)
On 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.
Re:Ask HN: How come there is no example code for B2B-SaaS apps? by mguerville on Sunday May 03, 2020 @07:30AM (#23058381)
Thanks 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
Re:Ask HN: How come there is no example code for B2B-SaaS apps? by Roritharr on Sunday May 03, 2020 @08:06AM (#23058513)
I'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?

Re:Ask HN: How come there is no example code for B2B-SaaS apps? by mguerville on Thursday May 14, 2020 @09:29AM (#23179227)
Sorry 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
Re:Ask HN: How come there is no example code for B2B-SaaS apps? by JamesBarney on Sunday May 03, 2020 @02:46AM (#23057341)
The 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.

Re:Ask HN: How come there is no example code for B2B-SaaS apps? by TekMol on Sunday May 03, 2020 @01:26AM (#23057045)

    WHERE 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.
Re:Ask HN: How come there is no example code for B2B-SaaS apps? by hombre_fatal on Sunday May 03, 2020 @03:30AM (#23057479)
> But who does this

Everyone below a certain scale. Or when it just makes more sense, like Proboards' free tier.