Login

Lost your password?
Don't have an account? Sign Up

Multi-tenant Architecture for SaaS

When building Software as a Service (SaaS) you'll often need to use a Multi-tenant Architecture. There are many different ways that you can segregate compute and data storage in a multi-tenant environment. Data storage can be in a silo or partitioned. Compute can be pooled or siloed. And both together you can create lanes for groups of tenants. In this architecture, having the identity of each request is critical in being able to route a request all the way through to the right services and resources.

? EventStoreDB

? Subscribe:

? Join this channel to get access to source code & demos!

? Don't have the JOIN button? Support me on Patreon!

? Blog:
? Twitter:
✨ LinkedIn:

0:00 Intro
0:38 Multi-tenant
3:32 Identity
5:01 Code Example
7:13 Lanes

#softwarearchitecture #multitenant #saas

https://www.saas.place

47 comments

    1. Sergey T

      I have the same question related to a broader topic of this kind asked in one of the earlier commets, too (re intra-group – within one business boundary – multitenancy with more complex filtering scenarios) / this is a really interesting and practical aspect

    1. Mário Nunes

      @Greenthum6 sure there are tradeoffs, all tenants in 1 table probably doesn’t scale as well too.
      What you describe is also hard to do if you have 1 db per tenant, which in my opinion is the way to go.
      But of course, will depend on your requirements…

    2. Greenthum6

      @Mário Nunes I mean the joins between two tenants should NEVER happen. Same goes for queries where accidentally returning rows from wrong tenant may result in legal actions. Messing up is going to happen eventually as any query may break it. One database per one tenant is the only sane option if your application works with business critical data.

      Integration testing and any kind troubleshooting would be difficult if you always need to map a specific tenant to a set of table names since all SQL must be tailored at all times.

  1. Sathyajith P S

    Saas design using Hypermedia is pretty cool, Haven’t thought of Saas that way. Its new to me. Have you/any one you know has really hit the ceiling with pooled & partitioned SaaS architecture and had to move on to Hyper media etc..? I’ve never built a Saas. One thing that really scares me is that pooled and partitioned might not be good enough for the long run and migrating between pooled & partitioned to something else will be a big headache;

    1. CodeOpinion

      Tenants needing to communicate with each other I was going to bring up. But ultimately it’s not very far off as a request from a “client” and a request from tenant are treated the same.

  2. Greenthum6

    I’ve worked most of my career with transactional multitenant systems starting from key-partitioned tables and eventually moving to dedicated databases. The last thing you want to face is to mess up data between tenants. That’s why I would always go for a model with a dedicated database for each tenant. Sharing a database between tenants will eventually result in customers calling and asking why do they see data from a wrong company. And the next, even harder question is are their data compromised also. I’ve seen that happen, believe me you don’t want to be there.

    Database per tenant is also a great benefit when building test automation and CI. You just make sure users are redirected to correct tenant and from there it’s just single tenant system all the way. No need to carry any tenant IDs in every query or making sure all current and future SQL will use it correctly. The obvious benefits include scaling and better performance overall. Not all tenants are equal. There will be that mammoth one slowing others down.

    1. Zarium Sheridan

      I agree. Mixing diferent tenant data in the same DB is a REALLY BAD idea. It’s like “reverse sharding”. You’re basically putting stuff that don’t belong together in the same DB. In the same RDBMS. Usually you want to do the opposite, not only for security, but also for performance reasons. If your services are stateless, your relational database eventually becomes your main bottleneck.

    2. DevilTrigger

      How would you fit this with microservices ? Ideally each microservice should have its own database, so if you have 500 clients and 10 microservices you would end up with 5000 databases, which can be a nightmare

    3. Greenthum6

      @DevilTrigger There is no need to echo the customer data model over the whole system. That would be bad micro service design. If you go with microservices and one or more of your services has hundreds of databases to manage maybe rethink the solution.

    4. effg00glr

      what happens when you have millions of users??? how do you manage the databases? so each user signup is a new database? please share link of how this is done in real life..thanks

  3. Thariq Sulaiman

    hi thanks for the information which very clearly explained just want to know if you have a code sample for “Multi-Tenant pooled & partitioned” which uses seperate identity server to return tenant and then login to client application thanks in advance

    1. CodeOpinion

      Sure, that works. It’s not any different than being silo’ed using different db instances because ultimately you need to know which schema to use (vs which instance to connect to).

  4. mot tahh

    I’m currently working on something closely resemble farms, the different is, I have a service working as a proxy to hold the emails (the main access way) and the associated api that this email belongs to, after that, the presentation (Mobile app and blazor app) uses that api for all subsequent calls until a sign out is made.
    Watching this made me thinking if making a service for only an email check is an overkill, should it have the whole identity system, but I want each tenant to have a different jwt credential, and, with current implementation plan, there is only 2 methods exposed from the proxy, the first to register the email, the other is to check for the email, (Maybe an edit in the future), other methods are for the admin to spin up another tenant, these methods also required their own jwt credentials.

  5. Christian Ista

    About the identity, the user get a token from Identity how to know the tenant to assign to ? My used a login “companyAmyusername”, “companyBmyusername” depending of that I assign the tenant in the token but I think it’s not the best way. What is the best way ?

  6. RS

    I enjoyed this video, thanks. I prefer the approach that uses a separate database per tenant, but I have a concern. When you have to modify the database schema (adding/modifying tables for example), would you have to update each database manually and separately?

  7. Sebastian Perez

    Multi Tenant is nice when your app share the same logic and that’s it . But when maybe a distributor from a different car seller say ” i want to remove one field ” you can change that for just one client , you have to change your entire app for only one client.
    Clients are stupid sometimes.

  8. Thanh Nguyen

    Hi, I think my question is not really related to this topic. But I need advices from experts in this situation:
    – My app allows users define their own objects
    – Should I allow users to define their own real table per each object? Or should I use a big table (for ex: Integer1, Integer2,… String1, String2, …)
    Thanks

    1. Thanh Nguyen

      @CodeOpinion Currently our app is using MSSQL server – it is relational database. But now, users want to define their custom objects. Do you have any advices on how database designing structure for this requirement? Thanks

  9. Essam Al-Mansouri

    How do you go about handling multi-tenancy in an event-sourced app where a consumer should be able to handle events from different tenants? I thought about creating different topics for each tenant, but then I would also need to create consumers that listen to those new topics. How would you go about aggregates in a case where many tenants can share the same topic? Is there a way to do this transparently without each consumer being aware that they are in a multi-tenant environment? I really would prefer not to rely on developers of each service making sure that they check tenant id and handle requests appropriately.

    1. CodeOpinion

      Same as the database really. Depends if you want to share it. You can make it seamless for the developer so they are unaware of where it’s actually being published. Take that concern out of their hands.

Leave a Comment

Your email address will not be published.

*
*