Performance and scale ·Build
One customer slows everyone down: shared database, or one per customer?
An accounting firm's back office on synthetic data
A few large customers, tenants in a SaaS product, generate most of the load, and one customer's heavy processing at filing time makes everything slow. Meanwhile a prospect's security review asks how their data is kept apart from everyone else's, and wants a test as the answer, not a promise. I built an accounting firm's back office on synthetic data to answer both. Every tenant starts in one shared database, where the database itself enforces the boundary between them; a tenant whose ledger lines measurably slow the others moves those lines to a database of their own, and can move back. During one tenant's heavy job, the small tenants' p95 was 39.6 ms in the shared database and 4.5 ms once that tenant's ledger lines had their own.
- Problem
- a few large customers generate most of the load, one heavy deadline run slows everyone, and a prospect asks how their data is kept apart from others'.
- Deciding constraint
- a prospect's security review has to be answered with a test, not a promise, and an accountant works for many clients at once.
- Choice
- every tenant starts in one shared database under row-level security; a tenant whose ledger measurably slows the others moves its lines to a database of its own, and can move back.
- Result
- quiet tenants' p95 went from 39.6 ms during a heavy tenant's job back to 4.5 ms (5.1 ms at baseline) once its lines had their own database. Every query runs as two tenants and returns no foreign row; with the policy off, the same test finds 100.
The lab is public, with its tests and the command behind each of its figures: hybrid-tenant-isolation.
If this is your situation
If one customer's load affects the others, or a prospect's security review asks how tenants are separated, start by finding which tenants and which tables carry the load, and run one query as two tenants to see what comes back.
Performance and scalability → discuss tenancy
For your technical team How it was diagnosed, the options rejected and why, what went wrong, and the decision record.
Context and scope
A SaaS product serves customers of every size: a few large ones generate most of the load, many small ones generate little load. One large customer's filing-deadline run slows everyone down. A sales prospect asks how their data is kept apart from other customers'. And an accountant who serves many clients expects one list of deadlines across all of them.
The build is the back office of an accounting firm, on invented companies with valid-checksum tax numbers: clients, an accountant who serves many of them, deadlines, and imported ledger lines. One client is far larger than the rest. SAF-T files are imported, and a client can leave.
In scope: how tenant data is stored, how isolation is enforced, and how a tenant's heavy data is moved or removed. Out of scope: authentication and billing. Assumption: tenants differ a lot in size, and most of the load sits in one large table.
Drivers
Constraints
- A prospect's security review will ask where the tenant boundary is enforced.
- An accountant serving many clients needs one list of deadlines across them.
- The team can run a small, fixed number of databases, not one more per customer.
Scenarios
- When one tenant runs a heavy job, the other tenants' response times do not change. Measured by their p95 during that job, three repetitions.
- When a query is written without a tenant filter, no row of another tenant is returned. Measured by a test that runs every registered query as two tenants, with the policy switched off as its negative control.
- When a pooled connection is reused, the next request does not run as the previous tenant. Measured by a test on one connection, with a session-level setting as its negative control.
- When an accountant works for several clients, one query returns their work across all of them, and only theirs. Measured by the reviewed exception list.
- When a tenant's heavy data must be moved or removed, it is done for that tenant alone. Measured by a rehearsed move and an offboarding run.
Options against those drivers
| Model | Isolation | Cross-tenant queries | Noisy neighbours | Operations |
|---|---|---|---|---|
| Shared database | By the application or the database | Easy: one query | One heavy tenant affects all | One database |
| Schema per tenant | Stronger, by schema | Harder: across schemas | Shared resources still | Migrations run per schema |
| Database per tenant | Strongest | Hard: across databases | Contained | Many databases to run |
| Hybrid | By the database, in both places | Easy where it matters | Contained where the load is | Two placements to run |
The decision and how it works
Placement is decided per tenant, by measurement. Every tenant starts in the shared database. Clients, deadlines and the accountant's work have small rows and many relations, and stay there. Imported ledger lines are large, written in bulk and rarely read across clients; when one tenant's lines measurably slow the others, as in the benchmark below, that tenant's lines move to a database of their own. A routing entry says where each tenant's lines are stored, so moving a tenant, or moving it back, is a verified copy and one routing change, with no change to the code.
The database enforces the boundary. Row-level security is forced on every tenant table, and the application connects as a role that owns nothing and cannot bypass it. The tenant is resolved in the database from the caller and set for one transaction, never for the session, so a pooled connection handed to the next request carries no tenant at all. A query written without any tenant filter still returns only that tenant's rows.
The cross-tenant user is an exception kept like code. The one cross-tenant read, an accountant's work across their clients, is a reviewed statement with its hash on a list. The code refuses it unless it is listed and unchanged since review; the database checks its own copy of the list; the scope lasts one transaction; and every use is recorded.
How the move is made, and how it is undone
A tenant's ledger lines move one tenant at a time: copy, verify with a hash, switch reads and writes, wait a full cycle, then delete the old rows. Until the delete, rolling back is one routing change; after it, going back is a copy in the other direction. The rehearsal moved 3,000,000 lines in 93.9 s (105.5 s with the deletion), then read, rolled back, moved again, deleted and moved back, and every read equalled the one before. It pauses the tenant during the copy; on a live system, add a second pass for lines written while the copy runs.
Leaving works the same way: a verified export first, personal data erased, accounting records kept, and a purge refused until a retention date is set and has passed. The retention period itself is a legal figure to set with an accountant.
How the decision is kept true
- The two-tenant test runs every registered query as two tenants and fails on any foreign row: eight queries, none returned. With the policy switched off, the same walk finds 100, so a green run means something.
- The pooled-connection test runs two requests on one connection; the negative control shows that a session-level setting would hand the next request the previous tenant's rows.
- The exception list is reviewed like code, and the database records each use.
- The noisy-neighbour benchmark, three repetitions, the same hardware limits each time, 3,000,000 ledger lines for the heavy tenant and eight quiet clients:
| Quiet tenants | p95 | Heavy job, median |
|---|---|---|
| Baseline, no heavy job | 5.1 ms | — |
| Heavy job on the shared database | 39.6 ms | 5.2 s |
| Heavy job on its own database | 4.5 ms | 3.8 s |
The median did not change; the degradation was in the tail. Once the lines had their own database the quiet tenants were back to the baseline: 4.5 ms is within its spread, not faster than it.
Where it went wrong, and the risks that remain
Bulk loading was refused. PostgreSQL refuses COPY FROM on a table with row-level security, so
imports and moves use batched inserts, which are slower. Reading with COPY TO is allowed. Plan the
load path before choosing the isolation mechanism, not after.
A heavy tenant made a query fail, not merely slow down. The first full benchmark failed with "No space
left on device": parallel query workers allocate shared memory in /dev/shm, and a container receives 64 MB by default. Both databases now run with 1 GB. A noisy neighbour can make other work fail outright.
A sequence does not travel with its rows. After a move, the new database's id sequence has to continue after the copied ids, and the application role is not allowed to set it. The move does it as an operator step, which a migration plan written only for rows would miss.
A test that cannot fail proves nothing. The first two-tenant test passed for tenants with no imported data, so it proved nothing; it now fails when a query returns no rows for either tenant. Each isolation test also has a negative control that switches the protection off and must then fail.
| Risk | Likelihood | Impact | Mitigation | Owner |
|---|---|---|---|---|
| A new table or report path is added without the policy | Medium | One tenant sees another's data | Force the policy on every tenant table; the two-tenant test over every registered query, with its negative control | Tech lead |
| The tenant setting leaks across pooled connections | Medium | The next request runs as the previous tenant | Set the tenant per transaction, never per session; the pooled-connection test | Developer |
| Cross-tenant users added as an afterthought | Medium | The first accountant with ten clients breaks the model | Design the exception first; review each statement and record each use | Architect |
| Someone runs arbitrary SQL as the application role | Low | The cross-tenant guard is bypassed | The guard protects against application mistakes, not this: keep the role's credentials out of reach | Operations |
| Per-tenant databases without automation | High | Migrations, backups and monitoring multiply with every customer | Automate provisioning and migrations before the second tenant database | Operations |
Operations, cost and scale boundary
- To run: migrations across the shared and dedicated databases, and backups of both.
- To learn: the isolation mechanism, and where each tenant's data is stored.
- Cost: a database per heavy tenant is a running cost that grows with customers, so it is a decision for the heaviest few, not for all.
- Tested range: one laptop, 3,000,000 ledger lines for one heavy tenant, eight quiet clients.
Where the choice flips: with few tenants of similar size, shared only is simpler and enough. With a contractual need for physical separation, a database per tenant for everything is the answer, accepting the harder cross-tenant queries.
On a different stack
Placement per tenant by measurement, the two-tenant test with its negative control and the rule that the filter is applied below the query carry over. The mechanism differs by database and framework. In PostgreSQL, row-level security applies the filter inside the database, with the tenant set per transaction, so even a raw query cannot cross it. Other databases and frameworks have their own equivalents; check that yours applies to every query, not only to those written through one layer.
Performance and scalability → discuss tenancy