Background services are useful in ASP.NET Core applications for tasks that should continue outside the normal HTTP request lifecycle. Queue processing, scheduled jobs, notification delivery, cleanup tasks, report generation, and message consumption are common examples.
The problem starts when a background service does more work than it should, runs synchronously, creates unlimited concurrency, or consumes shared resources needed by HTTP requests.
An application can then appear healthy from the outside while its background workers quietly consume database connections, CPU, memory, thread-pool capacity, or external API limits.
The result is often slow API responses, request timeouts, failed health checks, and unpredictable behavior under load.
The goal is not to avoid background services. The goal is to make their workload controlled and isolated enough that background processing cannot take down the request-processing side of the application.
How Background Services Run in ASP.NET Core
ASP.NET Core provides BackgroundService as a convenient base class for long-running background work.
A simple worker might look like this:
public class OrderWorker : BackgroundService
{
private readonly ILogger<OrderWorker> _logger;
public OrderWorker(ILogger<OrderWorker> logger)
{
_logger = logger;
}
protected override async Task ExecuteAsync(
CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
await ProcessOrdersAsync(stoppingToken);
await Task.Delay(
TimeSpan.FromSeconds(10),
stoppingToken);
}
}
private async Task ProcessOrdersAsync(
CancellationToken cancellationToken)
{
// Process pending orders
}
}The worker runs inside the same ASP.NET Core process as the web application.
That detail matters.
If the worker consumes too many resources, it competes with HTTP requests for the same application resources.
ASP.NET Core Process
|
+-- HTTP Requests
|
+-- Background Services
|
+-- Database Connections
|
+-- Thread Pool
|
+-- Memory
|
+-- CPUThere is no automatic wall between these workloads.
The Most Common Problem With Background Services
A common mistake is to write a background worker as if it were an independent application.
For example:
protected override async Task ExecuteAsync(
CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
var jobs = await LoadJobsAsync(stoppingToken);
foreach (var job in jobs)
{
await ProcessJobAsync(job, stoppingToken);
}
}
}This has several problems.
There may be no delay between iterations, the query might return a large number of records, and processing may continue indefinitely.
If LoadJobsAsync() returns 10,000 jobs, the worker may spend a long time processing them while continuing to consume application resources.
A better design limits the amount of work processed in each iteration.
var jobs = await LoadJobsAsync(
batchSize: 50,
cancellationToken);
foreach (var job in jobs)
{
await ProcessJobAsync(job, cancellationToken);
}The worker should have a defined workload boundary.
Avoid Blocking Calls Inside Background Services
The word background does not mean that synchronous code becomes harmless.
This is still blocking:
var result = SomeDatabaseCallAsync().Result;And this is also blocking:
SomeDatabaseCallAsync().GetAwaiter().GetResult();These calls can tie up thread-pool threads and create unnecessary contention.
Use asynchronous APIs throughout the worker:
var result = await SomeDatabaseCallAsync(
stoppingToken);For I/O-bound work, asynchronous execution allows the thread to return to the pool while the database, HTTP service, or message broker is doing its work.
Be Careful With CPU-Heavy Work
Async code does not automatically make CPU-intensive work cheap.
Consider:
protected override async Task ExecuteAsync(
CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
GenerateLargeReport();
await Task.Delay(
TimeSpan.FromMinutes(1),
stoppingToken);
}
}If GenerateLargeReport() consumes a CPU core for several seconds, HTTP requests in the same process may experience increased latency.
For CPU-heavy workloads, consider moving the processing to a separate worker process or job service.
The important distinction is:
I/O-bound work
|
v
Async BackgroundService can work well
CPU-heavy work
|
v
May need separate compute capacityDo Not Start Unlimited Tasks
One of the easiest ways to overload an ASP.NET Core application is to create a task for every available item.
For example:
var tasks = jobs.Select(
job => ProcessJobAsync(job, cancellationToken));
await Task.WhenAll(tasks);If jobs contains 20,000 records, this can create a huge amount of concurrent work.
The database, external APIs, connection pool, memory, and CPU can all become bottlenecks.
A bounded concurrency approach is safer.
For example:
using var semaphore = new SemaphoreSlim(10);
var tasks = jobs.Select(async job =>
{
await semaphore.WaitAsync(cancellationToken);
try
{
await ProcessJobAsync(
job,
cancellationToken);
}
finally
{
semaphore.Release();
}
});
await Task.WhenAll(tasks);Only a limited number of jobs are processed concurrently.
The correct limit depends on the workload.
Ten database operations may be reasonable in one system and excessive in another.
Use Queues for Controlled Background Processing
For many applications, a queue is a better architecture than continuously scanning a database.
ASP.NET Core provides Channel<T>, which can be used to build an in-process work queue.
For example:
public interface IBackgroundTaskQueue
{
ValueTask QueueAsync(
Func<CancellationToken, ValueTask> workItem);
ValueTask<Func<CancellationToken, ValueTask>>
DequeueAsync(CancellationToken cancellationToken);
}A bounded channel can prevent producers from adding unlimited work:
private readonly Channel<Func<CancellationToken, ValueTask>> _queue =
Channel.CreateBounded<Func<CancellationToken, ValueTask>>(
new BoundedChannelOptions(100)
{
FullMode = BoundedChannelFullMode.Wait
});The important part is that the queue is bounded.
Without a limit, an application can accept work faster than it can process it.
Eventually memory becomes the bottleneck.
Separate Producers From Consumers
A useful architecture looks like this:
HTTP Request
|
v
Queue
|
v
Background Worker
|
v
Database / External ServiceThe HTTP request does not need to perform the entire operation.
For example:
await queue.QueueAsync(
async cancellationToken =>
{
await GenerateReportAsync(
reportId,
cancellationToken);
});The worker later processes the job.
This can significantly reduce request latency, but it also changes the business semantics.
The HTTP request may now return before the operation is complete.
That means the application should provide a way to track job status when the operation is important.
Do Not Use BackgroundService as a Hidden Job Scheduler
A worker containing this code:
while (!stoppingToken.IsCancellationRequested)
{
await RunTaskAsync();
await Task.Delay(
TimeSpan.FromMinutes(5),
stoppingToken);
}is effectively acting as a scheduler.
That can be acceptable for simple tasks, but it creates operational problems when the application runs multiple instances.
Suppose the API has five replicas.
Each replica starts the same worker:
Instance 1 -> Runs cleanup
Instance 2 -> Runs cleanup
Instance 3 -> Runs cleanup
Instance 4 -> Runs cleanup
Instance 5 -> Runs cleanupThe task may now execute five times.
This is a common problem in containerized or horizontally scaled applications.
For work that must execute exactly once across all application instances, use a distributed scheduling or job-processing design, or implement distributed coordination deliberately.
Understand What Happens During Scaling
Background services scale with the application process.
If your ASP.NET Core application has:
3 application instancesyou usually have:
3 copies of the BackgroundServiceIf each worker consumes ten database connections, the application may consume up to thirty connections.
Scaling the API therefore also scales the background workload.
This is one reason high-volume background processing is often better separated from the web application.
Web Application
|
+-- Instance 1
+-- Instance 2
+-- Instance 3
Worker Application
|
+-- Worker 1
+-- Worker 2Now web traffic and background processing can scale independently.
Protect the Database From Background Workers
Background processing frequently interacts with the same database as HTTP requests.
Suppose the API normally uses 70 database connections under load, while the worker starts another 30 concurrent queries.
A connection pool configured for 100 connections is now close to its limit.
If traffic increases slightly, HTTP requests may start waiting for connections.
Join the conversation! Your thoughts help the community grow.