Search code examples
mongodbgrailsgrails-orm

What is the difference between multiple data sources and multi tenancy?


In context of mongodb and gorm, If we need to have different databases for different clients, then are multi-tenancy (With Database mode) and multiple data source approach are 2 solutions to achieve the same thing or is there any difference between them?

Multiple Data Source Solution: http://gorm.grails.org/latest/mongodb/manual/#multipleDataSources

Multiple Tenant Solution: http://gorm.grails.org/latest/mongodb/manual/#multiTenancy


Solution

  • Well they are not meant to achieve the same purpose

    tldr; Multiple Data Source is meant to have different databases (collections if you only plan to use mongodb) for different objects while Multi Tenant will store the same object but add a discriminator to identify client specific data.

    If your question is about supporting different databases for different clients the answer will be to go with multi tenant

    Multiple Data Sources

    Grails supports (for long) to have multiple database for the same application (it can be different db vendor or different db from same vendor). The purpose is to have specific data stored in a different db/namespace.

    For example, you can decide to have a db for all core entity of your business and to have a dedicated db for all audit/logging things. When using multiple data sources you will map an object to a dedicated datasource

    Multi Tenancy (with database tenant as per OP context)

    In mutli tenancy (database tenant) on the other hand, grails will have a single database schema for your client to store all the objects. so data from Client A will be in another db than Client B. Grails will have some default tenant resolver (that you can still override if needed) which will manage to determine which database needs to be queried depending the context.