Video in progress
In a service that serves many tenants in one JVM, one tenant with heavy requests fills the whole heap and harms the others. How do you protect against this?
There is no memory isolation between tenants inside a JVM, so protection is built at the application level: limits on request size and result size, per-tenant concurrency quotas, streaming, and, if needed, moving heavy tenants to a separate pool or deployment.
📖 Answer
The heap is a shared resource. If one tenant asks for a 500 MB export, its objects live in the same old gen as other tenants' requests, and the GC gives everyone the same pauses. If there is an OOM, the whole pod goes down, meaning all tenants.
The layers of protection:
-
Limits at the entrance. Request body size, page size (
limitwith a maximum value), forbidding requests without filters. -
Per-tenant concurrency. A semaphore or bulkhead for each tenant:
Semaphore permits = tenantPermits.computeIfAbsent(tenantId, id -> new Semaphore(4));
if (!permits.tryAcquire(200, TimeUnit.MILLISECONDS)) {
throw new TooManyRequestsException(tenantId);
}
try { return handle(request); } finally { permits.release(); }
A separate, smaller limit for heavy operations (export, reports).
-
Using memory incrementally. Return the result by streaming rather than collecting it all (a DB cursor,
StreamingResponseBody). Then the request size has almost no effect on the heap. -
Separating heavy work. Move exports and reports to a separate worker deployment through a queue. Even if an OOM happens there, the main API stays alive.
-
A separate "cell" for big tenants. If a few tenants generate half the total load, give them a separate deployment, the strongest isolation.
On the monitoring side: a tenant label on metrics (with controlled cardinality). It is hard to link a JFR allocation profile to the tenant context, so I write the result size and duration in the request log, which is a quick way to find the culprit.
Sign up to mark studied questions and save your progress.