skip to content

For Hangfire 1.8, how do the SQL Server, PostgreSQL and Redis storages differ, and which options set pickup latency and schema creation?

level: middleimportance: should knowfreq 30%

answer

  1. only SQL Server ships in the Hangfire repo
  2. SQL Server QueuePollInterval = TimeSpan.Zero
  3. PostgreSQL QueuePollInterval = 15 s
  4. EnableLongPolling uses LISTEN/NOTIFY
  5. Hangfire.Pro.Redis is paid

basics

~20 s

Hangfire.SqlServer is maintained with Hangfire itself; Hangfire.PostgreSql and Hangfire.Redis.StackExchange are community packages, and Hangfire.Pro.Redis is commercial. Pickup latency comes from QueuePollInterval (sub-second on SQL Server, 15 s on PostgreSQL unless EnableLongPolling); PrepareSchemaIfNecessary creates the schema.

solid answer

~40 s

Hangfire 1.8's own repository ships one storage, **Hangfire.SqlServer** (`UseSqlServerStorage`). Its `QueuePollInterval` defaults to `TimeSpan.Zero`, which selects sub-second long polling, so jobs from other processes are picked up almost at once; it uses a 5-minute sliding invisibility timeout and creates the `HangFire` schema while `PrepareSchemaIfNecessary` is `true`. **Hangfire.PostgreSql** 1.21 is a community package: `UsePostgreSqlStorage(c => c.UseNpgsqlConnection(cs), options)`, schema `hangfire`, `QueuePollInterval` 15 seconds, so a job enqueued by another process can wait up to 15 s for a worker unless you set `EnableLongPolling`, which wakes workers through PostgreSQL `LISTEN`/`NOTIFY`. For Redis there are the paid, closed-source **Hangfire.Pro.Redis** and the community **Hangfire.Redis.StackExchange** (`UseRedisStorage`, key prefix `{hangfire}:`). Pick the database you already operate and back up; then tune polling, invisibility and schema creation explicitly.

code

csharp · 15 lines
csharp
// SQL Server: defaults already poll sub-second
config.UseSqlServerStorage(sqlConnectionString, new SqlServerStorageOptions
{
    PrepareSchemaIfNecessary = false   // schema applied by the deployment pipeline
});

// PostgreSQL: wake workers through LISTEN/NOTIFY instead of 15-second polling
config.UsePostgreSqlStorage(
    c => c.UseNpgsqlConnection(pgConnectionString),
    new PostgreSqlStorageOptions
    {
        EnableLongPolling = true,
        UseSlidingInvisibilityTimeout = true,
        PrepareSchemaIfNecessary = false
    });

go deeper

for a junior

Remember that Hangfire needs a storage package, that SQL Server's ships with Hangfire and PostgreSQL's and Redis's come separately.

for a middle

Explain QueuePollInterval defaults per storage, EnableLongPolling on PostgreSQL, and what PrepareSchemaIfNecessary does at startup.

for a senior

Override the defaults that hurt in production: cross-process pickup latency, PostgreSQL's fixed invisibility timeout, and schema DDL at application startup.

for a principal

Choose the storage from what the organisation can operate and recover, including licence cost for Hangfire.Pro.Redis and durability for any Redis option.

## What a job storage is Hangfire keeps everything (jobs, their states and history, queues, recurring definitions, server heartbeats, counters and locks) in a **job storage**, chosen in `AddHangfire`. The storage determines how quickly workers notice new jobs, how crashes are recovered, how locks behave and what you have to operate. Hangfire.Core itself is storage-agnostic. ## The options in the 1.8 ecosystem | Package | Maintained by | Configure with | Notes | |---|---|---|---| | `Hangfire.SqlServer` | the Hangfire project | `UseSqlServerStorage(connectionString, options)` | ships in the Hangfire repository | | `Hangfire.PostgreSql` (1.21) | community | `UsePostgreSqlStorage(c => c.UseNpgsqlConnection(cs), options)` | connection-string overloads obsolete | | `Hangfire.Redis.StackExchange` | community | `UseRedisStorage(connectionString, options)` | keys prefixed `{hangfire}:` | | `Hangfire.Pro.Redis` | Hangfire, commercial | (closed source) | paid licence, no public source | The practical rule: use the database the team already runs, monitors and backs up. Job data is operational state; losing the storage loses scheduled payouts and recurring definitions. ## Pickup latency How fast an idle worker notices a job enqueued by **another** process depends on the storage's polling (jobs enqueued in the same process wake local workers directly on both SQL Server and PostgreSQL): - **SQL Server**: `QueuePollInterval` defaults to `TimeSpan.Zero`, meaning long polling with sub-second delays (200 ms by default) coordinated by a semaphore so that one worker per queue set polls. Values of one second or more switch to plain polling at that interval. - **PostgreSQL**: `QueuePollInterval` defaults to **15 seconds**. Setting `EnableLongPolling = true` makes the enqueuing side issue `NOTIFY` and idle workers `LISTEN`, so they wake immediately; otherwise lower the interval and accept more queries. - **Redis (community)**: workers are woken through Redis pub/sub when a job is enqueued. Delayed jobs have a separate delay: the server's `SchedulePollingInterval` (15 s) for promoting Scheduled jobs. ## Schema creation and naming - `PrepareSchemaIfNecessary` is `true` by default on SQL Server and PostgreSQL: on startup the storage creates or migrates its tables. In locked-down production databases set it to `false` and run the install scripts in the deployment pipeline with a privileged account. - Default schemas: `HangFire` (SQL Server, `SchemaName` option) and `hangfire` (PostgreSQL, `SchemaName` option). Keep Hangfire out of EF Core's migrations. ## Other options worth knowing | Option | SQL Server default | PostgreSQL default | |---|---|---| | invisibility timeout | `SlidingInvisibilityTimeout` 5 min | `InvisibilityTimeout` 30 min, `UseSlidingInvisibilityTimeout` false | | `JobExpirationCheckInterval` | 30 min | 1 h | | distributed locks | session application locks | `DistributedLockTimeout` 10 min | ## A marketplace on PostgreSQL 1. `UsePostgreSqlStorage(c => c.UseNpgsqlConnection(cs), new PostgreSqlStorageOptions { EnableLongPolling = true, UseSlidingInvisibilityTimeout = true })`. 2. Hangfire's tables in the `hangfire` schema of the main database, created by the deployment pipeline. 3. Dedicated `critical` and `default` servers, as sized elsewhere. That combination gives near-immediate pickup for payouts, crash recovery within minutes and no duplicate runs of long settlement jobs.

  • Why might a Hangfire.PostgreSql job enqueued from the web tier wait 10 seconds before starting, while one enqueued by a job starts at once?
    Jobs enqueued in the worker process signal its own idle workers directly. Jobs enqueued by the separate web process are only seen on the next poll, every 15 seconds by default. `EnableLongPolling = true` (LISTEN/NOTIFY) or a shorter `QueuePollInterval` fixes the cross-process delay.
  • What should you know before choosing Redis for Hangfire storage?
    Which package: Hangfire.Pro.Redis is a paid, closed-source storage from the Hangfire project; Hangfire.Redis.StackExchange is a community package with its own defaults (30-minute invisibility timeout, `{hangfire}:` key prefix). Either way the Redis deployment must persist data durably, because it now holds jobs, not cache entries.

  • Hangfire.PostgreSql is part of the official Hangfire repository like Hangfire.SqlServer.
  • All Hangfire storages pick up jobs within a second by default.
  • PrepareSchemaIfNecessary defaults to false, so the schema must always be scripted.
  • Hangfire.Pro.Redis is free and open source.
  • Hangfire's tables should be added to the app's EF Core migrations.

Assumes Hangfire 1.8.25 with Hangfire.SqlServer from the same repository, Hangfire.PostgreSql 1.21 and the community Hangfire.Redis.StackExchange; defaults differ between these packages.