StackExchange.Redis icon indicating copy to clipboard operation
StackExchange.Redis copied to clipboard

OutOfMemoryException on ConnectionMultiplexer.Connect

Open razzemans opened this issue 3 years ago • 4 comments

See the following stacktrace:

System.OutOfMemoryException:
   at System.Threading.Thread.StartCore (System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at System.Threading.Thread.Start (System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at System.Threading.Thread.Start (System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at Pipelines.Sockets.Unofficial.DedicatedThreadPoolPipeScheduler.StartWorker (Pipelines.Sockets.Unofficial, Version=1.0.0.0, Culture=neutral, PublicKeyToken=42ea0a778e13fbe2: /_/src/Pipelines.Sockets.Unofficial/DedicatedThreadPoolPipeScheduler.cs:112)
   at Pipelines.Sockets.Unofficial.DedicatedThreadPoolPipeScheduler..ctor (Pipelines.Sockets.Unofficial, Version=1.0.0.0, Culture=neutral, PublicKeyToken=42ea0a778e13fbe2: /_/src/Pipelines.Sockets.Unofficial/DedicatedThreadPoolPipeScheduler.cs:74)
   at StackExchange.Redis.SocketManager..ctor (StackExchange.Redis, Version=2.0.0.0, Culture=neutral, PublicKeyToken=c219ff1ca8c2ce46: /_/src/StackExchange.Redis/SocketManager.cs:98)
   at StackExchange.Redis.SocketManager..ctor (StackExchange.Redis, Version=2.0.0.0, Culture=neutral, PublicKeyToken=c219ff1ca8c2ce46: /_/src/StackExchange.Redis/SocketManager.cs:44)
   at StackExchange.Redis.SocketManager.get_Shared (StackExchange.Redis, Version=2.0.0.0, Culture=neutral, PublicKeyToken=c219ff1ca8c2ce46: /_/src/StackExchange.Redis/SocketManager.cs:132)
   at StackExchange.Redis.ConnectionMultiplexer.GetDefaultSocketManager (StackExchange.Redis, Version=2.0.0.0, Culture=neutral, PublicKeyToken=c219ff1ca8c2ce46: /_/src/StackExchange.Redis/ConnectionMultiplexer.ReaderWriter.cs:32)
   at StackExchange.Redis.ConnectionMultiplexer.OnCreateReaderWriter (StackExchange.Redis, Version=2.0.0.0, Culture=neutral, PublicKeyToken=c219ff1ca8c2ce46: /_/src/StackExchange.Redis/ConnectionMultiplexer.ReaderWriter.cs:12)
   at StackExchange.Redis.ConnectionMultiplexer..ctor (StackExchange.Redis, Version=2.0.0.0, Culture=neutral, PublicKeyToken=c219ff1ca8c2ce46: /_/src/StackExchange.Redis/ConnectionMultiplexer.cs:146)
   at StackExchange.Redis.ConnectionMultiplexer.CreateMultiplexer (StackExchange.Redis, Version=2.0.0.0, Culture=neutral, PublicKeyToken=c219ff1ca8c2ce46: /_/src/StackExchange.Redis/ConnectionMultiplexer.cs:159)
   at StackExchange.Redis.ConnectionMultiplexer.ConnectImpl (StackExchange.Redis, Version=2.0.0.0, Culture=neutral, PublicKeyToken=c219ff1ca8c2ce46: /_/src/StackExchange.Redis/ConnectionMultiplexer.cs:689)
   at StackExchange.Redis.ConnectionMultiplexer.Connect (StackExchange.Redis, Version=2.0.0.0, Culture=neutral, PublicKeyToken=c219ff1ca8c2ce46: /_/src/StackExchange.Redis/ConnectionMultiplexer.cs:673)
   at StackExchange.Redis.ConnectionMultiplexer.Connect (StackExchange.Redis, Version=2.0.0.0, Culture=neutral, PublicKeyToken=c219ff1ca8c2ce46: /_/src/StackExchange.Redis/ConnectionMultiplexer.cs:651)

We're using 2.6.96. Any idea what causes this or how to debug this? We've been having major issues on our production environments and I have a feeling this is the culprit. Wondering if I should upgrade to 2.6.104 or downgrade to a version we were running earlier. Thanks!

razzemans avatar Apr 11 '23 15:04 razzemans

To debug this, you'll want to take a memory dump. We don't use that much memory overall, so if you're hitting exhaustion it's usually because you're creating a lot of something that's not apparent...could even be crazy amounts of ConnectionMultiplexers.

If it helps, remember that out of memory is just hitting the limit, this is probably happening and discernable for cause well before the limit, just observe as memory is growing and take a dump before it reaches full OOM. You can open memory dumps in Visual Studio to see what objects you have a ton of to reason about the cause.

NickCraver avatar Apr 13 '23 13:04 NickCraver

I actually did a memory dump and I had trouble comprehending it. It shows a low amount of threads in the parallel stacks windows, yet it complains the system was out of threads? There ARE a huge amount of tasks and they all eventually hit Redis. Doesn't seem to be blocking though.

razzemans avatar Apr 13 '23 15:04 razzemans

Ok upon further investigation, we did a refactoring of our codebase where our CMS content was previously stored in the IMemoryCache and now it is (also) stored in Redis. Can we just have too many calls to Redis? It would explain the many tasks waiting on Redis which previously would not be the case. The calls are non blocking (I actually found one endpoint in our codebase called every 20 seconds which would do a non-async set to redis, but I find that hard to be the absolute culprit).

To be clear: this is on our production application with very high traffic on a peak moment (100.000s of users, thousands of requests per second). We eventually ran on over 20 P3v2 instances but still our application was utterly, completely down, while CPU, Memory, DB and Redis itself didn't show troubling numbers.

razzemans avatar Apr 13 '23 15:04 razzemans

For anyone interested, it was a combination of different things that led to these issues, mainly doing some non-async ToList() calls on EF Core queries that was blocking some threads which we only experienced during extremely high load.

Will say that we reverted to an older StackExchange package since 2.6.96 seems to be much worse performance wise than 2.6.90.

razzemans avatar Apr 21 '23 12:04 razzemans