Hangfire
Hangfire is a robust background job framework for .NET that allows applications to execute logic outside the main web request pipeline. Since Hangfire itself does not store state, it requires a persistent storage backend to manage jobs, queues, execution history, and server coordination.
Integrating NCache with Hangfire allows NCache to serve as the shared, distributed storage engine for managing Hangfire’s job data and state. By replacing traditional SQL or Redis backends with NCache, your background processing pipeline benefits from an in-memory, highly scalable, and distributed architecture where all Hangfire components coordinate through a single shared cache.
Note
This feature is currently supported in the NCache OpenSource edition.
Important
Hangfire is supported in NCache 5.3.6.2 and onwards.
Decoupled Clients and Servers
All Hangfire instances interact exclusively through a shared NCache cluster, eliminating the need for direct client-to-server communication.
Hangfire Client (Web App/API): The application submits background jobs to Hangfire, which uses the NCache storage provider to store job payloads, arguments, and parameters in NCache, create execution queue entries, and trigger Pub/Sub queue wake-up signals. It does not execute the background tasks directly.
Hangfire Server (Worker Background Process): The worker receives queue wake-up signals via NCache Pub/Sub notifications or polls the queues, then attempts to dequeue jobs under an NCache distributed lock, executes the actual code, and updates terminal state records back in the cache.
How It Works: The Request Flow
When you enqueue a background task, NCache manages the job from creation to execution across your servers through the following steps:
Job Registration: Your application creates a job through Hangfire, which uses the NCache storage provider to save its details, arguments, and parameters in NCache with an initial expiration.
Queue Insertion: The job ID is appended to the designated queue entry in NCache under a distributed write transaction.
Queue Notification: If Pub/Sub is enabled, NCache sends a queue wake-up signal to waiting workers when new work is available.
Locked Dequeue & Execution: An available Hangfire worker attempts to dequeue the job from NCache under a distributed lock to prevent another server from claiming the same job concurrently, marks it as in progress, and executes the workload.
State Persistence & Batching: Once finished, results (Succeeded/Failed) and counters are grouped together and saved to NCache in periodic batches to keep performance fast and smooth.
Supported Job Types
The NCache storage provider natively manages the backing data structures for standard Hangfire job patterns:
Fire-and-Forget: Added directly to the queue in NCache so an available worker can run it immediately.
Delayed/Scheduled: Stored with a future execution time in NCache; once that time arrives, Hangfire automatically moves the job into the active queue.
Recurring: Job definitions and schedule metadata are stored as NCache hash entries and sets, which Hangfire evaluates periodically.
Continuations: Stored in a waiting state in NCache and only queued for execution after their parent job finishes successfully.
Reliability and Crash Recovery
The NCache storage provider includes built-in background safety mechanisms for worker failures and missed queue notifications:
Crashed Worker Recovery (
FetchedJobsWatcher): While a job is being processed, itsFetchedAttimestamp is periodically renewed at approximately one-third of the configuredInvisibilityTimeout. If the worker or process crashes, the renewal stops. Once the processing entry becomes stale,FetchedJobsWatcherdetects it and requeues the job at the front of its original queue. Because recovery is timeout-based, jobs should be designed to tolerate possible duplicate execution. Recovery occurs when the timeout has elapsed and the watcher performs its next scan; it does not necessarily occur at the exact moment the timeout expires.Backlog Signal Recovery (
QueueBacklogWatcher): If a real-time queue notification is missed, NCache periodically checks for waiting jobs every 10 seconds and sends queue wake-up signals to workers, ensuring that queued tasks continue to be processed.
In This Section
NCache as Hangfire Storage Provider
Learn how to configure and use NCache as an in-memory distributed storage provider for Hangfire in ASP.NET Core applications.