Two methods to resolve StackExchange.Redis.RedisTimeoutException in C#

StackExchange.Redis.RedisTimeoutException: Timeout performing SETEX (5000ms), next: SETEX AutoOps:PodLauncher:2109:LWSY:75:85001:IsMainServerRunning, inst: 0, qu: 0, qs: 1, aw: False, rs: ReadAsync, ws: Idle, in: 0, in-pipe: 0, out-pipe: 0, serverEndpoint: r-bp1je5yrr7ctdzwhmk.redis.rds.aliyuncs.com:6379, mc: 1/1/0, mgr: 10 of 10 available, clientName: 2109-85001-0, IOCP: (Busy=1,Free=999,Min=4,Max=1000), WORKER: (Busy=2,Free=32765,Min=32,Max=32767), v: 2.1.28.64774 (Please take a look at this article for some common client-side issues that can cause timeouts: https://stackexchange.github.io/StackExchange.Redis/Timeouts)
   at StackExchange.Redis.ConnectionMultiplexer.ExecuteSyncImpl[T](Message message, ResultProcessor`1 processor, ServerEndPoint server) in /_/src/StackExchange.Redis/ConnectionMultiplexer.cs:line 2616
   at StackExchange.Redis.RedisBase.ExecuteSync[T](Message message, ResultProcessor`1 processor, ServerEndPoint server) in /_/src/StackExchange.Redis/RedisBase.cs:line 54
   at StackExchange.Redis.RedisDatabase.StringSet(RedisKey key, RedisValue value, Nullable`1 expiry, When when, CommandFlags flags) in /_/src/StackExchange.Redis/RedisDatabase.cs:line 2500
   at Q1.Foundation.RepoLibs.RedisLib.StringSet(RedisKey key, RedisValue value, Nullable`1 expiry, When when, CommandFlags flags)
   at GameSvrLauncher.Lib.RedisCache.<>c__DisplayClass27_0.<set_IsMainServerRunning>b__0(ILogger logger)
   at GameSvrLauncher.Lib.Utils.RetryAction(Action`1 func, String actionDesc, ILogger logger, Int32 executeTimes, Int32 sleepTimes, Action`2 warnCallback, Action`2 faultCallback)

Method #1:

Set minimal worker threads:

Way #1: By environment variables

Before .net 6:

COMPlus_ThreadPool_ForceMinWorkerThreads

.net 6

DOTNET_ThreadPool_ForceMinWorkerThreads

NOTES: The value is in hexadecimal format

ref docs:
https://docs.microsoft.com/en-us/dotnet/core/run-time-config/threading
https://github.com/dotnet/runtime/issues/11774

Way #2:By System.Threading.ThreadPool.SetMinThreads methods

Method #2:

Set sync timeout for redis connection.

"172.16.127.229:6379,defaultDatabase=1,syncTimeout=10000"

the default sync timeout is 5000 ms.
ref docs:
https://stackexchange.github.io/StackExchange.Redis/Configuration.html

Leave a Comment