Learn the "why" behind slow queries and how to fix them in our 2-Part Webinar.
Register now >
Docs Menu
Docs Home
/ /

Build a Multi-Tenant Architecture for MongoDB Search

You can implement multi-tenancy with MongoDB Search so that a single instance of an application serves multiple tenants. This page describes design recommendations that apply specifically to MongoDB Search for structuring data and MongoDB Search indexes to scale safely while maintaining tenant isolation.

Important

This guidance assumes that you can colocate tenants within a single VPC. If strict network isolation is required, you must maintain separate projects for each tenant.

Select a strategy based on your isolation needs, scaling goals, and write-load requirements. The most common strategies are:

  • A single collection for all tenants - This strategy is recommended for most workloads.

  • One Database per tenant - This strategy offers high isolation but increases index count.

  • One Collection per tenant - This strategy is not recommended for MongoDB Search workloads.

We recommend applying the one collection for all tenants strategy. In this architecture, you store all tenant data in a single collection within a single database. You can distinguish between tenants by including a tenant_id field within each document. This field can be any unique identifier for the tenant, such as a UUID or a tenant name.

This centralized approach offers the following benefits:

  • Reduces the number of indexes

    Since all tenant data is stored in one collection, you need only a single index that serves documents for all tenants. You avoid hitting the cluster resource limits. For example, you can include the tenant_id and other shared or tenant-specific fields in a single index.

  • Simplifies maintenance operations

    With only a single collection storing all data, you don't have to maintain multiple collections or scale resources across multiple databases. You also simplify backup, sharding, and replication operations as you need to target only a single collection in a single database.

  • Processes query efficiently

    You provide the tenant_id field in your queries as a filter using the equals operator within the $search.compound.filter clause.

    Note

    Your query results will only include documents from the matching tenant, preventing data from leaking across tenants.

If the tenants you colocate share similar access patterns and resource sizes, this strategy also keeps the Lucene index field count low.

Important

When every tenant has a high write load, colocating all tenants into a single collection might create write contention. In that scenario, consider splitting tenants with the most writes into separate collections so that you can distribute the write load across more underlying resources.

If you have large tenants with unique resourcing or indexing requirements, use a View:

  1. Isolate a tenant. Create a View using a $match with a $expr operation to filter only the tenant's documents.

  2. Index separately. Create a MongoDB Search index on the View.

    This allows you to customize the index definition for the large tenant without impacting the shared index used by others. All other tenants can use a single index.

    We don't recommend exceeding 2500 indexes per cluster because this will generate a high load on the base cluster and risks disrupting your database workload.

If you currently apply the one collection per tenant strategy, we recommend scaling up the base tier to prevent increased replication lagor OOM errors due to resource contention from ahigh number of indexes. We also recommend migrating to the one collection for all tenants strategy.

If specific tenants have massive write loads, they might cause contention in a shared collection. We recommend that you move only the highest-volume tenants to separate collections to distribute the I/O load.

If a tenant requires unique indexing or is significantly larger than others, use a MongoDB View:

  • Create a View using

  • $match to filter only that tenant's documents.

  • Create a MongoDB Search index on that View. This allows customization for outlier tenants without disrupting the shared index used by the majority.

In a database-per-tenant setup, each tenant contains its own database. This setup isolates tenant data, but it also multiplies the number of indexes in the cluster. When each tenant has many indexed fields, the cluster might reach the 2500-index ceiling.

Warning

A high index count generates significant load on the base cluster and might disrupt your workload. To keep the cluster stable, carefully monitor the number of indexes and avoid exceeding 2500 indexes in total.

The collection-per-tenant strategy maps each tenant to its own collection within a shared database. The overhead of maintaining an index per collection might cause:

  • Increased replication lag

  • OOM errors due to resource contention

  • Instability in the base tier

We do not recommend this strategy for MongoDB Search unless tenants have very predictable, lightweight workloads. If you currently use this strategy, migrate toward the one collection for all tenants strategy.

Consider the following recommendations to manage indexed fields:

Back

Review Deployment Options

On this page