Menu

1 / 2

Your .NET parallel loop can become serial in a one-CPU container

#stackoverflow#records#service#calls#record
Reading 0:00
15s threshold

In my lab, changing a container's CPU limit from two CPUs to one almost doubled the time needed to process 200 records: 42.1 seconds became 83.6 seconds. These are medians from five runs of each configuration. The two-CPU version processed two records at once; the one-CPU version processed one. Both wrote all 200 records.

I think this kind of slowdown is easy to miss: the batch still finishes and every record gets written. Without a baseline for how long it should take, the extra waiting can look like ordinary service latency.

Each record needed three enrichment calls followed by a write to a system of record. I used this familiar pattern:

await Parallel.ForEachAsync(records, cancellationToken, async (record, ct) => { await ProcessAsync(record, ct); });

Parallel.ForEachAsync without ParallelOptions allows at most Environment.ProcessorCount concurrent operations. Passing new ParallelOptions { CancellationToken = ct } leaves MaxDegreeOfParallelism at its initial value, -1 .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.