Quick revision
The short answer
A shared schema with a tenant_id column is the cheapest and simplest to operate, but isolation depends on every query filtering by tenant, which Hibernate 6 can enforce with a @TenantId column. Schema per tenant gives stronger isolation and per-tenant restore through Hibernate's schema-based multi-tenancy with a MultiTenantConnectionProvider, and database per tenant gives the strongest isolation and per-tenant tuning at the cost of many databases to migrate, monitor and pay for. In both Hibernate approaches a CurrentTenantIdentifierResolver supplies the tenant per session, and we choose by isolation and compliance needs, tenant count and size spread, often with a hybrid.
What to remember
In this answer, we compared three models. A shared schema is cheapest and simplest, with Hibernate 6 enforcing tenant filtering through @TenantId. Schema per tenant isolates tables and allows per-tenant restore through a MultiTenantConnectionProvider that switches schemas. Database per tenant isolates fully but multiplies operations and cost. A CurrentTenantIdentifierResolver supplies the tenant in all of them, and we choose by isolation and compliance needs, tenant count and size spread, often ending with a hybrid behind a tenant directory.
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.
A shared schema with a tenant_id column is the cheapest and simplest to operate, but isolation depends on every query filtering by tenant, which Hibernate 6 can enforce with a @TenantId column. Schema per tenant gives stronger isolation and per-tenant restore through Hibernate's schema-based multi-tenancy with a MultiTenantConnectionProvider, and database per tenant gives the strongest isolation and per-tenant tuning at the cost of many databases to migrate, monitor and pay for. In both Hibernate approaches a CurrentTenantIdentifierResolver supplies the tenant per session, and we choose by isolation and compliance needs, tenant count and size spread, often with a hybrid.
Why this question is asked
The multi-tenancy model is one of the first architecture decisions in a SaaS product and one of the hardest to change later, because it shapes the schema, the migrations, operations and even the sales contracts. Interviewers ask this to see whether we know how each model works in practice with Hibernate and can weigh it against the business, not just the code. In this answer, we will walk through each of the three models and how Hibernate supports it, compare them side by side, look at how a CurrentTenantIdentifierResolver fits in, and then see how to choose, including hybrids.
1Shared Schema With a tenant_id Column
In the shared model, all tenants' rows live in the same tables, and each row carries a tenant_id. There is one database, one schema and one migration per release, so it is the cheapest to run and the simplest to operate, and it handles 50,000 small tenants as easily as 50.
Its weakness is that isolation exists only in our queries. Every query must filter by tenant, and one that does not leaks data across customers. Hibernate 6 helps by making this automatic: a field annotated with @TenantId is treated as a discriminator, Hibernate adds the tenant condition to the queries it generates and sets the value on insert. Native queries still need care, and row-level security in PostgreSQL can add a database-side guarantee. Noisy neighbours are the other weakness: one big tenant's load affects everyone.
2Schema Per Tenant
Here every tenant has its own schema, a namespace inside the same database, with identical tables. A query for tenant acme runs against acme.invoice. Isolation is stronger: a missing WHERE clause cannot reach another tenant, because their tables are elsewhere, and we can back up, restore or delete one tenant's schema on its own.
In Hibernate, we implement a MultiTenantConnectionProvider, which hands out a connection for a given tenant id. For schema-based multi-tenancy, it takes a connection from the shared pool and switches it to the tenant's schema, for example with connection.setSchema(tenant) or SET search_path in PostgreSQL, and resets it on release so the pooled connection does not keep the tenant's schema.
The cost shows up at scale. Each schema has its own tables and indexes, so 2,000 tenants means 2,000 copies of every table, a large system catalog, and a migration that must run 2,000 times.
3Database Per Tenant
The strongest model gives every tenant its own database, sometimes its own server. Isolation is physical: one tenant's load, failure or data cannot touch another's, and each database can be sized, tuned, placed in a region and encrypted with its own keys. Compliance teams and large enterprise customers often ask for this.
In Hibernate it uses the same MultiTenantConnectionProvider interface, but getConnection picks a different DataSource per tenant from a map. The costs are operational: hundreds or thousands of databases to provision, migrate, back up, monitor and pay for, with a separate connection pool per tenant in each service instance, which quickly multiplies connections.
4Comparing the Models Side by Side
Each model moves the balance between cost and isolation. The resolver part is the same in the Hibernate approaches: a CurrentTenantIdentifierResolver returns the current tenant id for each session, usually from a request-scoped context filled from the user's token, and Hibernate passes it either to the @TenantId filter or to the connection provider.
| Shared schema | Schema per tenant | Database per tenant | |
|---|---|---|---|
| Hibernate mechanism | @TenantId discriminator | MultiTenantConnectionProvider, setSchema | MultiTenantConnectionProvider, DataSource map |
| Isolation | query filtering only | separate tables | separate databases |
| Per-tenant restore | hard | easy | easy |
| Cost per tenant | lowest | medium | highest |
| Migrations | one per release | one per schema | one per database |
| Noisy neighbours | affect everyone | share one database | isolated |
| Comfortable tenant count | very large | hundreds to low thousands | tens to hundreds |
5Choosing a Model
We choose from the business, not from taste. The first question is isolation and compliance: do contracts or regulations require physical separation, per-tenant encryption keys or data residency? If yes for some customers, those customers need their own databases. The second is the number of tenants and how their sizes are spread. Thousands of small tenants favour the shared schema; a few dozen large ones can afford separate databases.
Hybrids are common and often best. Most tenants share a schema, and the largest or most regulated tenants get their own database, with a routing layer that maps each tenant to its location. The design that makes this possible is a tenant directory from the start, so moving a tenant from the shared database to a dedicated one is an operation, not a rewrite.
- Contracts require physical isolation?YesDatabase per tenant for those tenantsNoConsider sharing
- Thousands of small tenants?YesShared schema with @TenantIdNoSchema or database per tenant
- A few tenants much larger?YesHybrid: move them to dedicated databasesNoKeep one model
- Add a tenant directoryso tenants can move later
4 steps in order. Play them, or step through them one at a time.
6Follow-up questions
How do schema migrations work with 2,000 schemas?
A migration tool such as Flyway or Liquibase runs once per schema, each schema keeping its own history table, driven by a job that loops over the tenant directory in parallel batches. Migrations must be backward compatible, using expand and contract steps, because for a while some schemas are on the new version and others on the old. The job records which schemas succeeded so a failure in schema 1,217 can be retried without re-running the rest, and new tenants are created at the latest version.
Can the shared schema model ever give per-tenant restore?
Only indirectly: we restore a full backup to a separate database and copy that tenant's rows back, which is slow and error-prone. If per-tenant restore is a contractual promise, the shared model makes it expensive.
Do the models affect connection pools?
Yes. Shared and schema per tenant can use one pool per service instance. Database per tenant usually needs a pool per tenant database, so connections multiply with tenants and instances, and a pooler such as PgBouncer or lazy, small per-tenant pools become necessary.
7Conclusion
In this answer, we compared three models. A shared schema is cheapest and simplest, with Hibernate 6 enforcing tenant filtering through @TenantId. Schema per tenant isolates tables and allows per-tenant restore through a MultiTenantConnectionProvider that switches schemas. Database per tenant isolates fully but multiplies operations and cost. A CurrentTenantIdentifierResolver supplies the tenant in all of them, and we choose by isolation and compliance needs, tenant count and size spread, often ending with a hybrid behind a tenant directory.
Open with
I see the three models as a trade between cost and isolation. A shared schema with a tenant_id column is the cheapest and simplest: one schema, one migration, and it scales to tens of thousands of small tenants.
Then, if they want more
Isolation depends on every query filtering by tenant, and with Hibernate 6 I would use a @TenantId discriminator column so Hibernate adds the filter and fills the column automatically, plus care with native queries and maybe row-level security.
Schema per tenant gives every tenant its own tables, so a missing filter cannot cross tenants and I can restore one tenant alone; in Hibernate I implement a MultiTenantConnectionProvider that switches the connection's schema for the tenant and resets it on release.
Database per tenant gives the strongest isolation and lets me tune, place and encrypt each tenant separately, using the same provider interface with a map of data sources, but it means many databases to migrate, monitor and pay for. In both Hibernate provider approaches, a CurrentTenantIdentifierResolver supplies the tenant id per session from the request context.
I would choose from isolation and compliance requirements, number of tenants and how uneven they are, and in practice I often end up with a hybrid: most tenants shared, the biggest or most regulated ones on dedicated databases, with a tenant directory so tenants can move.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Hibernate 6
- 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
- Spring Data JPA reference, Hibernate ORM documentation, Spring Framework reference, Transaction management
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 "Compare shared schema with a tenant_id column, schema per tenant, and database per...": https://scrivio-interview-library.netlify.app/databases/database-scaling/a8df720bb5/