{
  "schema_version": 2,
  "id": "integrate/hangfire",
  "title": "Redis with Hangfire",
  "url": "https://redis.io/docs/latest/integrate/hangfire/",
  "summary": "Run .NET background jobs with Hangfire and the community Redis storage package.",
  "tags": [
    "docs",
    "integrate",
    "oss",
    "rs",
    "rc"
  ],
  "last_updated": "2026-11-17T16:15:30Z",
  "children": [],
  "page_type": "content",
  "content_hash": "ee48fd3681fd47731a30ce8a9f8e77dea14d4126e65d23d04c71a7909c69a7a7",
  "sections": [
    {
      "id": "overview",
      "title": "Overview",
      "role": "overview",
      "text": "[Hangfire](https://www.hangfire.io/) runs background jobs in .NET applications. It stores\njobs in a pluggable storage, and its default is SQL Server. See the [job queue use case](https://redis.io/docs/latest/develop/use-cases/job-queue) for when to choose\nthis pattern."
    },
    {
      "id": "choose-a-redis-storage-package",
      "title": "Choose a Redis storage package",
      "role": "content",
      "text": "Hangfire's core package doesn't include Redis storage. You can get it in two ways:\n\n- **`Hangfire.Redis.StackExchange`** is a community package, listed on Hangfire's\n  [extensions page](https://www.hangfire.io/extensions.html). It is licensed under the\n  LGPL v3 and built on the [StackExchange.Redis](https://redis.io/docs/latest/develop/clients/dotnet)\n  client. Hangfire OÜ doesn't provide official support for community extensions. This page\n  uses this package.\n- **`Hangfire.Pro.Redis`** is part of Hangfire Pro, which is distributed through a private\n  NuGet feed under paid subscriptions. See the\n  [Hangfire Pro documentation](https://docs.hangfire.io/en/latest/configuration/using-redis.html).\n\nThe two have different configuration APIs, so the code on this page is for the community\npackage."
    },
    {
      "id": "requirements",
      "title": "Requirements",
      "role": "content",
      "text": "The examples on this page were run with Hangfire.Core 1.8.25 and\nHangfire.Redis.StackExchange 1.12.0 on .NET 9 against Redis 7.2. Add the packages to your\nproject:\n\n[code example]\n\nYou also need a running Redis server. You can run one locally with\n[Redis Open Source](https://redis.io/docs/latest/operate/oss_and_stack/install/archive/install-redis),\nuse [Docker](https://hub.docker.com/_/redis), or connect to\n[Redis Cloud](https://redis.io/cloud/)."
    },
    {
      "id": "configure-the-storage-and-enqueue-jobs",
      "title": "Configure the storage and enqueue jobs",
      "role": "configuration",
      "text": "Call `UseRedisStorage` with a connection string, then use `BackgroundJob` to create jobs:\n\n[code example]\n\nA job is a method on a class. Use `AutomaticRetry` to control retries:\n\n[code example]"
    },
    {
      "id": "run-a-server",
      "title": "Run a server",
      "role": "content",
      "text": "A `BackgroundJobServer` fetches and runs jobs. You can host it in any .NET process:\n\n[code example]\n\n> [!NOTE]\n> Hangfire's scheduler moves delayed and retried jobs to the queue on a polling cycle. In\n> these runs the cycle was about 15 seconds. In a test, a job scheduled 3 seconds ahead ran about 12 seconds after it\n> was created, and a retry with a 1-second delay ran 12 and then 15 seconds later. Don't rely\n> on `Schedule` or the retry delay for second-level accuracy."
    },
    {
      "id": "what-hangfire-stores-in-redis",
      "title": "What Hangfire stores in Redis",
      "role": "content",
      "text": "Hangfire's keys start with the prefix `{hangfire}:`. You can change it with the `Prefix`\noption.\n\n| Key | Type | Contents |\n|-----|------|----------|\n| `{hangfire}:queue:<name>` | list | The IDs of jobs that are ready to run. |\n| `{hangfire}:queues` | set | The names of the queues. |\n| `{hangfire}:schedule` | sorted set | Delayed jobs, scored by run time. |\n| `{hangfire}:job:<id>` | hash | The job: `Type`, `Method`, `Arguments`, `ParameterTypes`, `State`, `CreatedAt`, and so on. |\n| `{hangfire}:queue:<name>:dequeued` | list | The IDs of jobs that a server has fetched and is running. |\n\nA succeeded job's hash expires after about 24 hours by default."
    },
    {
      "id": "what-happens-to-a-job-when-a-server-is-killed",
      "title": "What happens to a job when a server is killed",
      "role": "content",
      "text": "A server moves a job to the `dequeued` list when it fetches it, and removes it when the\njob finishes. If the server dies, the job stays there until it passes the\n`InvisibilityTimeout` of the storage options, and then another server fetches it again.\nThe default is 30 minutes. In a test with `InvisibilityTimeout` set to 10 seconds, a new\nserver ran the job 62 seconds after the first server was stopped with `kill -9`, so\nrecovery is not instant even with a short timeout.\n\nSet the option to longer than your longest job, or a healthy job will be fetched twice:\n\n[code example]\n\nWrite jobs so that running one twice is safe."
    },
    {
      "id": "more-information",
      "title": "More information",
      "role": "content",
      "text": "- [Hangfire documentation](https://docs.hangfire.io/)\n- [Hangfire.Redis.StackExchange on GitHub](https://github.com/marcoCasamento/Hangfire.Redis.StackExchange)"
    }
  ],
  "examples": [
    {
      "id": "requirements-ex0",
      "language": "bash",
      "code": "dotnet add package Hangfire.Core\ndotnet add package Hangfire.Redis.StackExchange",
      "section_id": "requirements"
    },
    {
      "id": "configure-the-storage-and-enqueue-jobs-ex0",
      "language": "csharp",
      "code": "using Hangfire;\nusing Hangfire.Redis.StackExchange;\n\nGlobalConfiguration.Configuration\n    .UseRedisStorage(\"<your-redis-host>:6379\");\n\n// Run as soon as a worker is free.\nBackgroundJob.Enqueue<Jobs>(j => j.Welcome(42, \"hello\"));\n\n// Run no sooner than 3 seconds from now.\nBackgroundJob.Schedule<Jobs>(j => j.Welcome(43, \"later\"), TimeSpan.FromSeconds(3));",
      "section_id": "configure-the-storage-and-enqueue-jobs"
    },
    {
      "id": "configure-the-storage-and-enqueue-jobs-ex1",
      "language": "csharp",
      "code": "public class Jobs\n{\n    public void Welcome(int userId, string greeting) =>\n        Console.WriteLine($\"Sending welcome to {userId}: {greeting}\");\n\n    [AutomaticRetry(Attempts = 3, DelaysInSeconds = new[] { 1 })]\n    public void Flaky()\n    {\n        // Throw an exception here to trigger a retry.\n    }\n}",
      "section_id": "configure-the-storage-and-enqueue-jobs"
    },
    {
      "id": "run-a-server-ex0",
      "language": "csharp",
      "code": "using var server = new BackgroundJobServer(new BackgroundJobServerOptions { WorkerCount = 2 });\nawait Task.Delay(Timeout.Infinite);",
      "section_id": "run-a-server"
    },
    {
      "id": "what-happens-to-a-job-when-a-server-is-killed-ex0",
      "language": "csharp",
      "code": "GlobalConfiguration.Configuration.UseRedisStorage(\"<your-redis-host>:6379\",\n    new RedisStorageOptions { InvisibilityTimeout = TimeSpan.FromMinutes(30) });",
      "section_id": "what-happens-to-a-job-when-a-server-is-killed"
    }
  ]
}
