Rails - Multi Tenant Application with Customization Framework

Multi-tenant rails application: what are the pros and cons of different techniques?

Dont forget about using default scopes, while creating named scops the way you are now works it does feel like it could be done better. I came across this guide by Samuel Kadolph regarding this issue a few months ago and it looks like it could work well for your situation and have the benefit of keeping your application free of some PgSQL only features.

Basically the way he describes setting the application up involves adding the concepts of tennants to your application and then using this to scope the data at query time using the database.

Multitenant application - access for users who do not belong to a specific tenat eg: Customers

The thing is that I don't want and obviously no client would want a seperate login for each tenant here.

They actually do want this, mainly for legal, auditing or security reasons.

Multi-tenancy exactly means the separation of data. So during login or right after that you choose a tenant. After that you only see data exactly of this tenant. There is no break-out later: It's possible to switch to another tenant, but not to merge data of different tenants.

If this is not what you want, consider to redesign your data model: There could be assignments between projects and persons. Customers can have their "own" projects by having a foreign key in projects linking back to the customer. This data model approach differs from using a multi-tenancy approach which is actually a technical means to separate data on row or instance level.

Multi-Tenant Application and Entity Framework

You can achieve this using a wrapper around your DbContext, and override each collection of entities with a where clause.

public class WrapperContext : YourDBContext
{

public override DbSet<YourEntitity> YourEntities
{
get
{
return base.YourEntities.Where(t => t.Tenant_Id == someId);
}
set
{
base.YourEntities = value;
}
}
}

Rails / Multi-Tenancy: Conditional default scope based on a different model's db value / global setting?

I would avoid using default_scope as I've been bitten by it in the past. In particular, I've had places in an application where I want to definitely have it scoped, and other places where I don't. The places where I want the scoping typically end up being controllers / background jobs and the places where I don't want / need it end up being the tests.

So with that in mind, I would opt for an explicit method in the controller, rather than an implicit scoping in the model:

Whereas you have:

class Derp < ApplicationRecord
if Server.first.multitenant?
default_scope { where(account_id: Account.current_id) }
end
end

I would have a method in the controller called something like account_derps:

def account_derps
Derp.for_account(current_account)
end

Then wherever I wanted to load just the derps for the given account I would use account_derps. I would then be free to use Derp to do an unscoped find if I ever needed to do that.

Best part about this method is you could chuck your Server.first.multitenant? logic here too.


You mention another problem here:

This works, but I've got large record sets that I'm querying on this particular standalone client (70,000 records). I've got an index on the account_id, but it took my main customers table from 100ms to 400ms on my development machine.

I think this is most likely due to a missing index. But I don't see the table schema here or the query so I don't know for certain. It could be that you're doing a where query on account_id and some other field, but you've only added the index to the account_id. If you're using PostgreSQL, then an EXPLAIN ANALYZE before the query will point you in the right direction. If you're not sure how to decipher its results (and sometimes they can be tricky to) then I would recommend using the wonderful pev (Postgres EXPLAIN Visualizer) which will point you at the slowest parts of your query in a graphical format.


Lastly, thanks for taking the time to read my book and to ask such a detailed question about a related topic on SO :)



Related Topics



Leave a reply



Submit