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

RedisConnectionException at each first Connect(), while redis-py and hiredis work fine

Open tw-bert opened this issue 12 years ago • 103 comments

Hi Marc,

Version 1.0.289.0 (nuget, .net 4.0).

When I leave my workstation alone for five minutes, after calling Connect() I get a RedisConnectionException the first time. Immediately retrying does work with no delay at a 90% success rate. No info is written to the supplied TextWriter.

var oCli = ConnectionMultiplexer.Connect("srv-flux-01:14144", log);

Complete exception message:

An unhandled exception of type 'StackExchange.Redis.RedisConnectionException' occurred in StackExchange.Redis.dll
Additional information: It was not possible to connect to the redis server(s); to create a disconnected multiplexer, disable AbortOnConnectFail

I connect from Win7 Pro X64 to Redis 2.8.9 (x64) on a Debian machine on our LAN (same subnet). No hardware firewalls in between, only Windows firewall. Redis connections from redis-py or RedisDesktopManager(which uses hiredis) never fail, and always connect within milliseconds.

I use a simple Windows Forms project, VS2013 Express, for testing. I use (and need) the .NET Framework 4 Client Profile.

Any ideas?

Kind regards, TW

tw-bert avatar May 09 '14 23:05 tw-bert

It's not really the same case but the resulting behaviour is the same if once started and connected, you shut down the redis server.

You get a (normal in this case) : "No connection is available to service this operation: EXEC"

Maybe this can help you find what's going wrong in case where a connection IS available ?

blemasle avatar Aug 06 '14 14:08 blemasle

After further testing if we call ConnectionMultiplexer.Connect(...) and this fails with a time out (in our case we turned Redis off first) the method throws an exception and fails. We then turn Redis back on and the connect call is made again it still continues to fail despite the fact that it should be able to connect to Redis (which we verified using Redis Desktop Manager). Our work around, that we think works, is to add "abortConnect=false" in the connection configuration which then creates a disconnected multiplexer and once we turn Redis back on this can connect just fine without issue.

@blemasle Maybe this type of work around would work for you as well?

This issue seems to only happen if the very first connect fails. We did encounter other issues with the first scenario as well when we attempted to dispose of the text logger we passed in to the Connect(...) call.

*Note - we did our testing using the latest version of StackExchange.Redis 1.0.322; we only have one Redis server (no shards or fail overs); and this was done in a Web API application using a singleton pattern to create the multiplexer.

Raven87 avatar Aug 06 '14 17:08 Raven87

@Raven87 The "abortConnect=false" configuration setting did not solve the problem for me.

AngCaruso avatar Aug 06 '14 20:08 AngCaruso

Yes, the problem also persists for us. We do have an ugly but (so far) reliable workaround: use BookSleeve as well as SE.Redis. Make a dummy connection using BookSleeve, and immediately after that, make a connection with SE.Redis. BookSleeve seems to initialize the routing in a proper way (see my earlier comments in May). Disconnect and dispose the BookSleeve connection when you are connected through SE.Redis.

Could be of help to someone, cheers

tw-bert avatar Aug 06 '14 20:08 tw-bert

I can reproduce "No connection is available to service this operation: EXEC" If I break into the debugger and pause execution for some time (say 1 minute) and then resume the website running; all requests fail with this immediately and doesn't seem to figure out the connection is okay.

Plasma avatar Aug 07 '14 05:08 Plasma

@Plasma I can't reproduce it using your provided steps. I'll try the abortConnect=false trick from @Raven87 but I do not believe in it.

The disconnected multiplexer was a good match with old version where some logic were done on first initialization. Since @mgravell made some changes to that logic as he said (moving it deeper in the lib if I remember correctly), we're seing the issue on each operation, stating that "no connection is available".

So I can't see how the disconnectedMultiplexer could solve this :)

blemasle avatar Aug 07 '14 08:08 blemasle

I notice that the connection check is done via a Task; perhaps thread starvation causes the check to timeout?

Sent from my iPhone

On 7 Aug 2014, at 6:40 pm, blemasle [email protected] wrote:

@Plasma I can't reproduce it using your provided steps. I'll try the abortConnect=false trick from @Raven87 but I do not believe in it.

The disconnected multiplexer was a good match with old version where some logic were done on first initialization. Since @mgravell made some changes to that logic as he said (moving it deeper in the lib if I remember correctly), we're seing the issue on each operation, stating that "no connection is available".

So I can't see how the disconnectedMultiplexer could solve this :)

— Reply to this email directly or view it on GitHub.

Plasma avatar Aug 07 '14 09:08 Plasma

It is indeed possible that it is related to thread starvation, especially at the start of an application due to the way that the thread-pool ramps (I remember sitting down with Lucian for about an hour just talking about the way this works). I am actively investigating.

On 7 August 2014 10:40, Andrew Armstrong [email protected] wrote:

I notice that the connection check is done via a Task; perhaps thread starvation causes the check to timeout?

Sent from my iPhone

On 7 Aug 2014, at 6:40 pm, blemasle [email protected] wrote:

@Plasma I can't reproduce it using your provided steps. I'll try the abortConnect=false trick from @Raven87 but I do not believe in it.

The disconnected multiplexer was a good match with old version where some logic were done on first initialization. Since @mgravell made some changes to that logic as he said (moving it deeper in the lib if I remember correctly), we're seing the issue on each operation, stating that "no connection is available".

So I can't see how the disconnectedMultiplexer could solve this :)

— Reply to this email directly or view it on GitHub.

— Reply to this email directly or view it on GitHub https://github.com/StackExchange/StackExchange.Redis/issues/42#issuecomment-51450930 .

Regards,

Marc

mgravell avatar Aug 07 '14 09:08 mgravell

This is a test that shows TIMEOUT happening, although I wonder if its the way async/await works in a Console app. https://gist.github.com/Plasma/62f9e3a2010ed1a6e486

Plasma avatar Aug 16 '14 05:08 Plasma

Simillar problems here, redis going up and down, connections aren't being made. The "abortConnect=false" doesn't help.

turowicz avatar Sep 04 '14 09:09 turowicz

I am able to repro this error on my machine now. If I turn of SSL it works just fine... So port 6379 works, but not 6380... I see the same when connecting using a Redis client.

noocyte avatar Sep 10 '14 06:09 noocyte

I am also facing same issue with ASP.net application on Windows 2008 server, but its working fine on a Windows 7 local dev pc.

ConnectionMultiplexer.Connect is throwing this error, on button click event. I have to reset IIS to make it work again. its working for one or two button click event and after that constistently getting this error and IIS reset is required to make it work again and fails again.

StackExchange.Redis.RedisConnectionException: It was not possible to connect to the redis server(s); to create a disconnected multiplexer, disable AbortOnConnectFail

Let me know when this issue will be fixed.

Vijay27anand avatar Sep 17 '14 19:09 Vijay27anand

We were experiencing the same issue, and what seemed to fix it was using a host name instead of an IP address.

In our case it was working fine on dev machines, but sporadically failed on the server.

cilliemalan avatar Sep 25 '14 13:09 cilliemalan

Closing the connection and disposing it before creating new one fixed it for us.

  static readonly Object Lock = new Object();
  static ConnectionMultiplexer _connection;

  static ConnectionMultiplexer Connection
  {
      get
      {
          lock (Lock)
          {
              if (_connection == null || !_connection.IsConnected)
              {
                  //kill old connection
                  if (_connection != null)
                  {
                      _connection.Close(false);
                      _connection.Dispose();
                      _connection = null;
                  }

                  _connection = ConnectionMultiplexer.Connect(_redisConnection);
              }
              return _connection;
          }
      }
  }

turowicz avatar Sep 25 '14 13:09 turowicz

hmmm... sounds like DNS lookup might be at least related; that gives me something to look into, at least - I'll boost the error specificity in that area to see if that is indeed the case

On 25 September 2014 14:20, Wojtek Turowicz [email protected] wrote:

Closing the connection and disposing it before creating new one fixed it for us.

static ConnectionMultiplexer _connection;

static ConnectionMultiplexer Connection { get { lock (Lock) { if (_connection == null || !_connection.IsConnected) { //kill old connection if (_connection != null) { _connection.Close(false); _connection.Dispose(); _connection = null; }

              _connection = ConnectionMultiplexer.Connect(_redisConnection);
          }
          return _connection;
      }
  }

}

— Reply to this email directly or view it on GitHub https://github.com/StackExchange/StackExchange.Redis/issues/42#issuecomment-56817167 .

Regards,

Marc

mgravell avatar Sep 25 '14 13:09 mgravell

Turowicz If we have connection as static singleton as mention in the post, for a enterprise application in one IIS worker process we will have only one connection right, so is it good idea to have it as static connection or how to multiple connection from one worker process for concurrency.

Vijay27anand avatar Sep 25 '14 18:09 Vijay27anand

You need only one connection multiplexer. The Connect(...) method returns a new instance of it. This is how I understand the documentation provided in Readme.md file.

turowicz avatar Sep 26 '14 08:09 turowicz

Yes, multiplexers should be aggressively shared and re-used; they should not be created per-call. Any mechanism that supports that is fine; the simplest of which is a static field. But there are lots of ways of implementing it.

On 26 September 2014 09:26, Wojtek Turowicz [email protected] wrote:

You need only one connection multiplexer. The Connect(...) method returns a new instance of it. This is how I understand the documentation provided in Readme.md file.

— Reply to this email directly or view it on GitHub https://github.com/StackExchange/StackExchange.Redis/issues/42#issuecomment-56933345 .

Regards,

Marc

mgravell avatar Sep 26 '14 08:09 mgravell

I have a requirement to have Redis server running in different boxes and application code in different boxes. Based on the session id which is a number we will to route to Redis server 1 or 2 or 3. We are using the static connection multiplexer to reuse the connection but every time we are hitting the first initialized connection since its static. Is there a way that we can have multiple connections to different Redis server and reuse it and based on our session id hash logic identify which Redis server to hit and get the value.

Vijay27anand avatar Oct 06 '14 19:10 Vijay27anand

If you have "n" multiplexers and code that fetches the multiplexer you want based on the session: sure. Just: get the appropriate multiplexer and have fun.

Note that when cluster is RTM you can achieve something similar using hash tags in the key, meaning: everything "/foo/{sessionid}/bar/..." is guaranteed to be in the same hash bucket, so on the same node. SE.Redis does not currently include non-cluster client-based sharding, simply because I don't have the scenarios to properly design, implement, and test such. Most of the actual shard code exists though, because of cluster. On 6 Oct 2014 20:25, "Vijay27anand" [email protected] wrote:

I have a requirement to have Redis server running in different boxes and application code in different boxes. Based on the session id which is a number we will to route to Redis server 1 or 2 or 3. We are using the static connection multiplexer to reuse the connection but every time we are hitting the first initialized connection since its static. Is there a way that we can have multiple connections to different Redis server and reuse it and based on our session id hash logic identify which Redis server to hit and get the value.

— Reply to this email directly or view it on GitHub https://github.com/StackExchange/StackExchange.Redis/issues/42#issuecomment-58078547 .

mgravell avatar Oct 06 '14 21:10 mgravell