Back

[SOLVED] Multi Tenancy separated by database: saas, schema and maintenance

  • 0
  • Databases
  • Self Hosted
D5
30 May, 2023, 10:08

You asked for resources if I'm not wrong to learn? 😅

TL;DR
The user was seeking assistance with multi-tenancy separation in a SaaS application using MariaDB. They initially wanted to separate data into different tables but learned that separate databases would be a better option. The suggestion was made to create a `master` database with a collection for all tenants and then create collections for each tenant within the `master` database. This would allow for easy querying of data based on the tenant using the application. The problem was marked as solved, with the user planning to create functions to create and update tenants. The user also mentioned the possibility of manually updating existing tenants if necessary. No specific resources were mentioned or requested
TheIlluminatiMember
30 May, 2023, 10:12

I know where to find AppWrite docs, and it is, indeed, well documented. But I'm asking if there are any best practices for creating a SaaS application in AppWrite since information like this isn't something you'd likely find in the docs but more rather on a blog or forum. But I don't think you can help me with my problem. I will close this thread now.

D5
30 May, 2023, 10:13

Oh, yes, I can help you

D5
30 May, 2023, 10:13

And other people too

D5
30 May, 2023, 10:26

@TheIlluminatiMember Tell me firstly what data or what kind of data you will have and I will try to find the best way you could structure it

D5
30 May, 2023, 10:27

Without that I can't determine what is the most optimal solution

safwan
30 May, 2023, 11:10

@TheIlluminatiMember Correct me if I'm wrong.

You want to have a main database that stores the various tenants. Now you also want to have a different database for each tenant, that all have the same structure, correct?

safwan
30 May, 2023, 11:16
safwan
30 May, 2023, 11:16

Just to be sure, this is what you want to do right?

safwan
30 May, 2023, 11:17

If this is what you want to do, I suggest isolating all this behaviour inside of one Database.

safwan
30 May, 2023, 11:20

What you could do is, create a database called master.

Inside master, have a collection called main which stores all your tenants in documents.

You can then create collections for all your tenants inside of the master database, by following a naming convention like tenant-[name/id]. Whatever data you want to store for each tenant, can be done easily in the form of documents, and since it's all inside one single DB, you won't have to worry about referencing a new Database when updating.

TheIlluminatiMember
30 May, 2023, 13:25

What I'm looking for is a structure like this.

D5
30 May, 2023, 13:28

Yes, that's correct. You don't need to have more than 2 databases for that

TheIlluminatiMember
30 May, 2023, 13:28

Yes I do, because in this example I only have 1 tenant named Test

TheIlluminatiMember
30 May, 2023, 13:29

The Light blue database is linked to a tenant in the main database

TheIlluminatiMember
30 May, 2023, 13:29

If I have 2 tenants there should be 2 light blue databases

D5
30 May, 2023, 13:29

You should associate documents with the tennant document with a relationship or manually with an ID In cloud, instead of having independent databases

TheIlluminatiMember
30 May, 2023, 13:33

I may consider going for that approach but I would much rather make use of the databases since it would be very easy to query data from it depending on which tenant is using the application.

D5
30 May, 2023, 13:33

So when you want to get all associated, you basically apply a filter for that

D5
30 May, 2023, 13:33

Yes, but as said, it's not the most optimal

Drake
30 May, 2023, 16:28

Yes, you can use separate databases for each of your tenants. As others have mentioned, it doesn't create a separate mariadb database but is a way to segregate data into different mariadb tables.

I can't place this logic in the function because that would mean when I create a tenant now and update the function later the existing tenants would not be updated.

I think functions are the way to go, but, as you said, existing tenants would need to be updated. maybe manually...maybe your function can update the existing ones too. it all depends on you.

TheIlluminatiMember
31 May, 2023, 06:37

Thank's for clearing things up for me @Steven! I'll create functions to not only create but also update tenants. I will mark this as solved now.

TheIlluminatiMember
31 May, 2023, 06:37

[SOLVED] Multi Tenancy separated by database: saas, schema and maintenance

D5
31 May, 2023, 08:56

But collections don't separate data into different tables?

Drake
31 May, 2023, 12:23

Each collection is a separate mariadb table

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more