DEV Community

Cover image for Bounded concurrent I/O without overload
Khashayar Nourian
Khashayar Nourian

Posted on Originally published at knourian.ir on

Bounded concurrent I/O without overload

Sending independent HTTP requests one at a time wastes the waiting time between them. Starting all of them at once removes that wait but can overload the service they call. Bounded concurrency keeps several requests in flight while capping how many are active together.

A runnable .NET console project requests the same eight items three ways—sequential, unbounded, and bounded—and prints the peak number of active requests for each. The bounded version wraps the fetch in a SemaphoreSlim, acquires a slot before the HTTP call, and releases it in finally so a failed request doesn't keep its slot.

The limit covers one batch, not the service: two concurrent batches can each stay under it and still send more requests together than the dependency can handle. It also doesn't cap requests per second, and every waiting item still has a task in memory.

Read the full article on my blog: Bounded concurrent I/O without overload.

Top comments (0)