Q59 [Scenario]: Design a multi-tenant SaaS architecture in MongoDB. Should you use single collections, separate databases, or separate collections?
- Shared Collection (Discriminator/Tenant ID): Best for scale and operational simplicity. Filter all queries by
tenantId(addtenantIdas leading key in compound indexes). - Separate Databases per Tenant: Best for strict compliance, data isolation, and easy backup/restore per customer.
- Separate Collections: Anti-pattern if tenant counts reach thousands (exceeds namespace limits).
Key Takeaways
- Shared Collection +
tenantIdindex prefix = Maximum scalability. - Separate DBs = Maximum isolation compliance.
Knowledge Check
In a shared collection multi-tenant architecture, where should the tenantId field reside in compound indexes?