Many background queue implementations still rely on BlockingCollection<T>. While simple, it relies on synchronous OS monitor primitives that block worker threads when empty or full.
System.Threading.Channels provides an asynchronous, lock-free alternative designed for high-throughput producer/consumer pipelines.
Before: BlockingCollection
var queue = new BlockingCollection<WorkItem>(boundedCapacity: 1000);
// Blocks thread pool thread if full
queue.Add(item);
// Synchronously blocks thread if empty
var work = queue.Take();
After: Bounded Async Channel
var options = new BoundedChannelOptions(1000)
{
FullMode = BoundedChannelFullMode.Wait,
SingleReader = true,
SingleWriter = false
};
var channel = Channel.CreateBounded<WorkItem>(options);
// Asynchronously yields without blocking thread pool
await channel.Writer.WriteAsync(item, ct);
await foreach (var work in channel.Reader.ReadAllAsync(ct))
{
await ProcessAsync(work, ct);
}
Why It Matters
-
No Thread Starvation: Awaiting
ReadAsyncorReadAllAsyncsuspends the state machine instead of holding an active thread pool thread captive. -
Optimized Concurrency: Setting
SingleReader = trueorSingleWriter = trueenables lock-free single-producer/single-consumer ring buffer paths. -
Zero-Allocation Reads: Returns
ValueTask<T>, eliminating heap allocations when items are immediately ready.
Watch out: You must explicitly invoke channel.Writer.Complete() or channel.Writer.TryComplete(ex) on shutdown; otherwise ReadAllAsync() never terminates.
Have you transitioned your BackgroundService queues to Channels yet?

Top comments (1)
I checked ChannelOptions and Channel.CreateUnbounded in dotnet/runtime. SingleReader means you promise there is at most one read at a time, and SingleWriter is the same promise for writes. The remarks say the channel may optimize if that promise is true. They are not a switch that turns on a lock free ring buffer, and the sample sets SingleWriter to false, so it is not the single writer case the SPSC sentence describes. CreateUnbounded only takes the single consumer channel when SingleReader is true. It does not also require SingleWriter.
FullMode.Wait does not drop items. WriteAsync waits until a slot is free, so a slow reader still stacks latency on the writer. It just does not block a thread. Use DropOldest or DropWrite when stale work is allowed to disappear. Wait is the right mode when every item has to be processed.