Scrivio Interview library
ScenarioAdvanced6 min read · updated 12 November 2026

One tenant's bulk import in a shared-table SaaS database saturates I/O and slows every other tenant. How do you handle this now and design against it long term?

Right now we slow the import down: throttle or reschedule it, cap the connections it can use, and stop runaway queries with statement timeouts or by cancelling them. For the long term we put heavy operations behind per-tenant quotas and a job queue with concurrency limits, measure resource use per tenant so we see noisy neighbours early, and offer large tenants dedicated capacity, a separate database or a cell, behind a routing layer that makes moving them possible.

Why this question is asked

Shared infrastructure is what makes SaaS cheap, and the noisy neighbour is its price. When one customer's import saturates the disks, two thousand other customers see slow pages and blame us. Interviewers ask this scenario because a good answer has two halves: calm incident handling that restores service in minutes, and a design that stops the next import from doing the same. In this answer, we will handle the incident first, then add quotas and a queue, make per-tenant usage visible, and design dedicated capacity and cells for the long term.

1First, Restore Service for Everyone Else

During the incident, the goal is to protect the other tenants without destroying the importing tenant's work. We start by finding what the import is doing. pg_stat_activity shows each connection's query, state and start time, and if the import runs under its own role or application_name we can find its sessions at once.

SQL
SELECT pid, usename, application_name, state, now() - query_start AS running, left(query, 60)
FROM pg_stat_activity
WHERE application_name = 'bulk-import' ORDER BY running DESC;
-- 4211 | importer | bulk-import | active | 00:41:12 | INSERT INTO line_items SELECT ...
SELECT pg_cancel_backend(4211); -- cancels the current query, keeps the session
SELECT pg_terminate_backend(4211); -- ends the session if cancel is not enough
ALTER ROLE importer SET statement_timeout = '5min'; -- applies to new sessions of that role

Then we reduce its pressure. If the import runs in our job system, we pause it or reschedule it for a quiet hour. We cap the connection pool of the import worker to one or two connections, so it cannot run 32 heavy statements in parallel. And we make it work in smaller chunks: committing every 10,000 rows with a short pause between chunks spreads the I/O and lets autovacuum and replicas keep up. Each of these restores headroom without telling the customer their data is lost.

2Why One Import Hurts Everyone

A database instance has one pool of disk throughput, one buffer cache and one stream of WAL to replicas. A bulk import of 50 million rows writes data pages, index pages for every index on the table, and WAL for all of it. It also evicts other tenants' hot pages from the cache, so their queries that used to hit memory now read from disk. And replicas fall behind while replaying the flood of WAL, so read traffic sent to replicas sees stale data. Nothing in PostgreSQL gives each tenant a fair share of I/O, so fairness has to come from the application.

3Quotas and a Queue for Heavy Operations

The structural fix is to stop heavy operations from running directly in request threads. Imports, exports, bulk updates and large reports become jobs on a queue. Workers take jobs with a global concurrency limit, for example at most 4 bulk jobs per database at once, and a per-tenant limit, for example 1 at a time. Each tenant also gets quotas suited to its plan, such as rows imported per hour or API requests per minute, enforced with a rate limiter before work is accepted.

Java
@Component
class ImportWorker {
private final Semaphore perDatabase = new Semaphore(4); // global bulk limit
private final Map<UUID, Semaphore> perTenant = new ConcurrentHashMap<>();
void run(ImportJob job) throws InterruptedException {
Semaphore t = perTenant.computeIfAbsent(job.tenantId(), id -> new Semaphore(1));
if (!t.tryAcquire()) { requeueLater(job); return; } // tenant already importing
perDatabase.acquire();
try {
for (List<Row> chunk : job.chunks(10_000)) {
importChunk(chunk); // one transaction per chunk
Thread.sleep(200); // leave room for others
}
} finally { perDatabase.release(); t.release(); }
}
}
Step throughAccepting a bulk job
  1. Tenant submits import50 million rows
  2. Within the tenant's quota?
    YesAccept and queueNoReject with retry-after
  3. Tenant already running a bulk job?
    YesWait in queueNoContinue
  4. Database bulk slots free?
    YesStart, in chunksNoWait in queue
  5. Report progressrows done, estimated finish

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

In a real deployment with several worker instances, these limits live in the queue or in a shared store rather than in local semaphores, but the idea is the same. Notice also the chunking: the import still finishes, just at a rate the shared database can absorb.

4Seeing Noisy Neighbours Early

We cannot manage what we cannot attribute. pg_stat_statements groups statistics by normalised query and by database role, not by tenant, and comments added to SQL do not split its entries, because the query id ignores them. So we combine sources. Heavy workloads run under their own roles or application_name, which shows up in pg_stat_activity and in pg_stat_statements per role. The application emits metrics tagged with the tenant id: rows written, query time and job duration per tenant. A dashboard ranks tenants by their share of database time, and an alert fires when one tenant uses, say, more than 30 percent for 10 minutes. Then the next noisy neighbour is a page to the on-call engineer before it is a ticket from other customers.

5Dedicated Capacity and Cells for the Long Term

Some tenants will always be heavy, and quotas only go so far. For them we offer dedicated capacity: a separate database, often as a paid tier, so their imports cannot touch anyone else. This only works if moving a tenant is routine, which requires a tenant routing layer that maps each tenant to its database and a tested process to copy a tenant's data and switch the mapping.

A cell-based architecture takes the same idea further. A cell is a complete, independent copy of the stack, with its own application instances, database and queues, serving a fixed set of tenants. If one tenant overloads its cell, only the tenants in that cell are affected, perhaps 200 instead of 2,000. This limit on the damage is called the blast radius. A thin routing layer at the front sends each request to the tenant's cell.

Step throughCell-based tenancy
  1. Global routertenant -> cell lookup, nothing else
  2. Cell 1app, queue, database: about 200 small tenants
  3. Cell 2app, queue, database: about 200 small tenants
  4. Dedicated cellone large tenant, sized for its imports
  5. Control planecreates cells, moves tenants, rolls out changes cell by cell

5 layers, from the top down. Step through them one at a time.

6Follow-up questions

Should we just give the database more I/O?

Scaling up buys time and is sometimes the right short-term step, but it does not create fairness. The next, larger import will saturate the bigger instance too. Limits and isolation are what make the system predictable.

How do we tell the customer?

Honestly and with numbers: their import will complete, it now runs in chunks at a rate that protects shared performance, here is the expected finish time, and a dedicated tier is available if they need faster bulk loads regularly.

Does row-level security help with noisy neighbours?

No. RLS isolates data, not resources. Two tenants protected by RLS still share the same disks and cache, so resource isolation needs limits, queues or separate capacity.

7Conclusion

In this answer, we saw how to handle a tenant whose bulk import slows everyone: in the moment, throttle or reschedule it, cap its connections and use statement timeouts and query cancellation; for the long term, put heavy work behind per-tenant quotas and a queue with concurrency limits, attribute resource use to tenants through roles and tenant-tagged metrics, and offer dedicated databases or cells behind a tenant routing layer to limit the blast radius.

What to say in the interviewAbout 80 seconds aloud

Open with

First I would restore service for the other tenants. I would find the import's sessions in pg_stat_activity, ideally by its role or application_name, cancel any runaway statement with pg_cancel_backend, and set a statement_timeout on that role.

Then, if they want more

Then I would pause or reschedule the job, cap its connection pool to one or two connections, and make it commit in chunks of, say, ten thousand rows with short pauses, so it still finishes without saturating I/O. For the long term, heavy operations like imports and exports should not run directly from requests.

They go through a job queue with a global concurrency limit per database and a per-tenant limit, plus quotas by plan enforced with rate limiting.

I also want to see the next noisy neighbour early. pg_stat_statements groups by query and role, not tenant, so I would run bulk work under separate roles and emit application metrics tagged by tenant, with a dashboard and an alert when one tenant takes a large share of database time.

Finally, large tenants should be able to get dedicated capacity, a separate database or cell, and a tenant routing layer makes moving them a routine operation. A cell-based design limits the blast radius so one tenant can only affect its own cell.

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.
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 "One tenant's bulk import in a shared-table SaaS database saturates I/O and slows every...": https://scrivio-interview-library.netlify.app/databases/data-modeling/dae184e268/