dotnetcore / CAP

Distributed transaction solution in micro-service base on eventually consistency, also an eventbus with Outbox pattern
http://cap.dotnetcore.xyz
MIT License
6.62k stars 1.28k forks source link

Support database isolated multi-tenancy #699

Open mertyildiz41 opened 3 years ago

mertyildiz41 commented 3 years ago

Hi, When using microservice architecture, event process at the same time with different tenant, may better to archive this information on the header and using multi-tenancy database architecture.

Is it possible to open a new branch for this feature?

yang-xiaodong commented 3 years ago

Hello,

I am not sure what kind of multi-tenant architecture is used in your project. In our practice, the tenant id is stored in the message header, and then the consumer retrieves it from the header.

You can think that message-based and HTTP-based(request-response) have familiar processing methods.

Let me know what you think?

mertyildiz41 commented 3 years ago

Hello yang-xiaodong,

I sending event for calculation and after whole calculation done i gonna process calculation result, I don't know is it right way but i thinking to follow Received table to event is Success. But multi-tenant is block me is that way. i can't select Received table without tenant id and also hard to where clause inside of content json. Do you have any idea how i know whole events is done after i sending million events. I'll be pleased :)

yang-xiaodong commented 3 years ago

I got it, it seems that querying by tenant-id in json is indeed not a good idea, we will consider support for multi-tenancy.

At present, I think you can create a mapping table, map the Id of the Received table to the tenant Id, and then use the sql join query to get the data you want.

You can get Received table Id using the following code:

[CapSubscribe("xxxxx")]
public void Subscriber2(Person p, [FromCap]CapHeader header)
{
   // received table id
    var id = header[Headers.MessageId]; 
}
ztzsws1 commented 3 years ago

Hi @yang-xiaodong , I read the above discussion and it seems that @mertyildiz41 his multi-tenant architecture is based on table or row isolation.
So you plan to add tenantId column in publish and receive table for support multi-tenancy? Do you plan to support a multi-tenant architecture with database isolation? Thanks,

yang-xiaodong commented 3 years ago

Hello @ztzsws1 ,

The currently architecture unable to support a multi-tenant system with database isolation.

If you have any ideas, please feel free to drop them here

colinin commented 3 years ago

I think it's possible to store the identity of a tenant in Message.Headers and use static fields to allow users to change the identity name of a tenant

mertyildiz41 commented 3 years ago

I think static fields, not the correct way. Bcz with every message delivery with the different tenants. I actually taking tenant info from the user credential. Better to run out from authentication to message header.

matengo commented 3 years ago

With our previous messaging solution we attach tennant info to each outgoing message, and then on the reciever side, we have a pipeline before the message end up in the recever function, where we take the tennant info from the message and create a new scope (using (var scope = _serviceProvider.CreateScope()){}) get a scoped tennant info class from the DI container, set tennant info to the class, and then call the reciever function.. Then in the reciever function, and everywhere in the scope of this message we have an injectable tennant info class... Would something like that be possible?

teomane commented 2 years ago

The article "Using CAP library in a multi-tenant application via SubscribeFilter" may give an idea about multitenancy

witskeeper commented 4 months ago

Perhaps this project is a solution:

https://github.com/netcorepal/netcorepal-cloud-framework/tree/main/src/Context.CAP