StackExchange.Redis client best practices
1. Enabling server GC can optimize the client and provide better performance and throughput.
2. Set AbortOnConnectFail to false, when AbortOnConnectFail is false, the StackExchange.Redis will reconnect automatically.
3. Reuse the ConnectionMultiplexer, do not create a new one for each request. The Lazy<ConnectionMultiplexer> pattern is recommended.
4. Redis works best with smaller values, so consider chopping up bigger data into multiple keys. In this Redis discussion, 100KB is considered large.
5. Configure your ThreadPool settings to avoid timeouts.
6. Use at least the default connectTimeout of 5 seconds.
7. Be aware of the performance costs associated with different operations you are running. For instance, the KEYS command is an O(n) operation and should be avoided.
Performance testing
redis-benchmakr.exe
examples:
private static readonly Lazy<ConnectionMultiplexer> _lazy = new Lazy<ConnectionMultiplexer>(()=>{
string connectionString = ConfigurationManager.AppSettings["RedisConnection"].ToString();
return ConnectionMultiplexer.Connect(connectionString);
});
public static ConnectionMultiplexer Connection => _lazy.Value;