Architecture

Background Jobs in .NET: Stop Doing Slow Work Inside the Request

Yasir Rehman
Yasir Rehman
·November 14, 2026·5 min read

Your "Create order" endpoint takes eight seconds. Users click twice, get duplicate orders, or give up. You check the database and it's fast. The problem is everything else the request does before it replies: sending invoice emails, building a PDF, calling a third-party API.

This is one of the most common performance problems I find in .NET applications. The fix is simple in principle: do only the essential work inside the request, and move the slow work to the background. This guide shows how, and how to choose the right tool.

The problem: slow work inside the request

Here's a pattern I see often:

[HttpPost("orders")]
public async Task<IActionResult> Create(Order o)
{
    await _db.SaveAsync(o);
    await _email.SendInvoicesAsync(o);   // slow: SMTP or email API
    await _pdf.BuildReportAsync(o);      // slow: PDF generation
    return Ok();
}

The user waits for every line. If the email service is slow or down, the whole request is slow or fails, even though the order was saved. Under load, these long requests also hold resources and make everything else slower.

The fix: save, queue, reply

[HttpPost("orders")]
public async Task<IActionResult> Create(Order o)
{
    await _db.SaveAsync(o);
    await _queue.EnqueueAsync(new InvoiceJob(o.Id));
    return Accepted();   // reply in milliseconds
}

The request saves what must be saved, puts a small job on a queue, and replies. A background worker picks up the job and sends the emails and builds the PDF. If the email service fails, the worker retries later. The user never waits for it.

Three ways to run background work in .NET

1. A hosted service with an in-memory queue

.NET has built-in support for long-running background services. You register a BackgroundService, give it a queue (for example a Channel<T>), and it processes jobs as they arrive. Microsoft's guide to background tasks with hosted services shows the pattern.

Good for: simple jobs, one server, nothing to install.
Watch out: jobs in memory are lost if the app restarts. Store anything important in the database first.

2. Hangfire

Hangfire stores jobs in your database, retries failures automatically and gives you a dashboard to see what ran, what failed and why. It also handles scheduled and recurring jobs ("send the daily report at 7 AM").

Good for: most business apps that need reliable jobs and visibility.
Watch out: it adds tables and load to your database; keep job payloads small.

3. A cloud queue

Azure Queue Storage, Azure Service Bus or AWS SQS hold messages outside your app. Workers can run in the same app, a separate service or serverless functions, and scale independently.

Good for: high volume, multiple services, or when the worker should scale separately.
Watch out: more moving parts and costs to manage.

Rules that keep background jobs reliable

→ Pass IDs, not objects. Queue InvoiceJob(orderId) and load fresh data in the worker. Stale copies cause subtle bugs.

→ Make jobs idempotent. A retried job must not send two emails. Record what was done and check before doing it again.

→ Retry with limits. Retry a few times with increasing delays, then move the job to a failed list and alert someone.

→ Log and monitor. You need to know when jobs fail, not hear it from customers.

→ Pass a CancellationToken so shutdowns stop work cleanly.

Quick wins in the request itself

Background jobs fix the slow work, but check the request too. Avoid blocking on async code with .Result or .Wait(), return only the data the client needs, and make sure your database queries are indexed and paged. My write-up on SQL Server performance fixes from client audits covers the database side, and .NET vs Node.js for enterprise APIs explains why .NET is a strong choice for this kind of work.

A worked example: order confirmation

An e-commerce API confirms orders by saving the order, emailing the customer, emailing the warehouse, generating a PDF invoice and updating an external accounting system, all inside one request. On a busy day, checkout slows down and sometimes times out, even though the order was saved.

After the change, the endpoint saves the order and enqueues a single OrderConfirmedJob(orderId). The worker sends both emails, builds the PDF and syncs accounting, each step recorded so a retry doesn't repeat what already succeeded. Checkout now replies in milliseconds, and a failure in the accounting sync no longer affects customers. It's retried and alerted on instead.

Frequently asked questions

When should work stay inside the request?

When the user needs the result to continue, like validating input, saving the record they just created, or returning data they asked for. Anything the user doesn't need to see immediately is a candidate for the background.

How do users know the background work finished?

Show a status ("Invoice is being prepared"), send a notification when it's done, or let the client poll a status endpoint. For most emails and PDFs, users don't need to wait at all.

Is Hangfire better than a hosted service?

For simple, non-critical jobs on one server, a hosted service is enough. When jobs must survive restarts, be retried and be visible in a dashboard, Hangfire or a cloud queue is the safer choice.

Does this help with scaling?

Yes. Requests stay short, so the web servers handle more users, and workers can scale separately when the job queue grows.

Slow API in production?

I've spent 5+ years building and fixing .NET applications in production, and moving slow work out of requests is one of the first things I look for. If your API is slow or timing out, send me a message with what it does and where it hurts. I'll tell you where I'd look first. See my services.

Found this useful? Share it with your network.

Yasir Rehman

Yasir Rehman

Full-Stack .NET & React Developer

Building production software with .NET, React, and SQL Server. Writing about architecture, integrations, and the lessons learned shipping real systems.

Comments

Leave a comment

Chat on WhatsApp