Hangfire.InMemory 1.0.0

Hangfire.InMemory

Latest version Build status Quality Gate Status Bugs Code Smells

This in-memory job storage aims to provide developers a quick way to start using Hangfire without additional infrastructure like SQL Server or Redis. It offers the flexibility to swap out the in-memory implementation in a production environment. This is not intended to compete with TPL or other in-memory processing libraries, as serialization itself incurs a significant overhead.

The implementation includes proper synchronization mechanisms, utilizing blocking operations for locks, queues, and queries. This avoids active polling. Read and write operations are handled by a dedicated background thread to minimize synchronization between threads, keeping the system simple and ready for future async-based enhancements. Internal states use SortedDictionary, SortedSet, and LinkedList to avoid large allocations on the Large Object Heap, thus reducing potential OutOfMemoryException issues caused by memory fragmentation.

Additionally, this storage uses a monotonic clock where possible, using the Stopwatch.GetTimestamp method. This ensures that expiration rules remain effective even if the system clock changes abruptly.

Requirements

  • Hangfire 1.8.0 and above: any latest version of Hangfire.InMemory
  • Hangfire 1.7.0: Hangfire.InMemory 0.11.0 and above
  • Hangfire 1.6.0: Hangfire.InMemory 0.3.7

Installation

Hangfire.InMemory is available on NuGet. You can install it using your preferred package manager:

> dotnet add package Hangfire.InMemory

Configuration

After installing the package, configure it using the UseInMemoryStorage method on the IGlobalConfiguration interface:

GlobalConfiguration.Configuration.UseInMemoryStorage();

Maximum Expiration Time

Starting from version 0.7.0, the package controls the maximum expiration time for storage entries and sets it to 3 hours by default when a higher expiration time is passed. For example, the default expiration time for background jobs is 24 hours, and for batch jobs and their contents, the default time is 7 days, which can be too big for in-memory storage that runs side-by-side with the application.

You can control this behavior or even turn it off with the MaxExpirationTime option available in the InMemoryStorageOptions class in the following way:

GlobalConfiguration.Configuration.UseInMemoryStorage(new InMemoryStorageOptions
{
    MaxExpirationTime = TimeSpan.FromHours(3) // Default value, we can also set it to `null` to disable.
});

It is also possible to use TimeSpan.Zero as a value for this option. In this case, entries will be removed immediately instead of relying on the time-based eviction implementation. Please note that some unwanted side effects may appear when using low value – for example, an antecedent background job may be created, processed, and expired before its continuation is created, resulting in exceptions.

Comparing Keys

Different storages use different rules for comparing keys. Some of them, like Redis, use case-sensitive comparisons, while others, like SQL Server, may use case-insensitive comparison implementation. It is possible to set this behavior explicitly and simplify moving to another storage implementation in a production environment by configuring the StringComparer option in the InMemoryStorageOptions class in the following way:

GlobalConfiguration.Configuration.UseInMemoryStorage(new InMemoryStorageOptions
{
    StringComparer = StringComparer.Ordinal // Default value, case-sensitive.
});

Setting Key Type Jobs

Starting from version 1.0, Hangfire.InMemory uses long-based keys for background jobs, similar to the Hangfire.SqlServer package. However, you can change this to use Guid-based keys to match the Hangfire.Pro.Redis experience. To do so, simply configure the InMemoryStorageOptions.IdType property as follows:

GlobalConfiguration.Configuration.UseInMemoryStorage(new InMemoryStorageOptions
{
    IdType = InMemoryStorageIdType.Guid
});

Setting the Maximum State History Length

The MaxStateHistoryLength option in the InMemoryStorageOptions class sets the maximum number of state history entries to be retained for each background job. This is useful for controlling memory usage by limiting the number of state transitions stored in memory.

By default, Hangfire.InMemory retains 10 state history entries, but you can adjust this setting based on your application's requirements.

GlobalConfiguration.Configuration.UseInMemoryStorage(new InMemoryStorageOptions
{
    MaxStateHistoryLength = 10 // default value
});

No packages depend on Hangfire.InMemory.

https://github.com/HangfireIO/Hangfire.InMemory/releases 1.0.0 • Breaking – Remove the deprecated DisableJobSerialization option. • Breaking – Change default value for the IdType option to long.

0.10.4 • Fixed – Problem with locks implementation due to a regression in .NET 8.0.

0.10.3 • Changed – Significantly optimize GetFirstByLowestScoreFromSet method overloads.

0.10.2 • Changed – Refactor command dispatching to make it more simple and less allocating. • Changed – Straightforward locking implementation with more unit tests. • Fixed – InvalidOperationException "Wrong level" when trying to release a lock (regression from 0.10.1). • Fixed – "An item with the same key has already been added" on the Awaiting Jobs page (regression from 0.10.0).

0.10.1 • Changed – Roll back a breaking change in 0.10.0 for the InMemoryStorageOptions class. • Changed – Increase the default eviction interval to 5 seconds. • Changed – More efficient storage of state history records. • Changed – Implement fast path for the FetchNextJob method. • Fixed – More robust entry eviction implementation. • Fixed – Graceful dispatcher shutdown without additional waiting. • Project – Faster build pipeline on AppVeyor after migration to modern Powershell 7+.

0.10.0 • Breaking – InMemoryStorageOptions class instances are now immutable after initialization. • Added – Support long-based job identifiers through the InMemoryStorageOptions.IdType property. • Added – Expose the InMemoryStorageOptions.CommandTimeout option to control the command timeouts. • Changed – Significantly improve query dispatching pipeline in terms of speed and allocations. • Changed – More compact representation of jobs and their parameters. • Changed – Optimise the GetFirstByLowestScoreFromSet query when the number of items is huge. • Changed – Better concurrency handling implementation for the collection of locks.

0.9.0 • Added – Implement the disposable pattern for the InMemoryStorage class. • Changed – Use more compact representation of job parameters and state data. • Changed – Move to SortedDictionary and LinkedList to avoid using Large Object Heap. • Changed – TimeSpan.Zero value for MaxExpirationTime now causes immediate entry eviction. • Fixed – Ensure near-zero max expiration limit can't lead to uninitialized job eviction. • Deprecated – DisableJobSerialization option is now obsolete, serialization is always enabled.

0.8.1 • Fixed – Incorrect validation in the MaxStateHistoryLength setter (by @DPschichholz).

0.8.0 • Project – Sign NuGet package and .NET assemblies on build with a company's own certificate. • Project – Require package signature validation when restoring dependencies. • Project – Add HangfireIO as an owner for the NuGet package. • Project – Add readme file and icon to the NuGet package. • Project – Fix Git repository URL in the NuGet package metadata.

0.7.0 • Added – InMemoryStorageOptions.MaxExpirationTime option to control the maximum expiration time. • Changed – The default value for maximum expiration time is 2 hours now, not days. • Fixed – Populate ParametersSnapshot and InvocationData properties in IMonitoringApi.JobDetails. • Fixed – The "Awaiting Jobs" page now includes the state name of an antecedent background job. • Fixed – The "Scheduled Jobs" page now has correct identifiers for jobs with explicit queues defined. • Fixed – Unify job ordering in Monitoring API to be the same as in other storages. • Project – Enable source link support with embedded symbols for simplified debugging. • Project – Refactored internals and added even more unit tests. • Project – Enable NuGet package restore with lock file and locked mode. • Project – Project and Release Notes URLs in the NuGet package now point to the repository. • Project – Enable tests running on the net6.0 platform and Ubuntu on AppVeyor.

0.6.0 • Added – InMemoryStorageOptions.MaxStateHistoryLength option to control state entries. • Changed – Always use monotonic clock when working with time. • Changed – Release distributed locks when their connection is disposed. • Changed – Pass dispatcher fault exceptions to a caller thread. • Project – Refactor internal types to have a cleaner project structure and avoid mistakes. • Project – Enable static analysis by the Microsoft.CodeAnalysis.NetAnalyzers package. • Project – Enable portable PDBs for the .NET Framework 4.5.1 platform.

0.5.1 • Fixed – Infinite loop in recurring job scheduler consuming 100% CPU regression after 0.5.0.

0.5.0 • Added – InMemoryStorageOptions.StringComparer as a central option for key and index comparisons.

0.4.1 • Fixed – "Awaiting Jobs" metric is now correctly populated with Version180 compatibility level.

0.4.0 • Breaking – Package now depends on Hangfire.Core version 1.8.0. • Breaking – Replace the net45 target with net451 one as the former is not supported.

• Changed – Improve GetFirstByLowestScoreFromSet operations. • Changed – Implement the Job.Queue feature. • Changed – Implement the Connection.GetUtcDateTime feature. • Changed – Implement the Connection.GetSetContains feature. • Changed – Implement the Connection.GetSetCount.Limited feature. • Changed – Implement the Connection.BatchedGetFirstByLowestScoreFromSet feature for the storage. • Changed – Implement the Transaction.AcquireDistributedLock feature. • Changed – Implement the Transaction.CreateJob feature. • Changed – Implement the Transaction.SetJobParameter feature. • Changed – Implement the new monitoring features. • Changed – Populate the new properties in Monitoring API. • Changed – Populate the Retries metric in the GetStatistics method.

0.3.7 • Fixed – Throw BackgroundJobServerGoneException outside of dispatcher thread.

0.3.6 • Fixed – Ensure lock entries are eventually removed from their collection. • Fixed – Ensure lock entries are always updated under a monitor.

0.3.5 • Fixed – Ensure entries are expired even during constant storage pressure.

0.3.4 • Fixed – Reverse state list instead of sorting it by date in the JobDetails method. • Fixed – Better sorting for state indexes, take into account job creation date. • Fixed – Reverse succeeded and deleted job lists to match Redis implementation.

0.3.3 • Fixed – Sort queues and servers when returning them from monitoring api and in the Dashboard UI.

0.3.2 • Fixed – Enqueued jobs may become invisible when adding a lot of jobs simultaneously to a new queue. • Fixed – Some workers are waiting for background jobs forever when several jobs added at once. • Fixed – Workers are able to detect new background jobs only after another background job is processed. • Fixed – Workers don't see background jobs when multiple queues are used with minimal workload.

0.3.1 • Fixed – NullReferenceException in the SignalOneQueueWaitNode method when using multiple queues.

0.3.0 • Added – InMemoryStorageOptions.DisableJobSerialization option. • Fixed – ObjectDisposedException on semaphore when committing a transaction. • Fixed – Gracefully handle ObjectDisposedException when signaling for query completion. • Fixed – Avoid killing the whole process in case of an exception in dispatcher, stop it instead. • Project – Add a lot of new unit tests for InMemoryMonitoringApi class.

0.2.0 • Fixed – A lot of corner cases revealed by unit tests. • Project – Added a ton of unit tests for the top-level classes to ensure behavior is consistent.

0.1.0 – Initial release

.NET Framework 4.5.1

.NET Standard 2.0

Version Downloads Last updated
1.0.0 9 2025-11-24
0.11.0 0 2024-09-18
0.10.4 0 2024-09-10
0.10.3 0 2024-06-17
0.10.2 0 2024-06-17
0.10.0 0 2024-06-03
0.9.0 0 2024-04-23
0.8.1 0 2024-03-21
0.8.0 0 2024-02-16
0.7.0 0 2024-01-10
0.6.0 0 2023-10-23
0.5.1 0 2023-06-28
0.5.0 0 2023-06-27
0.4.1 0 2023-05-24
0.4.0 0 2023-04-28
0.3.7 0 2023-03-22
0.3.6 0 2023-02-08
0.3.5 0 2022-11-25
0.3.4 0 2020-11-06
0.3.3 0 2020-11-06
0.3.2 0 2020-10-29
0.3.1 0 2020-10-22
0.3.0 0 2020-10-22
0.2.0 0 2020-10-12
0.1.0 0 2020-09-29