Why Does an AI Feature Slow Down When Several People Use It?
An AI feature may work comfortably during a demonstration with a small number of users. The situation changes when many requests arrive at the same time.
A burst of requests can start hundreds of asynchronous operations. Each operation may spend most of its time waiting for a remote AI service, but the application still needs to retain request state, manage responses, handle cancellations, and eventually return results to users.
Users may see a spinner without knowing whether their request has started, is waiting for capacity, or has already been canceled.
A practical way to control this workload is to use a bounded admission queue with a fixed number of workers.
The queue limits how much work can wait. The workers limit how many operations can execute at the same time.
You also need an explicit policy for what happens when the queue is full and when a caller cancels.
This approach controls local application concurrency. It does not automatically enforce an AI provider's request-per-minute, token, or billing limits.
The following console example makes these boundaries visible before connecting a real AI model.
Which Limit Solves Which Problem?
Different controls solve different problems.
Control | Question It Answers | What It Does Not Establish |
|---|---|---|
Queue capacity | How many accepted jobs may wait here? | How quickly the provider accepts requests |
Worker count | How many jobs may execute at once here? | A requests-per-minute limit |
Admission policy | What happens when no waiting slot is available? | Whether another application instance has capacity |
Rate limiter | How much traffic may start within a time window? | How much queued work the application can retain |
A semaphore can restrict active work while many callers continue waiting to acquire it. That may be acceptable when admission is bounded elsewhere.
However, if every incoming request creates another waiting task without a limit, a semaphore alone does not bound the backlog.
For this example, a full queue rejects a new job immediately. An application could instead allow callers to wait for capacity, but that waiting period should also have a deadline and an explicit limit.
What Do You Need to Run the Example?
Use the .NET 10 SDK and create a console project:
dotnet new console --framework net10.0 --name AiQueueDemo
cd AiQueueDemoReplace Program.cs with the following code.
The example uses a short, cancellable delay as a fake AI operation. This makes it possible to inspect the concurrency mechanism independently of an external AI provider.
The code is illustrative. The important properties are the queue capacity, worker limit, cancellation behavior, and admission behavior.
using System;
using System.Linq;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
const int capacity = 3;
const int workerCount = 2;
var queue = Channel.CreateBounded<AiJob>(
new BoundedChannelOptions(capacity)
{
FullMode = BoundedChannelFullMode.Wait,
SingleWriter = true,
SingleReader = false
});
using var shutdown = new CancellationTokenSource();
using var canceledCaller = new CancellationTokenSource();
canceledCaller.Cancel();
int active = 0;
int peak = 0;
int completed = 0;
int canceled = 0;
int rejected = 0;
// Prefill before starting workers so admission results are deterministic.
var jobs = new[]
{
new AiJob("q1", CancellationToken.None),
new AiJob("q2", CancellationToken.None),
new AiJob("q3", canceledCaller.Token),
new AiJob("q4", CancellationToken.None)
};
foreach (var job in jobs)
{
if (!queue.Writer.TryWrite(job))
{
rejected++;
}
}
queue.Writer.Complete();
await Task.WhenAll(
Enumerable.Range(0, workerCount)
.Select(_ => ConsumeAsync()));
Console.WriteLine(
$"completed={completed}; canceled={canceled}; rejected={rejected}");
Console.WriteLine(
$"peak active={peak}; limit respected={peak <= workerCount}");
async Task ConsumeAsync()
{
await foreach (var job in queue.Reader.ReadAllAsync(shutdown.Token))
{
if (job.Caller.IsCancellationRequested)
{
Interlocked.Increment(ref canceled);
continue;
}
using var linked = CancellationTokenSource.CreateLinkedTokenSource(
job.Caller,
shutdown.Token);
int now = Interlocked.Increment(ref active);
UpdatePeak(now);
try
{
await Task.Delay(100, linked.Token);
linked.Token.ThrowIfCancellationRequested();
Interlocked.Increment(ref completed);
}
catch (OperationCanceledException)
when (linked.IsCancellationRequested)
{
Interlocked.Increment(ref canceled);
}
finally
{
Interlocked.Decrement(ref active);
}
}
}
void UpdatePeak(int candidate)
{
int observed;
do
{
observed = Volatile.Read(ref peak);
if (candidate <= observed)
{
return;
}
}
while (Interlocked.CompareExchange(
ref peak,
candidate,
observed) != observed);
}
record AiJob(string Id, CancellationToken Caller);The example uses .NET's System.Threading.Channels API to create a bounded producer-consumer queue.
The channel stores waiting jobs, while two consumers perform the work.
Why Use TryWrite with Wait Mode?
The channel's full mode and the writer method serve different purposes.
BoundedChannelFullMode.Wait defines the behavior of the bounded channel when it reaches capacity.
TryWrite attempts to add an item immediately and returns whether the operation succeeded. It does not wait for capacity to become available.
This example deliberately fills the queue before starting the workers.
With a capacity of three:
Job 1 → Accepted
Job 2 → Accepted
Job 3 → Accepted
Job 4 → RejectedThe fourth job cannot enter because all three waiting slots are occupied.
Calling:
queue.Writer.Complete();announces that no additional jobs will be written. It does not discard jobs that were already accepted.
Two consumers then read the queue.
Each consumer waits for its current operation to finish before reading another job. Therefore, the design allows at most two active operations at a time.
This relationship is important.
Starting an unawaited operation inside the worker loop could allow the worker to continue reading additional jobs and would break the intended concurrency limit.
How Does the Concurrency Limit Work?
The important relationship is:
Bounded Queue
Capacity = 3
|
↓
┌─────────────────────┐
│ Waiting │
│ q1 q2 q3 │
└─────────────────────┘
↓ ↓
Worker Worker
1 2
↓ ↓
AI Call AI CallThe queue controls waiting work.
The workers control active work.
With:
const int capacity = 3;
const int workerCount = 2;the application can have up to three accepted jobs waiting in the queue and two jobs executing concurrently.
These are separate limits.
A larger queue does not increase execution capacity.
More workers increase execution concurrency, but they may also increase pressure on the downstream AI service.
Join the conversation! Your thoughts help the community grow.