StackExchange / StackExchange.Redis

General purpose redis client
https://stackexchange.github.io/StackExchange.Redis/
Other
5.91k stars 1.51k forks source link

Azure Redis Cache connection from .net 4.6.1 #2495

Closed sasankjonna closed 1 year ago

sasankjonna commented 1 year ago

I am trying to connect Azure Redis cache from a simple .net 4.6.1 console app and getting the below when I try do StringSet

StackExchange.Redis.RedisConnectionException: 'The message timed out in the backlog attempting to send because no connection became available (5000ms) - Last Connection Exception: It was not possible to connect to the redis server(s). ConnectTimeout, command=SET, timeout: 5000, inst: 0, qu: 1, qs: 0, aw: False, bw: SpinningDown, rs: NotStarted, ws: Initializing, in: 0, last-in: 0, cur-in: 0, sync-ops: 1, async-ops: 1, serverEndpoint: sjrediscache.redis.cache.windows.net:6380, conn-sec: n/a, aoc: 0, mc: 1/1/0, mgr: 10 of 10 available, clientName: 5CG0251G81(SE.Redis-v2.6.116.40240), IOCP: (Busy=0,Free=1000,Min=8,Max=1000), WORKER: (Busy=0,Free=2047,Min=8,Max=2047), v: 2.6.116.40240 (Please take a look at this article for some common client-side issues that can cause timeouts: https://stackexchange.github.io/StackExchange.Redis/Timeouts)'

Here is my code, any suggestions of what can be done?

System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12; // Connect to Azure Redis Cache string redisConnectionString = "sjrediscache.redis.cache.windows.net:6380,password=**=,ssl=True,abortConnect=False,sslProtocols=tls12"; redisConnection = ConnectionMultiplexer.Connect(redisConnectionString);

    // Perform operations on Redis Cache
    IDatabase db = redisConnection.GetDatabase();
    string cacheKey = "myKey";
    string cacheValue = "Hello, Redis!";

    // Set a value in the cache
    db.StringSet(cacheKey, cacheValue);
mgravell commented 1 year ago

It never connected. If you take the abortConnect=false away, it might give you an exception that says what failed. Alternatively, pass a something (Console.Out or a StringWriter, typically) into the optional "log" parameter when connecting, and see what it says in the verbose log

sasankjonna commented 1 year ago

It still says the same

The message timed out in the backlog attempting to send because no connection became available (5000ms) - Last Connection Exception: It was not possible to connect to the redis server(s). ConnectTimeout, command=SET, timeout: 5000, inst: 0, qu: 1, qs: 0, aw: False, bw: SpinningDown, rs: NotStarted, ws: Idle, in: 0, last-in: 0, cur-in: 0, sync-ops: 1, async-ops: 1, serverEndpoint: sjrediscache.redis.cache.windows.net:6380, conn-sec: n/a, aoc: 0, mc: 1/1/0, mgr: 10 of 10 available, clientName: 5CG0251G81(SE.Redis-v2.6.116.40240), IOCP: (Busy=0,Free=1000,Min=8,Max=1000), WORKER: (Busy=0,Free=2047,Min=8,Max=2047), v: 2.6.116.40240 (Please take a look at this article for some common client-side issues that can cause timeouts: https://stackexchange.github.io/StackExchange.Redis/Timeouts)'

philon-msft commented 1 year ago

If you try explicitly setting abortConnect=true, that may give you a better error message. And if you pass a TextWriter of some kind into the Connect() call, it will receive detailed logs on why the connection is failing.

sasankjonna commented 1 year ago

no luck, plz advice

image

sasankjonna commented 1 year ago

are there any firewall settings I need to be aware of?

NickCraver commented 1 year ago

@sasankjonna As Philo mentioned you should set abortConnect=true which isn't in your screenshot, and the aoc: 0 in the exception message confirms it's still not true. Still though, you're on a more recent version which is letting us know we simply couldn't connect ("Last Connection Exception: It was not possible to connect to the redis server(s)"), and yep, my money would be on firewall rules here.

sasankjonna commented 1 year ago

image

This is what I am seeing if I set abortConnect=true

mgravell commented 1 year ago

The abortConnect=true hack can be useful for guarding against transient connection faults, such as "redis server was mostly asleep until we poked it, so the first connect will often time out, but it is there really"; it can't help with fundamental connection problems. Given the mention of net461, I'm thinking "TLS"; I realise your app is net461, but: just while we investigate, is it possible to use the same connect code on net7, just to see if it connects? if net7 connects cleanly, then we can assume that we have working line of sight to the server, which means the difference is something net461-specific (such as: TLS); if net7 doesn't connect cleanly, then we should investigate things like "is the server up", "DNS", "firewalls", etc

sasankjonna commented 1 year ago

image

this is .net 6

sasankjonna commented 1 year ago

with abortconnect=false I see the below error

Unhandled exception. StackExchange.Redis.RedisConnectionException: The message timed out in the backlog attempting to send because no connection became available (5000ms) - Last Connection Exception: It was not possible to connect to the redis server(s). ConnectTimeout, command=GET, timeout: 5000, inst: 0, qu: 1, qs: 0, aw: False, bw: SpinningDown, rs: NotStarted, ws: Idle, in: 0, last-in: 0, cur-in: 0, sync-ops: 1, async-ops: 1, serverEndpoint: sjrediscache.redis.cache.windows.net:6380, conn-sec: n/a, aoc: 0, mc: 1/1/0, mgr: 10 of 10 available, clientName: 5CG0251G81(SE.Redis-v2.6.116.40240), IOCP: (Busy=0,Free=1000,Min=8,Max=1000), WORKER: (Busy=0,Free=32767,Min=8,Max=32767), POOL: (Threads=10,QueuedItems=0,CompletedItems=69), v: 2.6.116.40240 (Please take a look at this article for some common client-side issues that can cause timeouts: https://stackexchange.github.io/StackExchange.Redis/Timeouts) ---> StackExchange.Redis.RedisConnectionException: It was not possible to connect to the redis server(s). ConnectTimeout --- End of inner exception stack trace ---

mgravell commented 1 year ago

Ok, so: probably not TLS; ultimately the client needs to be able to open a connection to the server, and it doesn't look like that's working. The client can't fix network level problems, but if it was me, things I'd try:

sasankjonna commented 1 year ago

I am able to connect and Set/Get values to same Azure redis instance using redis-cli but not from my .net code.

sasankjonna commented 1 year ago

Error :

Unhandled exception. StackExchange.Redis.RedisConnectionException: The message timed out in the backlog attempting to send because no connection became available (5000ms) - Last Connection Exception: It was not possible to connect to the redis server(s). ConnectTimeout, command=SET, timeout: 5000, inst: 0, qu: 1, qs: 0, aw: False, bw: SpinningDown, rs: NotStarted, ws: Initializing, in: 0, last-in: 0, cur-in: 0, sync-ops: 1, async-ops: 1, serverEndpoint: sjrediscache.redis.cache.windows.net:6379, conn-sec: n/a, aoc: 0, mc: 1/1/0, mgr: 10 of 10 available, clientName: 5CG0251G81(SE.Redis-v2.6.122.38350), IOCP: (Busy=0,Free=1000,Min=8,Max=1000), WORKER: (Busy=0,Free=32767,Min=8,Max=32767), POOL: (Threads=12,QueuedItems=0,CompletedItems=65,Timers=4), v: 2.6.122.38350 (Please take a look at this article for some common client-side issues that can cause timeouts: https://stackexchange.github.io/StackExchange.Redis/Timeouts) ---> StackExchange.Redis.RedisConnectionException: It was not possible to connect to the redis server(s). ConnectTimeout --- End of inner exception stack trace --- at StackExchange.Redis.ConnectionMultiplexer.ExecuteSyncImpl[T](Message message, ResultProcessor1 processor, ServerEndPoint server, T defaultValue) in /_/src/StackExchange.Redis/ConnectionMultiplexer.cs:line 2093 at StackExchange.Redis.RedisBase.ExecuteSync[T](Message message, ResultProcessor1 processor, ServerEndPoint server, T defaultValue) in //src/StackExchange.Redis/RedisBase.cs:line 62 at StackExchange.Redis.RedisDatabase.StringSet(RedisKey key, RedisValue value, Nullable`1 expiry, Boolean keepTtl, When when, CommandFlags flags) in //src/StackExchange.Redis/RedisDatabase.cs:line 3164 at AzureRedis.Program.Main(String[] args) in C:\Users\Jonnasn1\source\repos\AzureRedis\AzureRedis\Program.cs:line 19

NickCraver commented 1 year ago

@sasankjonna I see in your exception that you're connecting to :6379 but your connection string in earlier screenshots is :6380. Given this is most likely some configuration mismatch, let's be sure to double check your endpoint please.

:6379 is the default for non-SSL, but :6380 is the SSL endpoint.

sasankjonna commented 1 year ago

Yes, initially I tried to connect to 6380. Later I disabled SSL and been trying to connect to 6379. Ofcourse with no luck. Whom to contact for proper support on this?

NickCraver commented 1 year ago

AFAIK, this is normal Azure Portal support path (cc @philon-msft on the routing question) - I have little insight into those paths, just maintain the client as able.

sasankjonna commented 1 year ago

I am really at dead-end coz a simple POC code is not able to connect to Azure Redis.

philon-msft commented 1 year ago

Yes I'd recommend going through 'Help > Support + Troubleshooting' in the Azure Portal

NickCraver commented 1 year ago

@sasankjonna Did you get going here?

NickCraver commented 1 year ago

Closing out to cleanup here

Mrudula-Nemani commented 7 months ago

Error :

Unhandled exception. StackExchange.Redis.RedisConnectionException: The message timed out in the backlog attempting to send because no connection became available (5000ms) - Last Connection Exception: It was not possible to connect to the redis server(s). ConnectTimeout, command=SET, timeout: 5000, inst: 0, qu: 1, qs: 0, aw: False, bw: SpinningDown, rs: NotStarted, ws: Initializing, in: 0, last-in: 0, cur-in: 0, sync-ops: 1, async-ops: 1, serverEndpoint: sjrediscache.redis.cache.windows.net:6379, conn-sec: n/a, aoc: 0, mc: 1/1/0, mgr: 10 of 10 available, clientName: 5CG0251G81(SE.Redis-v2.6.122.38350), IOCP: (Busy=0,Free=1000,Min=8,Max=1000), WORKER: (Busy=0,Free=32767,Min=8,Max=32767), POOL: (Threads=12,QueuedItems=0,CompletedItems=65,Timers=4), v: 2.6.122.38350 (Please take a look at this article for some common client-side issues that can cause timeouts: https://stackexchange.github.io/StackExchange.Redis/Timeouts) ---> StackExchange.Redis.RedisConnectionException: It was not possible to connect to the redis server(s). ConnectTimeout --- End of inner exception stack trace --- at StackExchange.Redis.ConnectionMultiplexer.ExecuteSyncImpl[T](Message message, ResultProcessor1 processor, ServerEndPoint server, T defaultValue) in /_/src/StackExchange.Redis/ConnectionMultiplexer.cs:line 2093 at StackExchange.Redis.RedisBase.ExecuteSync[T](Message message, ResultProcessor1 processor, ServerEndPoint server, T defaultValue) in //src/StackExchange.Redis/RedisBase.cs:line 62 at StackExchange.Redis.RedisDatabase.StringSet(RedisKey key, RedisValue value, Nullable`1 expiry, Boolean keepTtl, When when, CommandFlags flags) in //src/StackExchange.Redis/RedisDatabase.cs:line 3164 at AzureRedis.Program.Main(String[] args) in C:\Users\Jonnasn1\source\repos\AzureRedis\AzureRedis\Program.cs:line 19

I am also facing the same error, can someone help me how to solve this.

mgravell commented 7 months ago

"It was not possible to connect to the redis server(s)." - I can't solve that from here; you'd need to check:

The quickest way to answer most of these is to use redis-cli from the intended client machine; can you connect? If you can't connect with redis-cli, all bets are off. Note that if the server is behind TLS, you need to use recent versions of redis-cli, and in particular the long out-of-date Windows port of redis-cli lacks this, IIRC.

It is not sufficient to use redis-cli on the server itself; that misses most of the places things can be broken.