CPU-heavy work in WebFlux
CPU-heavy work needs processor capacity even when it runs inside a reactive pipeline. Keep long computations off shared request event loops, bound concurrent tasks and their waiting queue, and choose between a dedicated executor and durable workers according to latency and recovery needs.
Inside this article 5 sections +
A reactive endpoint can remain free of blocking I/O and still monopolize its execution thread. Compressing a large report, calculating a complex score, or transforming a substantial document consumes CPU while it runs. A blocking-call detector may remain quiet because the code is computing rather than waiting.
Moving that work to another executor can protect shared request processing, but it does not create additional processor capacity. The design needs two controls: where the computation executes and how much computational work the service admits. Without the second, a supposedly isolated executor becomes another growing queue.
Verify that CPU is the scarce resource
Use a representative input and profile the expensive operation. Distinguish actual computation from database waits, locks, native calls, and allocations that trigger collection work. A method called renderReport may spend most of its time fetching fonts or waiting for a remote template. Its name does not establish the execution policy it needs.
Record input size and shape alongside elapsed time and CPU evidence. A small report and a pathological report can differ by far more than their byte count suggests. Nested data, image dimensions, or algorithmic complexity may create a long tail. Include the largest supported input and explicit rejection behavior for unsupported complexity.
Check the container's processor allocation rather than using the host's advertised core count as an unquestioned capacity budget. Several services may share a host, and CPU limits can throttle a process whose worker count looked sensible on a developer laptop. Interpret thread activity together with actual process CPU and throttling evidence.
Choose an execution boundary for the product promise
| Placement | Suitable condition | Required control |
|---|---|---|
| Request path | Small, bounded transformation with measured cost | Input limits and mixed-route latency checks |
| Dedicated bounded executor | Interactive computation that fits the request deadline | Finite workers, finite waiting capacity, explicit rejection |
| Separate process or worker service | Heavy native work or stronger resource isolation | CPU/memory allocation and operation-level cancellation |
| Durable background job | Work can outlive the HTTP request | Persistent identity, status, retries, and duplicate-safe output |
A dedicated executor can make ownership visible and prevent long computations from running on shared event loops. Its queue must be bounded, and rejection must become a defined application outcome. Silently falling back to executing the task on the submitting event-loop thread can defeat the isolation precisely during overload.
Do not select a scheduler merely because it accepts blocking tasks. CPU work should have concurrency related to the processor budget and the rest of the application's needs. Very large worker counts can increase contention and retained inputs without improving useful completion. Shared schedulers also create interference with other code using the same pool.
Schedule the work before it executes
The computation must remain deferred until the chosen execution boundary runs it. Calling an expensive method and then wrapping its result in a publisher does not move the work. Keep the source lazy and test the thread or executor that actually performs the computation.
Illustrative execution contract, not a complete Reactor API example:
when subscribed:
validate input bounds and remaining deadline
acquire one compute admission permit, or reject
submit deferred computation to the dedicated executor
on success, error, cancellation, or rejection:
release the permit exactly once
publish a result associated with the original operation
Permit lifetime matters. Releasing admission when the HTTP client disconnects is unsafe if the computation continues consuming CPU in the background. Track actual task completion separately from subscriber interest. If cancellation cannot stop the task, keep its capacity accounted for until it ends, and avoid starting replacement work that exceeds the intended budget.
Make cancellation and deadlines concrete
CPU loops may need cooperative interruption or cancellation checks at safe intervals. A timeout on the reactive response does not automatically stop an arbitrary library call. Inspect the library's behavior, and use process isolation when a non-cooperative computation must be terminated reliably without destabilizing the request process.
Set a deadline before admitting the task and include queue wait in that deadline. A report that has already spent its entire budget waiting should not begin a large computation that no customer is still waiting to receive. For work with continuing business value, convert the request into a durable operation with a status resource rather than leaving it as abandoned in-memory work.
Bound retained input and output sizes. A queue of compressed source documents may look small until each running task expands images and constructs a large object graph. Estimate per-task peak memory from representative observations, then test concurrent execution. Processor and memory budgets can require different concurrency limits; the tighter one should govern admission.
Give a custom executor an application lifecycle owner. Create it once for the intended scope, expose its queue and active-task metrics, and shut it down deliberately during deployment. Creating a new executor for each request hides the aggregate thread budget and makes cancellation and graceful shutdown much harder to verify.
Test collateral latency and recovery
Create two routes in a controlled environment: a lightweight account lookup and a report computation using a fixed input fixture. Drive them together under a declared arrival pattern. Compare the original path, the bounded executor design, and any separate-worker design without changing the input or total resource allocation unnoticed.
Measure lightweight-route latency, report completion, rejections, queue age, CPU, memory, and retained work after cancellations. A successful isolation change protects the lightweight route within its objective while keeping report work bounded. Faster report throughput alone does not demonstrate that other users remain unaffected.
Increase offered report load above capacity briefly, then reduce it. Verify that rejected work is explicit, accepted work reaches a known result, and the service returns to its baseline without restarting. The blocking-call guide covers synchronous waits, while benchmark methodology keeps comparisons fair. Performance architecture must account for active computation as carefully as waiting.