C# Interview Question #142

What is the difference between I/O-bound and CPU-bound work?

Advanced Async, Parallelism & Threading Senior Advanced

Quick Interview Answer

I/O-bound work spends most of its time waiting for external resources such as databases, files, HTTP services, or network operations. async/await is normally the appropriate approach because the thread does not need to remain blocked while waiting.

Detailed Explanation

I/O-bound work spends most of its time waiting for external resources such as databases, files, HTTP services, or network operations. async/await is normally the appropriate approach because the thread does not need to remain blocked while waiting.

CPU-bound work spends most of its time performing calculations. Parallel execution or Task.Run may be useful in some application types when work can be safely distributed across CPU cores.

In ASP.NET Core, wrapping ordinary server-side CPU work in Task.Run usually does not improve scalability because it still consumes a thread-pool thread.

Code Example

// I/O-bound
var products = await _context.Products.ToListAsync();

// CPU-bound example
long result = CalculateLargeDataset(data);