---
title: Background Processing Patterns
description: Reliable patterns for idempotency, retries, and dependency boundaries.
order: 62
available_in: [dotnet, nodejs]
---

# Background Processing Patterns

Use these patterns to keep background processing reliable as load and team size increase.

## Idempotent handlers

Design handlers so re-execution is safe.

Patterns:

- Upsert instead of blind insert.
- Check external side effect state before issuing duplicate calls.
- Use deterministic business keys for deduplication.

## Thin job, rich domain service

Keep job classes focused on orchestration and call domain services for business logic.

Benefits:

- easier testing
- clearer retry boundaries
- cleaner dependency graph

## Retry-aware external calls

When calling external systems:

- treat transient failures as expected
- use `RetryBehavior.Backoff` for dependency outages
- keep terminal errors explicit and observable

## Concurrency boundaries

For recurring jobs that mutate shared resources:

- set `AllowConcurrentRuns = false`
- keep execution windows shorter than schedule interval when possible

## Operational visibility pattern

Track by `RunId` and `JobName` in logs and traces so run-history and app diagnostics connect cleanly.

:::code-group
```csharp
_logger.LogInformation("Processing invoice batch. Job={JobName} RunId={RunId} Attempt={Attempt}",
    context.JobName,
    context.RunId,
    context.Attempt);
```

```typescript
logger.info("Processing invoice batch", {
  jobName: context.jobName,
  runId: context.runId,
  attempt: context.attempt
});
```
:::
