Scrivio Interview library
DesignAdvanced6 min read · updated 12 November 2026

Design a multi-tenant search service for 5,000 customers, where 20 large tenants hold 70 percent of the data. Would you use an index per tenant, a shared index with routing, or a hybrid?

With 5,000 tenants, an index per tenant gives isolation and easy deletion but creates thousands of tiny shards, while one shared index with a tenant_id filter and custom routing keeps the shard count low and sends each tenant's queries to one shard. Because 20 tenants hold 70 percent of the data, I would choose a hybrid: dedicated indices for the largest tenants and a few shared, routed indices for the long tail. Tenant filters are enforced in a query layer or with filtered aliases, and tenants move between tiers by reindexing and switching aliases.

Why this question is asked

Multi-tenant search is a classic design question because the obvious choices both fail at scale in opposite ways. One index per customer is simple and safe until the cluster drowns in shards; one big index is efficient until a giant tenant overwhelms its shard or a missing filter leaks one customer's data to another. Interviewers want to see us reason about the data distribution and pick deliberately. In this answer, we will cost out both pure options, design the hybrid, make tenant isolation impossible to forget, and plan how tenants move between tiers.

1Option 1: An Index per Tenant

Giving each tenant its own index is attractive. Isolation is natural, since a query against tenant-123 cannot see another tenant's documents. Each tenant can have its own mapping and analyzers. Deleting a tenant who leaves is one instant index deletion, which also helps with data protection obligations. Noisy tenants can be moved to other nodes.

The problem is the count. 5,000 indices with 1 primary and 1 replica each is 10,000 shards, and the long tail of tenants, perhaps 4,000 of them, holds only a few megabytes each. As we know from oversharding, each shard costs heap and cluster state regardless of its size, so most of the cluster's resources would go to overhead. Mapping updates across 5,000 indices are slow, and the count grows with every new customer.

2Option 2: One Shared Index With Routing

The opposite is one shared index, or a few, holding every tenant's documents with a tenant_id keyword field. Every query adds a filter on tenant_id. To avoid every query hitting every shard, we use the tenant id as the custom routing value when indexing and searching, so all of a tenant's documents land on one shard and its queries touch only that shard.

Java
PUT shared-1/_doc/9001?routing=t-123
{ "tenant_id": "t-123", "title": "Invoice template" }
GET shared-1/_search?routing=t-123
{ "query": { "bool": {
"filter": [ { "term": { "tenant_id": "t-123" } } ], // still required: other tenants share this shard
"must": [ { "match": { "title": "invoice" } } ] } } }

Notice that routing is not a security boundary. Other tenants hash to the same shard, so the tenant filter is still mandatory. And there is a catch with our data: the 20 large tenants hold 70 percent of the data. With routing, each big tenant's documents pile onto one shard, which can become far larger than the others. The setting index.routing_partition_size spreads a routing value over several shards to soften this, but it does not change the fact that big and small tenants have very different needs. Shared indices also mix term statistics across tenants, which can skew relevance scoring, and all tenants must agree on one mapping.

3The Hybrid Design

The data distribution points to the answer. The 20 large tenants each get a dedicated index, sized for their data with enough primaries to keep shards in the tens of gigabytes. They get isolation, their own mappings and tuning, their own lifecycle, and they cannot hurt the long tail. That is 20 indices, perhaps 100 shards with replicas, which is cheap.

The other 4,980 tenants share a small number of indices, for example 4 shared indices of 6 primaries each, with routing by tenant id. They hold 30 percent of the data, so the shards stay at healthy sizes. The total shard count stays in the low hundreds instead of 10,000, and onboarding a new small tenant costs nothing at all.

Tiers in the hybrid design
TierTenantsLayoutWhy
Dedicated20 largest (70% of data)one index each, sized primariesisolation, own mapping, no hot shard
Shared4,980 others (30% of data)few indices, routing by tenant_idlow shard count, free onboarding
Lookupalltenant -> index, routing, aliasthe query layer resolves where to go
Every request goes through the lookup, so applications never choose an index themselves.

4Making the Tenant Filter Impossible to Forget

The biggest risk in shared indices is a data leak: one query without the tenant filter shows one customer another's data. We remove the possibility of forgetting it. The first approach is a query layer, a small service or library through which every search passes. It takes the authenticated tenant from the request, looks up the tenant's index and routing, and always wraps the caller's query in a bool filter on tenant_id. Application code never builds raw requests to the cluster.

The second approach is filtered aliases. For each tenant, we create an alias that carries both the filter and the routing, so the application searches the alias as if it were the tenant's own index. Large tenants' aliases simply point at their dedicated index without a filter. Recent versions also offer document-level security in the security features of Elasticsearch and the OpenSearch security plugin, depending on licence and configuration, which can enforce the filter per user role.

Java
POST _aliases
{ "actions": [ { "add": {
"index": "shared-2", "alias": "tenant-t-123",
"filter": { "term": { "tenant_id": "t-123" } },
"routing": "t-123" } } ] }
GET tenant-t-123/_search { "query": { "match": { "title": "invoice" } } } // filter and routing applied

Five thousand aliases also live in the cluster state, so at very large scale the query layer is often lighter. Either way, tests should check that a search under one tenant never returns another tenant's document.

5Moving Tenants Between Tiers

Tenants grow. When a shared-tier tenant crosses a threshold, for example 20 GB, we promote it. We create its dedicated index, reindex its documents from the shared index with a query on its tenant_id, keep applying new changes to both locations for the duration (or replay them from the change stream afterwards), compare document counts, then switch the tenant's alias or lookup entry to the new index. Finally we delete its documents from the shared index with a delete by query. Demotion of a shrinking tenant is the same in reverse.

Because every request resolves the tenant through the lookup or alias, the switch is one atomic change and the application never notices. Planning this path from the start is what makes the hybrid safe to run for years.

Step throughPromoting a growing tenant
  1. Tenant passes 20 GB in the shared tier
  2. Create its dedicated index
  3. Reindex its documents by tenant_id
  4. Catch up changes made meanwhiledual write or replay
  5. Counts match?
    YesSwitch alias or lookup entryNoFind and fix the gap
  6. Delete its documents from the shared index

6 steps in order. Play them, or step through them one at a time.

6Follow-up questions

How do we delete a small tenant from a shared index?

With a delete by query on its tenant_id, using its routing value so only its shard is touched. The documents are marked deleted immediately and physically removed when segments merge. If a regulation requires proof of removal from disk, a dedicated index, which can simply be deleted, is easier, which is another reason sensitive tenants may get their own tier.

What about tenants who need custom fields?

In shared indices, custom fields are a common cause of mapping explosion, because every tenant's field names end up in one mapping. We can give custom fields a generic shape, such as a flattened field or key and value pairs, or move tenants with heavy customisation to dedicated indices.

7Conclusion

In this answer, we compared an index per tenant, which isolates well but oversharding makes impossible at 5,000 tenants, with a shared index using a tenant filter and custom routing, which keeps shards few but struggles with very large tenants. Given that 20 tenants hold 70 percent of the data, the hybrid gives them dedicated indices and puts the long tail in shared routed indices. A query layer or filtered aliases make the tenant filter mandatory, and reindexing plus alias switches move tenants between tiers.

What to say in the interviewAbout 90 seconds aloud

Open with

I would start from the data distribution. An index per tenant gives the best isolation, custom mappings and instant deletion, but 5,000 indices with replicas is 10,000 shards, most of them a few megabytes, so the cluster would spend its heap and master time on overhead.

Then, if they want more

A shared index with a tenant_id field, a mandatory filter and custom routing by tenant keeps the shard count low and sends each tenant's queries to one shard, but the 20 large tenants would make their shards huge, and they would share mappings and relevance statistics with everyone.

So I would build a hybrid: each of the 20 large tenants, who hold 70 percent of the data, gets a dedicated index sized for it, and the other 4,980 share a few indices with routing by tenant id. That keeps the total in the low hundreds of shards.

To prevent leaks, no application talks to the cluster directly; a query layer looks up the tenant's index and routing and always adds the tenant filter, or we use filtered aliases that carry the filter and routing per tenant, and we test that cross-tenant results are impossible.

Finally I would plan promotion from the start: when a tenant passes a threshold, create its index, reindex its documents by tenant id, catch up recent changes, check counts, switch the alias, and delete its documents from the shared index.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
Elasticsearch documentation
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Design a multi-tenant search service for 5,000 customers, where 20 large tenants hold 70...": https://scrivio-interview-library.netlify.app/databases/search-engines/c555a0b246/