Heavy work inside an HTTP request

1 min read

A CPU fan-out (see fan-out and fan-inFan-out and fan-in with Parallel.ForRender independent items on all cores, keep the results in order, then merge them one at a time.Opens in a new tab) is fine inside a request for modest reports at low or moderate traffic.

When it stops being fine

For large reports or many concurrent users, each request takes up to Environment.ProcessorCount cores, so simultaneous requests oversubscribe the machine. Parallel.For also holds the request thread until it finishes, and a thread poolThe thread poolA set of reusable worker threads that take jobs from a queue.Opens in a new tab short of free threads grows slowly.

Fix

Queue the job. Enqueue it on a background worker, such as a hosted service with a channel or a job queue like Hangfire, and return a job ID. The client polls, or is notified, when the result is ready.

Where it matters

Cores are shared by all concurrent requests. async/await frees threads during I/O waits, but CPU-bound work still needs cores (see how many threads handle one requestHow many threads handle one request?One at a time, but not always the same one.Opens in a new tab).