Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multi tenancy with tenant sharing data

I'm currently in the process of making a webapp that sell subscriptions as a multi tenant app. The tech i'm using is rails.

However, it will not just be isolated tenants using the current app.

Each tenant create products and publish them on their personnal instance of the app. Each tenant has it's own user base.

The problematic specification is that a tenant may share its product to others tenants, so they can resell it.

Explanation :

FruitShop sells apple oranges and tomatoes.
VegetableShop sells radish and pepper bell.

Fruitshop share tomatoes to other shops.

VegetableShop decide to get tomatoes from the available list of shared items and add it to its inventory.

Now a customer browsing vegetableshop will see radish, pepper bell and Tomatoes.

As you can guess, a select products where tenant_id='vegetableshop_ID' will not work.

I was thinking of doing a many to many relation with some kind of tenant_to_product table that would have tenant_id, product_id, price_id and even publish begin-end dates. And products would be a "half tenanted table" where the tenant ID is replaced by tenant_creator_id to know who is the original owner.

To me it seems cumbersome, adding it would mean complex query, even for shop selling only their own produts. Getting the sold products would be complicated :

select tenant_to_products.* 
where tenant_to_products.tenant_ID='current tenant' 
AND (tenant_to_products.product match publication constraints) 

for each tenant_to_product do
   # it will trigger a lot of DB call
   Display tenant_to_product.product with tenant_to_product.price

Un-sharing a product would also mean a complex update modifying all tenant_to_products referencing the original product.

I'm not sure it would be a good idea to implement this constraint like this, what do you suggest me to do? Am I planning to do something stupid or is it a not so bad idea?

like image 834
Syl Avatar asked Feb 20 '14 17:02

Syl


People also ask

Is multi-tenant secure?

This means that each user has specifically limited privileges to access a single shared database with other tenants' data encapsulated there. In fact, this risk is possible while utilizing a single shared database for multiple tenants. The multi tenant software architecture leads to higher data breach risks.

What are the data isolation options available if the client decides to go with multi-tenant cloud model?

Separate Databases or Shared Database. Once we've identified the tenant specific data and the tenant shared data, the next step would be to decide between the two main strategies for storing data: Separate Databases one for each tenant, or. a Shared Database used by all tenants.

How do you implement a multi-tenant database?

We can implement multi-tenancy using any of the following approaches: Database per Tenant: Each Tenant has its own database and is isolated from other tenants. Shared Database, Shared Schema: All Tenants share a database and tables. Every table has a Column with the Tenant Identifier, that shows the owner of the row.

What is multi-tenancy in data center?

A multi-tenant data center, also known as a colocation data center, is a facility in which organizations can rent space to host their data. Businesses can rent to meet varying needs, from a server rack to a complete purpose-built module.


1 Answers

You are going to need a more complicated subscription to product mechanism, as you have already worked out. It sounds like you are on the right track.

Abstract the information as much as possible. For example, don't call the table 'tenant_to_product', instead call it 'tenant_relationships', and have the product Id as a column in this table.

Then, when the tenant wants to have services, you can simply add a column to this table 'service Id' without having to add a whole extra table.

For performance, you can have a read-only database server with tenant relationships that is updated on a slight delay. Azure or similar cloud services would make this easy to spin up. However, that probably isn't needed unless you're in the order of 1 million+ users.

I would suggest you consider:

  • Active/Inactive (Vegetable shop may prefer to temporarily stop selling Tomatoes, as they are quite faulty at the moment, until the grower stops including bugs with them)

  • Server-side services for notification, such as 'productRemoved' service. These services will batch-up changes, providing faster feedback to the user.

  • Don't delete information, instead set columns 'delete_date' and 'delete_user_id' or similar.

  • Full auditing history of changes to products, tenants, relationships, etc. This table will grow quite large, so avoid reading from it and ensure updates are asynchronous so that the caller isn't blocked waiting for the table to update. But it will probably be very useful from a business perspective.

EDIT:

This related question may be useful if you haven't already seen it: How to create a multi-tenant database with shared table structures?

like image 176
Aidan Avatar answered Nov 25 '22 00:11

Aidan