|
| 1 | +--- |
| 2 | +categories: |
| 3 | +- docs |
| 4 | +- develop |
| 5 | +- stack |
| 6 | +- oss |
| 7 | +- rs |
| 8 | +- rc |
| 9 | +- oss |
| 10 | +- kubernetes |
| 11 | +- clients |
| 12 | +description: Get your NRedisStack app ready for production |
| 13 | +linkTitle: Production usage |
| 14 | +title: Production usage |
| 15 | +weight: 70 |
| 16 | +--- |
| 17 | + |
| 18 | +This guide offers recommendations to get the best reliability and |
| 19 | +performance in your production environment. |
| 20 | + |
| 21 | +## Checklist |
| 22 | + |
| 23 | +Each item in the checklist below links to the section |
| 24 | +for a recommendation. Use the checklist icons to record your |
| 25 | +progress in implementing the recommendations. |
| 26 | + |
| 27 | +{{< checklist "dotnetprodlist" >}} |
| 28 | + {{< checklist-item "#event-handling" >}}Event handling{{< /checklist-item >}} |
| 29 | + {{< checklist-item "#timeouts" >}}Timeouts{{< /checklist-item >}} |
| 30 | + {{< checklist-item "#exception-handling" >}}Exception handling{{< /checklist-item >}} |
| 31 | +{{< /checklist >}} |
| 32 | + |
| 33 | +## Recommendations |
| 34 | + |
| 35 | +The sections below offer recommendations for your production environment. Some |
| 36 | +of them may not apply to your particular use case. |
| 37 | + |
| 38 | +### Event handling |
| 39 | + |
| 40 | +The `ConnectionMultiplexer` class publishes several different types of |
| 41 | +[events](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/events/) |
| 42 | +for situations such as configuration changes and connection failures. |
| 43 | +Use these events to record server activity in a log, which you can then use |
| 44 | +to monitor performance and diagnose problems when they occur. |
| 45 | +See |
| 46 | +the StackExchange.Redis |
| 47 | +[Events](https://stackexchange.github.io/StackExchange.Redis/Events) |
| 48 | +page for the full list of events. |
| 49 | + |
| 50 | +#### Server notification events |
| 51 | + |
| 52 | +Some servers (such as Azure Cache for Redis) send notification events shortly |
| 53 | +before scheduled maintenance is due to happen. You can use code like the |
| 54 | +following to respond to these events (see the |
| 55 | +[StackExchange.Redis](https://stackexchange.github.io/StackExchange.Redis/ServerMaintenanceEvent) |
| 56 | +docs for the full list of supported events). For example, you could |
| 57 | +inform users who try to connect that service is temporarily unavailable |
| 58 | +rather than letting them run into errors. |
| 59 | + |
| 60 | +```cs |
| 61 | +using NRedisStack; |
| 62 | +using StackExchange.Redis; |
| 63 | + |
| 64 | +ConnectionMultiplexer muxer = ConnectionMultiplexer.Connect("localhost:6379"); |
| 65 | + |
| 66 | +muxer.ServerMaintenanceEvent += (object sender, ServerMaintenanceEvent e) => { |
| 67 | + // Identify the event and respond to it here. |
| 68 | + Console.WriteLine($"Maintenance event: {e.RawMessage}"); |
| 69 | +}; |
| 70 | +``` |
| 71 | + |
| 72 | +### Timeouts |
| 73 | + |
| 74 | +If a network or server error occurs while your code is opening a |
| 75 | +connection or issuing a command, it can end up hanging indefinitely. |
| 76 | +To prevent this, `NRedisStack` sets timeouts for socket |
| 77 | +reads and writes and for opening connections. |
| 78 | + |
| 79 | +By default, the timeout is five seconds for all operations, but |
| 80 | +you can set the time (in milliseconds) separately for connections |
| 81 | +and commands using the `ConnectTimeout`, `SyncTimeout`, and |
| 82 | +`AsyncTimeout` configuration options: |
| 83 | + |
| 84 | +```cs |
| 85 | +var muxer = ConnectionMultiplexer.Connect(new ConfigurationOptions { |
| 86 | + ConnectTimeout = 1000, // 1 second timeout for connections. |
| 87 | + SyncTimeout = 2000, // 2 seconds for synchronous commands. |
| 88 | + AsyncTimeout = 3000 // 3 seconds for asynchronous commands. |
| 89 | + . |
| 90 | + . |
| 91 | +}); |
| 92 | + |
| 93 | +var db = muxer.GetDatabase(); |
| 94 | +``` |
| 95 | + |
| 96 | +The default timeouts are a good starting point, but you may be able |
| 97 | +to improve performance by adjusting the values to suit your use case. |
| 98 | + |
| 99 | +### Exception handling |
| 100 | + |
| 101 | +Redis handles many errors using return values from commands, but there |
| 102 | +are also situations where exceptions can be thrown. In production code, |
| 103 | +you should handle exceptions as they occur. The list below describes some |
| 104 | +the most common Redis exceptions: |
| 105 | + |
| 106 | +- `RedisConnectionException`: Thrown when a connection attempt fails. |
| 107 | +- `RedisTimeoutException`: Thrown when a command times out. |
| 108 | +- `RedisCommandException`: Thrown when you issue an invalid command. |
| 109 | +- `RedisServerException`: Thrown when you attempt an invalid operation |
| 110 | + (for example, trying to access a |
| 111 | + [stream entry]({{< relref "/develop/data-types/streams#entry-ids" >}}) |
| 112 | + using an invalid ID). |
0 commit comments