Large Azure Storage jobs are easy to start and much harder to stop correctly.
A small application might upload a few files, download an object, or copy data between containers and finish within seconds. A production data process can be very different. It may process thousands of blobs, move large objects, copy data between storage accounts, or run as a background worker for several hours.
The difficult part is not only starting that work. The application also needs to know what should happen when the job is cancelled, the application is shutting down, a timeout occurs, a user presses Stop, or a deployment replaces the running process.
A clean stop means the application stops accepting new work, allows safe operations to finish when possible, cancels work that should not continue, records enough information to resume later, and avoids leaving the system in an unknown state.
This becomes especially important with Azure Blob Storage because many storage operations involve network calls and can take considerably longer than normal application requests.
This article explains how to design Azure Storage jobs so they can be cancelled or stopped safely without treating every interruption as a complete failure.
What Does “Stop Cleanly” Mean?
Stopping a storage job cleanly does not simply mean killing the process.
Suppose an application is processing 50,000 blobs. It has already completed 31,000 blobs and is currently processing blob 31,001 when the application receives a shutdown request.
A poor implementation might terminate immediately.
The result could be:
Work that was already completed may be repeated.
A blob operation may be interrupted.
The application may not record its progress.
Retry logic may restart too much work.
Temporary files may remain.
A copy operation may still be running.
Operators may not know where the job stopped.
A graceful implementation treats stopping as part of the normal lifecycle.
A typical lifecycle looks like this:
Running
|
| Stop requested
v
Stop accepting new work
|
v
Cancel or finish current operations
|
v
Save progress
|
v
Release resources
|
v
ExitThe exact behavior depends on the type of storage operation, but the principle remains the same.
Why Large Storage Jobs Need Special Handling
Azure Storage operations are remote operations. The application sends a request to Azure Storage, waits for a response, and may continue processing the returned data.
For a small operation, interruption may not matter much.
For a long-running job, interruption creates several questions.
Was the operation completed?
Was the response received?
Did the server complete the operation before the client disconnected?
Should the application retry it?
Was the data already copied?
Was the destination partially updated?
These questions are important because a network timeout does not always mean the server did nothing.
For example, an application might send a blob operation and then lose its network connection before receiving the response. Retrying without understanding the operation can result in duplicate work or unnecessary processing.
This is why cancellation, retries, and idempotency need to be designed together.
Cancellation Tokens Are the First Building Block
In .NET applications, CancellationToken provides a standard way to tell asynchronous work that it should stop.
The token does not forcibly terminate a running method. Instead, application code and supporting APIs observe the token and stop when cancellation is appropriate.
A simple worker can look like this:
public async Task ProcessBlobsAsync(
BlobContainerClient container,
CancellationToken cancellationToken)
{
await foreach (var blob in container.GetBlobsAsync(
cancellationToken: cancellationToken))
{
cancellationToken.ThrowIfCancellationRequested();
await ProcessBlobAsync(
container,
blob.Name,
cancellationToken);
}
}The important part is that cancellation is passed through the entire call chain.
For example:
Worker
|
+-- ProcessBlobsAsync
|
+-- ProcessBlobAsync
|
+-- DownloadAsync
|
+-- UploadAsyncIf the top-level method receives a cancellation token but the lower-level methods ignore it, the application may still take a long time to stop.
Passing Cancellation Through Storage Operations
A common mistake is to implement cancellation only around the outer loop.
For example, this code checks cancellation before starting an operation:
cancellationToken.ThrowIfCancellationRequested();
await blobClient.DownloadToAsync(filePath);But the download itself does not receive the token.
If the download takes several minutes, cancellation may not be observed until the download finishes.
A better approach is to pass the token into the Azure SDK operation where the API supports cancellation:
await blobClient.DownloadToAsync(
filePath,
cancellationToken);The same principle applies to uploads and other asynchronous operations that expose cancellation support.
The goal is not to make every operation instantly disappear. The goal is to give the application and SDK a way to stop waiting for work when cancellation is requested.
Graceful Shutdown in a Background Worker
A long-running storage process is often implemented as a .NET BackgroundService.
The worker should treat cancellation as an expected lifecycle event rather than an unexpected exception.
A simplified implementation looks like this:
public class StorageWorker : BackgroundService
{
private readonly BlobContainerClient _container;
public StorageWorker(BlobContainerClient container)
{
_container = container;
}
protected override async Task ExecuteAsync(
CancellationToken stoppingToken)
{
try
{
await ProcessBlobsAsync(
_container,
stoppingToken);
}
catch (OperationCanceledException)
when (stoppingToken.IsCancellationRequested)
{
// Expected during graceful shutdown.
}
}
private async Task ProcessBlobsAsync(
BlobContainerClient container,
CancellationToken cancellationToken)
{
await foreach (var blob in container.GetBlobsAsync(
cancellationToken: cancellationToken))
{
cancellationToken.ThrowIfCancellationRequested();
await ProcessBlobAsync(
container,
blob.Name,
cancellationToken);
}
}
private async Task ProcessBlobAsync(
BlobContainerClient container,
string blobName,
CancellationToken cancellationToken)
{
await Task.Delay(100, cancellationToken);
// Application-specific processing.
}
}The important detail is the distinction between cancellation and failure.
If the service is shutting down and the cancellation token is cancelled, an OperationCanceledException can be expected.
If a storage request fails because authentication is invalid or the storage account is unavailable, that is a different operational condition and should be handled separately.
Do Not Treat Cancellation as a Storage Error
This distinction becomes important in production monitoring.
Consider a worker that is restarted during a deployment.
The worker receives a shutdown signal, cancels its current operation, and exits.
If the monitoring system records this as a storage failure, operators may investigate a problem that never existed.
A useful classification is:
Expected cancellation
|
+-- Deployment shutdown
+-- User requested stop
+-- Application shutdown
Operational failure
|
+-- Authentication failure
+-- Permission failure
+-- Network failure
+-- Storage service error
Application failure
|
+-- Invalid input
+-- Processing exception
+-- Data validation failureThese categories should produce different logs, metrics, and alerts.
Use Timeouts for Operations That Should Not Run Forever
Cancellation and timeout solve related but different problems.
Cancellation usually means another part of the application has decided that the work should stop.
A timeout means the operation has taken longer than an acceptable limit.
For example, a blob download might normally complete within a few seconds. If the application waits indefinitely because of a network problem, the worker can become stuck.
A timeout can provide a maximum waiting period.
For application-level operations, a linked cancellation token can combine both conditions:
using var timeoutCts =
CancellationTokenSource.CreateLinkedTokenSource(
stoppingToken);
timeoutCts.CancelAfter(TimeSpan.FromMinutes(5));
await blobClient.DownloadToAsync(
filePath,
timeoutCts.Token);This creates two cancellation paths:
Application shutdown
|
v
stoppingToken
|
+------+
|
v
Linked token
^
|
5-minute timeoutThe operation stops when either condition occurs.
The timeout should be based on the actual workload. A five-minute limit might be reasonable for one application and completely wrong for another that regularly transfers large objects.
Do Not Use Very Short Timeouts for Large Files
A common mistake is applying one timeout to every storage operation.
Suppose an application transfers files ranging from 10 MB to 500 GB.
A timeout that works for a 10 MB object may be completely unsuitable for a 500 GB object.
Instead, consider:
File size
Expected network throughput
Storage operation type
Application SLA
Retry policy
Maximum acceptable processing time
The timeout should represent the application's operational expectation rather than an arbitrary number.
Stop Starting New Work First
When a large batch job receives a stop request, the safest first step is usually to stop scheduling new work.
Consider a worker processing batches:
Batch 1 -> running
Batch 2 -> running
Batch 3 -> queued
Batch 4 -> queued
Batch 5 -> queuedWhen cancellation arrives, the application should avoid starting Batch 4 and Batch 5.
This sounds simple, but concurrent workers can make it more complicated.
For example, if 20 tasks are consuming work from a queue, cancellation may arrive while several tasks are between fetching the next item and starting the storage operation.
The worker should check cancellation at those boundaries.
while (!cancellationToken.IsCancellationRequested)
{
var item = await GetNextItemAsync(cancellationToken);
if (item == null)
break;
await ProcessItemAsync(
item,
cancellationToken);
}This prevents the application from continuing to create unnecessary work after shutdown begins.
Limit Concurrency During Large Storage Jobs
Large storage jobs often use parallel processing to improve throughput.
For example:
1 worker -> slow
8 workers -> faster
50 workers -> potentially faster
500 workers -> potentially unstableMore concurrency does not automatically mean better performance.
Too much parallelism can increase:
Network pressure
Memory consumption
CPU usage
Storage throttling
Retry volume
Application shutdown time
A controlled concurrency limit is usually easier to operate.
For example:
var options = new ParallelOptions
{
MaxDegreeOfParallelism = 8,
CancellationToken = cancellationToken
};
await Parallel.ForEachAsync(
blobs,
options,
async (blob, token) =>
{
await ProcessBlobAsync(
container,
blob.Name,
token);
});When cancellation occurs, the parallel operation can stop scheduling additional work while existing operations respond to the cancellation token.
Make Processing Idempotent
Idempotency is one of the most important concepts for resumable storage jobs.
An operation is idempotent when repeating the same operation produces the same intended result rather than creating an additional unwanted effect.
For example, suppose a job reads a blob and generates a processed output.
Instead of assuming that the job will always run exactly once, design it so that the same input can safely be processed again.
A simple pattern is:
Input Blob
|
v
Check processing state
|
+-- Already completed --> Skip
|
+-- Not completed ------> Process
|
v
Write result
|
v
Record completionThis matters because cancellation can happen at almost any point.
The application may stop after writing the output but before recording the completion state.
When the job restarts, it should be able to determine whether the output already exists and whether it is valid.
Do Not Mark Work Complete Too Early
Consider this sequence:
1. Read blob
2. Process blob
3. Upload result
4. Mark blob as completedThis is safer than:
1. Read blob
2. Mark blob as completed
3. Process blob
4. Upload resultIf the application stops between steps 2 and 3 in the second design, the system may believe the work is complete when it is not.
Completion state should represent successful completion of the required operation.
The exact definition of completion depends on the application, but it should be based on an observable successful result.
Checkpoint Large Jobs
A checkpoint records enough progress information for a job to continue later.
Suppose a job processes 100,000 blobs.
Without checkpoints:
Start
|
v
Process 100,000 blobs
|
v
Failure
|
v
Start all overWith checkpoints:
Start
|
v
Process 10,000
|
v
Checkpoint
|
v
Process 10,000
|
v
Checkpoint
|
v
Failure
|
v
Resume from checkpointA checkpoint might contain:
Job ID
Source container
Last successfully processed item
Processing status
Timestamp
Application version
Number of successful items
Number of failed items
For large workloads, checkpointing can significantly reduce the amount of repeated work after a restart.
Checkpointing Is Not Always Just a Last Blob Name
A common mistake is storing only the last processed blob name.
Blob listings and processing order can change depending on how the application retrieves and filters data.
A more reliable design may use an explicit work table or queue where each work item has its own state.
For example:
Work Item
---------
ID
Blob Name
Status
StartedAt
CompletedAt
RetryCount
ErrorMessageThis gives the application a durable record of progress.
It also makes parallel processing easier because multiple workers can operate on different work items.
Handle Retries Carefully
Azure Storage applications commonly use retries because transient network and service conditions can occur.
Retries are useful, but cancellation should not accidentally turn into repeated work.
Consider this simplified logic:
Operation starts
|
v
Transient failure
|
v
Retry
|
v
Transient failure
|
v
RetryNow add cancellation:
Operation starts
|
v
Cancellation requested
|
v
Should retry?
|
+-- No
|
v
StopA cancellation request should normally prevent unnecessary retry attempts.
There is no value in starting another five retries when the application has already been instructed to shut down.
Retry policies should distinguish between transient failures and cancellation.
Understand Long-Running Copy Operations
Blob copy operations deserve special attention because the application may start a server-side operation and then receive a response indicating that the copy has not completed yet.
The application needs to understand the operation lifecycle rather than assuming:
Start copy = Copy finishedA long-running copy can behave more like:
Start copy
|
v
Copy pending
|
+---- Poll status
|
v
Copy completedIf the application is shutting down while the server-side copy is still running, cancellation of the client process does not necessarily mean the server-side operation has been cancelled.
This distinction is important.
Application cancellation means:
Stop waiting / stop client-side workIt does not automatically mean:
Undo every operation already accepted by Azure StorageFor long-running server-side operations, the application should record the operation state and check it again after restart when appropriate.
Use Leases When Work Ownership Matters
Azure Blob leases can help coordinate ownership of work.
A lease provides temporary exclusive access to a blob under specific conditions.
For example, multiple workers may see the same blob as available:
Worker A ----\
> Blob
Worker B ----/Without coordination, both workers may attempt to process it.
A lease can help establish:
Worker A
|
v
Acquire lease
|
v
Process blob
|
v
Release leaseIf Worker A stops unexpectedly, the lease eventually expires according to its lease rules, allowing another worker to take ownership.
Leases should not be treated as a complete job-management system. They are a coordination mechanism. The application still needs durable processing state and correct retry behavior.
Handle Application Shutdown Separately From User Cancellation
Not every cancellation has the same business meaning.
For example:
Deployment shutdown
-> Stop safely and resume later
User presses Stop
-> Stop the current job and mark it cancelled
Timeout
-> Record timeout and retry according to policy
Storage failure
-> Retry if transientThe application may use the same CancellationToken mechanism internally, but the business state can still be different.
This distinction is especially useful for administrative systems where operators need to know whether a job was cancelled intentionally, interrupted during deployment, or failed because of infrastructure problems.
Save State Before Exiting
A graceful shutdown should give the application enough time to save its current state.
A typical shutdown sequence might be:
Receive shutdown signal
|
v
Stop accepting new work
|
v
Cancel active operations
|
v
Record unfinished work
|
v
Release resources
|
v
ExitThe exact shutdown timeout depends on the hosting environment.
If the shutdown window is too short, the application may be terminated before it has finished saving state.
For this reason, shutdown behavior should be tested instead of assuming that graceful shutdown always works.
Test Cancellation During Different Stages
Do not test cancellation only when the worker is idle.
A production job can be interrupted during many different states.
Test cancellation:
Before a blob is selected.
During blob download.
During application processing.
During blob upload.
After upload but before completion state is saved.
During retry backoff.
During checkpoint creation.
During parallel processing.
During a long-running copy operation.
During application shutdown.
Each point can expose a different failure mode.
For example, cancellation after the output is uploaded but before the completion record is saved can reveal whether the application safely handles duplicate processing.
Monitor Job Progress
A long-running storage job should expose useful operational information.
Useful metrics include:
Items discovered
Items processed
Items completed
Items failed
Items skipped
Items cancelled
Retry count
Current concurrency
Processing rate
Average processing timeLogs should also identify the job and item being processed.
For example:
Job 81f2 started
Job 81f2 processing blob customer-10024.json
Job 81f2 uploaded processed result
Job 81f2 marked blob customer-10024.json as completed
Job 81f2 received shutdown request
Job 81f2 stopped with 428 items remainingThis is much more useful than a single message saying:
Worker stopped.Operators need to understand what happened and whether the job can safely resume.
Common Mistakes
Killing the Process Instead of Cancelling Work
Forcefully terminating the process prevents cleanup and state persistence.
Use graceful shutdown whenever the hosting environment allows it.
Checking Cancellation Only Once
Checking the token at the beginning of a large method is usually not enough.
Long-running loops and asynchronous storage operations should observe cancellation throughout the workflow.
Retrying Cancelled Operations
A cancellation request should not normally trigger another retry cycle.
Treat cancellation as a stop condition.
Assuming Client Cancellation Cancels Server Work
Stopping the local process does not necessarily undo an operation that Azure Storage has already accepted.
Long-running operations need explicit state tracking.
Using Unlimited Parallelism
Creating hundreds or thousands of simultaneous storage operations can increase throttling and make shutdown harder.
Use controlled concurrency.
Recording Completion Before the Result Exists
Completion should represent successful work.
Do not mark an item complete before its required output has been safely produced.
Not Testing Restart Behavior
A storage job that cannot restart safely is difficult to operate.
Test the complete cycle:
Start
Process
Cancel
Restart
Resume
CompleteA Practical Production Pattern
A reliable architecture for a large Azure Storage job can look like this:
Job Controller
|
v
Work Queue / State
|
+----------+----------+
| | |
v v v
Worker A Worker B Worker C
| | |
+----------+----------+
|
v
Azure Storage
|
v
Result / StateEach worker should:
Receive a cancellation token.
Stop accepting new work after cancellation.
Use bounded concurrency.
Pass cancellation to storage operations.
Retry transient failures.
Avoid retrying cancellation.
Record successful completion.
Save enough state to resume.
Log meaningful progress.
Release resources during shutdown.
The work queue or state store provides the durable view of the job. Azure Storage remains the data store, while the processing system maintains the information needed to safely execute and resume the workload.
When This Design Is Worth Using
Graceful cancellation and resumable processing become especially valuable when:
A job processes thousands of blobs.
Files are large.
Data processing runs for hours.
Deployments happen regularly.
Workers can be restarted.
Multiple workers process the same workload.
Storage operations can take a long time.
Duplicate processing has a cost.
Operators need to stop jobs manually.
The application runs in containers or managed compute environments.
For a tiny script that uploads one file and exits, this architecture may be unnecessary.
The more expensive and long-running the workload becomes, the more valuable explicit cancellation and progress tracking become.
Advantages and Disadvantages
Advantages
Safer application shutdown.
Less repeated processing after restarts.
Better handling of long-running operations.
Clearer operational visibility.
Easier deployment of background workers.
Better control over concurrency.
Improved recovery from transient failures.
More predictable job behavior.
Disadvantages
More application state to maintain.
Checkpointing adds design and storage requirements.
Idempotency requires additional planning.
Testing becomes more extensive.
Long-running server-side operations need additional lifecycle tracking.
Incorrect cancellation handling can still create inconsistent state.
The extra complexity is usually justified when the storage workload is large enough that restarting from the beginning is expensive or unsafe.
Production Checklist
Before running a large Azure Storage job in production, verify the following:
CancellationToken is passed through the processing pipeline.
Azure Storage operations receive cancellation where supported.
New work stops when shutdown begins.
Retry logic distinguishes cancellation from transient failure.
Timeouts are based on realistic workload durations.
Concurrency is bounded.
Processing is idempotent where possible.
Completion state is recorded only after successful processing.
Large jobs have a recovery or checkpoint strategy.
Long-running server-side operations have status tracking.
Blob leases are used where work ownership requires them.
Shutdown behavior is tested.
Restart behavior is tested.
Progress metrics are available.
Logs identify jobs and work items.
Cancelled jobs are distinguishable from failed jobs.
Operators know how to resume or restart a stopped job.
Summary
Large Azure Storage jobs should not be designed around the assumption that they will always run from start to finish.
Deployments, application restarts, user cancellation, timeouts, network failures, and infrastructure events can interrupt long-running work. The important part is how the application behaves when that happens.
Use cancellation tokens to propagate stop requests through the application and into supported Azure Storage operations. Stop scheduling new work as soon as shutdown begins, keep concurrency under control, and use realistic timeouts for operations that should not wait indefinitely.
For workloads that take a long time to complete, combine cancellation with idempotent processing, durable progress tracking, checkpointing, and appropriate retry behavior. Also remember that cancelling the local client does not automatically mean an already accepted server-side storage operation has been cancelled.
The result is a storage worker that can stop without losing its place and can start again without unnecessarily repeating large amounts of work. That makes long-running Azure Storage processing much easier to operate during deployments, failures, and normal administrative shutdowns.


Join the conversation! Your thoughts help the community grow.