Quick revision
The short answer
We use two isolation models: a pooled model, one shared schema with a tenant_id column, for the 2,000 small customers, and a dedicated schema or database for each of the 20 enterprise customers. Every request is routed by the tenant id from the token, through a tenant directory and an AbstractRoutingDataSource or Hibernate multi-tenancy, and noisy neighbours are contained with per-tenant rate limits and quotas. Caches and logs are tenant-aware, migrations run per schema or database with tooling that tracks versions, and the platform can move a tenant between tiers.
What to remember
In this answer, we used a pooled model for small tenants and dedicated schemas or databases for enterprise tenants, routed each request through the tenant directory and an AbstractRoutingDataSource, contained noisy neighbours with rate limits, quotas, fair queues and bulkheads, made cache keys and logs tenant-aware, ran migrations per target with Flyway and expand and contract changes, and built tooling to move a tenant between tiers.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We use two isolation models: a pooled model, one shared schema with a tenant_id column, for the 2,000 small customers, and a dedicated schema or database for each of the 20 enterprise customers. Every request is routed by the tenant id from the token, through a tenant directory and an AbstractRoutingDataSource or Hibernate multi-tenancy, and noisy neighbours are contained with per-tenant rate limits and quotas. Caches and logs are tenant-aware, migrations run per schema or database with tooling that tracks versions, and the platform can move a tenant between tiers.
Why this question is asked
B2B SaaS products almost always end up with two very different kinds of customers. Thousands of small ones want a low price and do not care where their data lives. A few large ones bring a large share of revenue and ask for stronger isolation, their own database, their own encryption keys, and protection from other customers' load. Building two products would be expensive; forcing everyone into one model loses either the small or the large customers. Interviewers ask this to see whether we can support both with one codebase. In this answer, we will choose the isolation models, route requests, limit noisy neighbours, make caching and logging tenant-aware, run migrations across many databases, and move tenants between tiers.
1Two Kinds of Customers, Two Isolation Models
The common names for the options are pool, bridge and silo. In the pool model, all tenants share tables and are separated by a tenant_id column. It is the cheapest per tenant and the easiest to operate, but isolation relies on the application. In the bridge model, each tenant gets its own schema in a shared database. In the silo model, each tenant gets its own database, sometimes its own cluster.
For our mix, the pool model fits the 2,000 small customers: one database, one schema, cheap and simple. The 20 enterprise customers get a dedicated database each, or a dedicated schema if their contract allows it, which gives them real data isolation, separate backups and restore, their own encryption keys, and protection from other tenants' heavy queries.
| Model | Who uses it | Isolation | Cost per tenant |
|---|---|---|---|
| Pool: shared schema, tenant_id | 2,000 small tenants | application-enforced | lowest |
| Bridge: schema per tenant | enterprise, moderate needs | schema boundaries | medium |
| Silo: database per tenant | enterprise, strict needs | separate database | highest |
2Routing Each Request to the Right Data
Each request carries a token with the tenant id. A tenant directory, a small table cached in each instance, maps every tenant to its tier and its data location: tenant 4411 is pooled, tenant 12 is in database ent-12. The request filter puts the tenant id and its data key into the tenant context.
Spring's AbstractRoutingDataSource then picks the right connection pool for each transaction. It holds a map of target data sources and asks us for a lookup key whenever a connection is needed.
7 messages in order. Play them, or step through them one at a time.
For pooled tenants the tenant_id filter from Hibernate's @TenantId still applies inside the shared database. An alternative for schema-per-tenant is Hibernate's own multi-tenancy with a MultiTenantConnectionProvider that switches the schema per tenant. Either way, the code that handles orders is the same for all tenants; only the connection differs. With 20 dedicated databases, we keep each enterprise pool small, for example 10 connections, and create pools lazily for rarely active tenants.
3Limiting Noisy Neighbours
A noisy neighbour is a tenant whose load hurts others: a nightly export of a million rows, an integration that calls the API in a tight loop. In the pool, they share everything, so we need limits. Per-tenant rate limits at the gateway cap request rates by plan. Per-tenant quotas cap expensive resources such as storage, export size and concurrent background jobs. Background work goes through queues with fair scheduling across tenants, so one tenant's 50,000 jobs do not delay everyone else's ten.
Enterprise tenants are already protected at the database level, but they share the application tier, so we also use bulkheads: separate thread pools or even separate deployments for the heaviest workloads, so their load cannot exhaust the threads that serve small tenants, and vice versa.
4Tenant-Aware Caching and Logging
Caches are a classic source of cross-tenant leaks: a cache key of product:42 returns the same entry to every tenant, even though product 42 belongs to only one. Every cache key therefore starts with the tenant, such as t:4411:product:42, which is enforced by a shared key builder rather than left to each developer.
Logs and traces carry the tenant id too. Putting it in the logging MDC, the mapped diagnostic context that adds fields to every log line, lets support filter one tenant's activity during an incident. Metrics are more careful: tagging every metric with 2,000 tenant ids explodes the number of time series, so we tag by tier and add per-tenant tags only for the enterprise tenants.
5Running Migrations Across Many Schemas and Databases
With one shared schema plus 20 dedicated databases, a schema change is 21 migrations. We use Flyway or Liquibase, which record applied versions in a history table inside each schema, and a small migration runner that iterates over the tenant directory and applies pending migrations to each target, reporting status per tenant.
Changes follow the expand and contract pattern: first add new columns or tables in a backward-compatible way, release code that works with both, and only later remove the old structure. That lets migrations run in waves, pool first or a pilot enterprise tenant first, and tolerate a target that is one version behind for a while.
6Moving a Tenant Between Tiers
Small customers grow, and sometimes a pooled tenant signs an enterprise contract. Tiers are only useful if we can move tenants between them without a long outage. The tooling copies the tenant's rows from the pool into a new dedicated database, keeps them in sync with change data capture while the tenant keeps working, briefly pauses writes for that tenant, switches the directory entry, verifies counts, and later deletes the old rows.
- Create dedicated databasesame schema version as the pool
- Copy rows where tenant_id = 4411bulk copy, table by table
- Sync changes with CDCuntil lag is near zero
- Pause writes for tenant 4411seconds to a minute
- Switch directory entrykey ent-4411, caches refreshed
- Counts match?YesResume writes, delete old rows laterNoSwitch back and investigate
6 steps in order. Play them, or step through them one at a time.
7Follow-up questions
Why not give every tenant its own schema?
With 2,000 tenants, schema-per-tenant means 2,000 sets of tables, 2,000 migrations per release and large catalogues in the database. It is workable but expensive for small customers who do not need it. The pool for small tenants keeps operations simple.
How do you keep the enterprise connection pools from exhausting the database server?
Each enterprise database has its own small pool, and if several dedicated databases share a server, the total of their pools is sized to that server. A connection proxy such as PgBouncer can also multiplex many application connections onto fewer database connections.
How does a request find its tenant before the directory is loaded?
The directory is cached in each instance at start-up and refreshed on change events; an unknown tenant id is looked up once and cached, and an id that does not exist is rejected.
8Conclusion
In this answer, we used a pooled model for small tenants and dedicated schemas or databases for enterprise tenants, routed each request through the tenant directory and an AbstractRoutingDataSource, contained noisy neighbours with rate limits, quotas, fair queues and bulkheads, made cache keys and logs tenant-aware, ran migrations per target with Flyway and expand and contract changes, and built tooling to move a tenant between tiers.
Open with
I would run two isolation models in one codebase. The 2,000 small customers share one schema with a tenant_id column, filtered automatically with Hibernate's @TenantId, which is cheapest and simplest.
Then, if they want more
The 20 enterprise customers each get a dedicated database or schema for real isolation, separate backups and their own keys. A tenant directory maps each tenant to its tier and data location, cached in every instance.
The tenant id comes from the token, and an AbstractRoutingDataSource picks the connection pool for each transaction based on that, so the business code is identical for every tenant. For noisy neighbours I would use per-tenant rate limits at the gateway, quotas on storage and exports, fair queues for background jobs and bulkheads for the heaviest workloads.
Cache keys always start with the tenant id, and the tenant goes into the logging MDC, while metrics are tagged by tier, plus tenant for enterprise only, to avoid cardinality problems. Migrations run with Flyway per target, using expand and contract so targets can be a version apart.
Finally I would build tooling to move a tenant from the pool to a dedicated database: copy, sync with CDC, brief write pause, switch the directory, verify.
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 official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
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 the backend for a B2B SaaS product built with Spring Boot that serves 2,000 small...": https://scrivio-interview-library.netlify.app/architecture/backend-system-design/96a0d6b980/