Scrivio Interview library
DesignAdvanced7 min read · updated 12 November 2026

Your platform runs both latency-sensitive APIs and heavy CPU analytics on the same Kubernetes nodes and JVMs. Teams use parallel streams, ForkJoinPool and large thread pools freely. How would you organise concurrency and parallelism so the workloads do not hurt each other?

I would separate the workloads first at the infrastructure level: latency-sensitive APIs and CPU-heavy analytics run in different deployments, ideally on different node pools, each with its own CPU requests and limits. Inside a service JVM, heavy work never runs on the common ForkJoinPool; it gets a dedicated, bounded pool so request threads are not starved. Pools are sized from availableProcessors(), which follows the container's CPU limit, by documented rules, and we watch CPU throttling and run-queue metrics because too many runnable threads under a CPU limit cause latency spikes. Guidelines, examples and review of heavy parallel code paths keep teams on the rules.

Why this question is asked

When APIs and analytics share nodes and JVMs, the symptoms are confusing: p99 latency jumps every afternoon, CPU graphs look fine on average, and nobody's code changed. The cause is usually that one workload takes the CPU the other needs at the moment it needs it, and in Kubernetes the CPU limit makes this worse by throttling the whole container. Interviewers ask this at senior levels to see whether we can isolate workloads at several layers and give teams rules they can follow. In this answer, we will see how the workloads hurt each other, separate them in Kubernetes, isolate them inside the JVM, size pools correctly for containers, and decide what to monitor and how to guide teams.

1How the Workloads Hurt Each Other

A latency-sensitive API needs a little CPU, but immediately: a request that waits 50 ms for a core has already broken a 100 ms target. Analytics needs a lot of CPU, but can wait. When both run on the same cores, analytics keeps every core busy and API threads wait in the operating system's run queue.

Inside one JVM the damage is more direct. A parallel stream in an analytics endpoint uses ForkJoinPool.commonPool(), which has about availableProcessors() - 1 threads and is shared by every parallel stream and by CompletableFuture stages that do not name an executor. One heavy report can occupy the whole common pool for seconds, and the API code that used the same pool for small async steps simply stops.

Kubernetes CPU limits add a third effect. A limit of 2 CPUs is enforced by the kernel's CFS quota: the container may use 200 ms of CPU time every 100 ms period. If 30 runnable threads burn that in the first 20 ms of the period, the whole container, including API threads, is paused for the remaining 80 ms. That is throttling, and it shows as latency spikes even when average CPU use looks low.

2Layer 1: Separate Deployments and Node Pools

The strongest isolation is not to share. APIs and analytics become separate deployments, so they are separate JVMs with separate heaps, pools and garbage collection, and each gets CPU requests and limits that match its pattern. Analytics can go to a dedicated node pool, using node selectors or taints and tolerations, so its CPU bursts never land on the nodes that serve customer traffic.

For the APIs, CPU requests should reflect real need so the scheduler places them well, and many teams set the CPU limit generously or not at all for latency-critical services to avoid throttling, while keeping memory limits strict. For analytics, a firm limit is fine, because throttling a batch job only makes it slower. Which policy fits depends on the cluster's rules, so we agree it with the platform team.

Step throughIsolation from the cluster down to the code
  1. Node poolsAPI nodes and analytics nodes; taints keep them apart
  2. Deploymentsseparate JVMs with their own CPU requests and limits
  3. JVM poolsrequest threads, I/O executor, dedicated CPU pool
  4. Code rulesno heavy work on commonPool; sizes from availableProcessors

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

Each layer limits how far one workload can reach into another.

3Layer 2: Dedicated Pools Inside the JVM

Sometimes the same service must do both, for example an API that occasionally builds a heavy export. Then heavy work must never run on the common pool or on request threads. We give it a dedicated, bounded pool with a queue and a rejection policy, so the worst a burst of exports can do is queue up exports, not stall requests.

Java
@Bean(destroyMethod = "close")
ExecutorService analyticsPool() {
int cpus = Runtime.getRuntime().availableProcessors(); // from the container limit
int threads = Math.max(1, cpus / 2); // leave half the CPU for requests
return new ThreadPoolExecutor(threads, threads, 0, TimeUnit.SECONDS,
new ArrayBlockingQueue<>(100),
Thread.ofPlatform().name("analytics-", 0).factory(),
new ThreadPoolExecutor.AbortPolicy()); // reject instead of piling up
}
// instead of reportRows.parallelStream()... in a request handler:
Future<Report> report = analyticsPool.submit(() -> buildReport(reportRows));

Notice the sizing choice: half of the available processors, documented as a rule rather than a guess. The exact fraction is a policy for the service owner, but the point is that the heavy pool can never take every core. CompletableFuture calls in service code should also always pass an executor, so they never fall back to the common pool.

4Layer 3: Sizing From the Container, Not the Host

Runtime.availableProcessors() in a container returns a number derived from the container's CPU limit, not the host's core count, and the common pool and GC thread counts follow it. On a 64-core node with a 2-CPU limit it returns 2. Older JDKs before container support returned 64 and sized everything for the host, which was a famous source of throttling. In current JDKs the CPU request (cpu shares) is no longer used for this value, only the limit, so a pod with no CPU limit sees the whole node; check your JDK version, and set -XX:ActiveProcessorCount explicitly when we want a fixed number.

So the sizing rules we document are: CPU-bound pools at most availableProcessors(), heavy pools a stated fraction of it, I/O work on virtual threads or pools limited by the downstream resource, and an explicit ActiveProcessorCount for pods without CPU limits.

5Watching Throttling and the Run Queue

Average CPU use hides this problem, so we monitor the signals that show it. In Kubernetes, the cAdvisor metrics container_cpu_cfs_throttled_periods_total and container_cpu_cfs_periods_total give the share of periods in which the container was throttled; anything above a few percent for an API deserves attention. On the node, the run queue length or load average compared with core count shows threads waiting for CPU. Inside the JVM, Micrometer's executor metrics show queue sizes and active threads per pool.

We correlate these with request latency. If p99 spikes line up with throttled periods or with the analytics pool being busy, we have found the coupling and can fix it at the right layer.

6Guiding the Teams

Teams use parallel streams freely because they are easy and usually harmless in small cases. So the guidance is short and concrete: parallel streams only for short CPU-bound work in batch code, never in request handlers; heavy computation goes to the service's named CPU pool; CompletableFuture always gets an explicit executor; pool sizes follow the documented rules. Each rule comes with a before and after example.

We also flag heavy parallel code paths for review, for example with a search in the build for parallelStream() and ForkJoinPool usage in service modules, so a reviewer who knows the sizing rules looks at them before they reach production.

7Follow-up questions

Should latency-critical services have a CPU limit at all?

It depends on the cluster policy. Without a limit, a service can burst into spare CPU and is never throttled, which helps latency, but a misbehaving pod can take more than its share of the node. Many teams keep accurate requests and either no limit or a generous one for APIs, and firm limits for batch work. Whatever the choice, it should be explicit and measured.

Why is the common ForkJoinPool a problem if it is sized to the cores?

Because it is shared. Its size is right for all the CPU work in the JVM together, not for one heavy task. When one report takes all its threads for seconds, every other user, including small async steps in request handling, waits behind it.

Would virtual threads solve this?

No. Virtual threads make waiting cheap, but CPU-bound analytics still needs real cores, and virtual threads run on a carrier pool sized to the processors. Isolation of CPU-heavy work is still a matter of separate pools, deployments and limits.

8Conclusion

In this answer, we saw how APIs and analytics hurt each other through shared cores, the shared common ForkJoinPool and CPU throttling under container limits. We isolated them in layers: separate deployments and node pools with their own requests and limits, dedicated bounded pools for heavy work inside a JVM, pool sizes derived from availableProcessors(), which follows the container limit, and monitoring of throttling and run queues alongside latency. Short, concrete guidelines and review of heavy parallel code keep teams within the rules.

What to say in the interviewAbout 80 seconds aloud

Open with

I would isolate the workloads in layers. First, at the cluster level, the APIs and the analytics jobs become separate deployments, and analytics goes to its own node pool with taints and tolerations, each with CPU requests and limits that fit its pattern, so a heavy report cannot take the cores that serve customers.

Then, if they want more

Second, inside a JVM that must do both, heavy work never runs on the common ForkJoinPool or request threads. The common pool is shared by all parallel streams and by CompletableFuture stages without an executor, so one report can stall everything.

I would give heavy work a dedicated bounded pool, say half of the available processors, with a rejection policy, and require an explicit executor for every CompletableFuture. Third, pools are sized from Runtime.availableProcessors, which in a container follows the CPU limit, with -XX:ActiveProcessorCount where there is no limit, and the rules are written down.

Then I would monitor what averages hide: CFS throttled periods per container, run-queue length on the nodes, and executor queue sizes, correlated with p99 latency, because too many runnable threads under a CPU limit cause throttling and spikes. Finally, short guidelines with examples, and review of any parallel streams or ForkJoinPool use in service code.

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 "Your platform runs both latency-sensitive APIs and heavy CPU analytics on the same...": https://scrivio-interview-library.netlify.app/java/threads/bab1d0eed6/