{
  "schema_version": 2,
  "id": "develop/clients/dotnet/produsage",
  "title": "Production usage",
  "url": "https://redis.io/docs/latest/develop/clients/dotnet/produsage/",
  "summary": "Get your `StackExchange.Redis` app ready for production",
  "tags": [
    "docs",
    "develop",
    "stack",
    "oss",
    "rs",
    "rc",
    "oss",
    "kubernetes",
    "clients"
  ],
  "last_updated": "2026-09-18T16:06:01+01:00",
  "page_type": "content",
  "content_hash": "d9e3f08482f43bbf46c27905f188766ad7b091283f6d97eb031d0a1843e509d1",
  "sections": [
    {
      "id": "overview",
      "title": "Overview",
      "role": "overview",
      "text": "This guide offers recommendations to get the best reliability and\nperformance in your production environment."
    },
    {
      "id": "checklist",
      "title": "Checklist",
      "role": "content",
      "text": "Each item in the checklist below links to the section\nfor a recommendation. Use the checklist icons to record your\nprogress in implementing the recommendations.\n\n[code example]"
    },
    {
      "id": "recommendations",
      "title": "Recommendations",
      "role": "content",
      "text": "The sections below offer recommendations for your production environment. Some\nof them may not apply to your particular use case."
    },
    {
      "id": "event-handling",
      "title": "Event handling",
      "role": "content",
      "text": "The `ConnectionMultiplexer` class publishes several different types of\n[events](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/events/)\nfor situations such as configuration changes and connection failures.\nUse these events to record server activity in a log, which you can then use\nto monitor performance and diagnose problems when they occur.\nSee\nthe StackExchange.Redis\n[Events](https://seredis.dev/Events.html)\npage for the full list of events.\n\n#### Server notification events\n\nSome servers (such as Azure Cache for Redis) send notification events shortly\nbefore scheduled maintenance is due to happen. You can use code like the\nfollowing to respond to these events (see the \n[StackExchange.Redis](https://seredis.dev/ServerMaintenanceEvent.html)\ndocs for the full list of supported events). For example, you could\ninform users who try to connect that service is temporarily unavailable\nrather than letting them run into errors.\n\nThe same event also carries\n[Smart client handoffs](https://redis.io/docs/latest/develop/clients/dotnet/connect#connect-using-smart-client-handoffs-sch)\n(SCH) notifications, which are a different, Redis Cloud- and Redis Software-specific\nmechanism. Check the runtime type of the event arguments to tell the two apart.\n\n[code example]"
    },
    {
      "id": "timeouts",
      "title": "Timeouts",
      "role": "content",
      "text": "If a network or server error occurs while your code is opening a\nconnection or issuing a command, it can end up hanging indefinitely.\nTo prevent this, `StackExchange.Redis` sets timeouts for socket\nreads and writes and for opening connections.\n\nBy default, the timeout is five seconds for `ConnectTimeout` and `SyncTimeout`,\nand `AsyncTimeout` uses the same value as `SyncTimeout`. You can set the time\n(in milliseconds) separately for connections and commands using these\nconfiguration options:\n\n[code example]\n\nThe default timeouts are a good starting point, but cloud applications may need\nlonger timeouts to handle transient network or server delays. Shorter timeouts\ndo not make commands run faster; they only fail sooner. Tune these values to\nimprove reliability and resilience for your workload."
    },
    {
      "id": "exception-handling",
      "title": "Exception handling",
      "role": "errors",
      "text": "Redis handles many errors using return values from commands, but there\nare also situations where exceptions can be thrown. In production code,\nyou should handle exceptions as they occur. The list below describes some\nthe most common Redis exceptions:\n\n- `RedisConnectionException`: Thrown when a connection attempt fails.\n- `RedisTimeoutException`: Thrown when a command times out.\n- `RedisCommandException`: Thrown when you issue an invalid command.\n- `RedisServerException`: Thrown when you attempt an invalid operation\n  (for example, trying to access a\n  [stream entry](https://redis.io/docs/latest/develop/data-types/streams#entry-ids)\n  using an invalid ID).\n\nSee [Error handling](https://redis.io/docs/latest/develop/clients/dotnet/error-handling)\nfor more information on handling exceptions."
    },
    {
      "id": "retries",
      "title": "Retries",
      "role": "content",
      "text": "During the initial `ConnectionMultiplexer.Connect()` call, `StackExchange.Redis`\nwill repeat failed connection attempts. By default, it will make three attempts,\nbut you can configure the number of retries using the `ConnectRetry`\nconfiguration option.\n\nYou should also consider the `AbortOnConnectFail` option. If this is `true`,\n`Connect()` fails when it can't connect after the configured number of attempts. If it is\n`false`, `Connect()` can return a disconnected multiplexer and keep trying to\nconnect in the background, which is helpful in cloud environments where\ndependencies may come online out of sequence. The default is `true`, except for\nAzure endpoints, where it defaults to `false`.\n\n[code example]\n\nAfter the initial `Connect()` call is successful, `StackExchange.Redis` will\nautomatically attempt to reconnect if the connection is lost. You can\nspecify a reconnection strategy with the `ReconnectRetryPolicy` configuration\noption. `StackExchange.Redis` provides two built-in classes that implement\nreconnection strategies:\n\n- `ExponentialRetry`: (Default) Uses an\n    [exponential backoff](https://en.wikipedia.org/wiki/Exponential_backoff)\n    strategy, where you specify an increment to the delay between successive\n    attempts and, optionally, a maximum delay, both in milliseconds.\n-   `LinearRetry`: Uses a linear backoff strategy with a fixed delay between\n    attempts, in milliseconds.\n\nThe example below shows how to use the `ExponentialRetry` class:\n\n[code example]\n\nYou can also implement your own custom reconnect policy by creating a class\nthat implements the `IReconnectRetryPolicy` interface.\n\n`StackExchange.Redis` doesn't provide an automated retry mechanism for commands.\nFor command retries, use a bounded retry strategy in your application code and\nretry only transient failures such as `RedisConnectionException` and\n`RedisTimeoutException`. Many .NET apps use a resilience library such as\n[Polly](https://www.pollydocs.org/strategies/retry.html) to manage retry delays,\nbackoff, jitter, and retry limits instead of writing retry loops by hand."
    }
  ],
  "examples": [
    {
      "id": "checklist-ex0",
      "language": "checklist {id=\"dotnetprodlist\"}",
      "code": "- [ ] [Event handling](#event-handling)\n- [ ] [Timeouts](#timeouts)\n- [ ] [Exception handling](#exception-handling)\n- [ ] [Retries](#retries)",
      "section_id": "checklist"
    },
    {
      "id": "event-handling-ex0",
      "language": "cs",
      "code": "using StackExchange.Redis;\n\nConnectionMultiplexer muxer = ConnectionMultiplexer.Connect(\"localhost:6379\");\n\nmuxer.ServerMaintenanceEvent += (object sender, ServerMaintenanceEvent e) => {\n    // Identify the event and respond to it here.\n    Console.WriteLine($\"Maintenance event: {e.RawMessage}\");\n};",
      "section_id": "event-handling"
    },
    {
      "id": "timeouts-ex0",
      "language": "cs",
      "code": "var muxer = ConnectionMultiplexer.Connect(new ConfigurationOptions {\n    ConnectTimeout = 10000,  // 10 seconds for connections.\n    SyncTimeout = 10000,     // 10 seconds for synchronous commands.\n    AsyncTimeout = 10000     // 10 seconds for asynchronous commands.\n        .\n        .\n});\n\nvar db = muxer.GetDatabase();",
      "section_id": "timeouts"
    },
    {
      "id": "retries-ex0",
      "language": "cs",
      "code": "var muxer = ConnectionMultiplexer.Connect(new ConfigurationOptions {\n    ConnectRetry = 5,  // Retry up to five times.\n    AbortOnConnectFail = false,\n        .\n        .\n});",
      "section_id": "retries"
    },
    {
      "id": "retries-ex1",
      "language": "cs",
      "code": "var muxer = ConnectionMultiplexer.Connect(new ConfigurationOptions {\n    // 500ms increment per attempt, max 2000ms.\n    ReconnectRetryPolicy = new ExponentialRetry(500, 2000),  \n        .\n        .\n});",
      "section_id": "retries"
    }
  ]
}
