2

Disclaimer: I am quite new to Redis and StackExchange.Redis client.

Scenario: I've got an ASP.NET MVC web app that uses ASP.NET Session State Provider for Azure Redis Cache.

This works like a charm!

Now, my controllers call my services layer (it's just a DLL reference but may become a Web Api call one day). I am trying to configure my redis client with Autofac as per below:

        var redis = ConnectionMultiplexer.Connect(ConfigurationManager.AppSettings["redis.connectionstring"]);
        builder.RegisterInstance(redis).AsSelf().SingleInstance();
        builder.RegisterType<RedisCacheClient>().As<ICacheClient>().WithParameter("db", redis.GetDatabase());

The connection is set up successfully and I can see a new client when I run client list on redis-cli.exe.

My RedisCacheClient class is just a thin wrapper on IDatabase:

    public void Add(string key, object value)
    {
        _db.StringSet(key, Serialize(value));
    }

    public T Get<T>(string key)
    {
        byte[] buffer = _db.StringGet(key);
        T o = Deserialize<T>(buffer);
        return o;
    }

    private static byte[] Serialize(object o)
    {
        if (o == null)
        {
            throw new ArgumentNullException("Cannot cache null values.");
        }
        var binaryFormatter = new BinaryFormatter();
        using (var memoryStream = new MemoryStream())
        {
            binaryFormatter.Serialize(memoryStream, o);
            byte[] buffer = memoryStream.ToArray();
            return buffer;
        }
    }

    private static T Deserialize<T>(byte[] stream)
    {
        if (stream == null)
        {
            return default(T);
        }
        var binaryFormatter = new BinaryFormatter();
        using (var memoryStream = new MemoryStream(stream))
        {
            T result = (T)binaryFormatter.Deserialize(memoryStream);
            return result;
        }
    }

Problem: I can store objects successfully but can never retrieve then! I keep getting connections timeout no matter what I do (current timeout is 30 secs). I've spent the last 4 hours trying to figure this out and no luck. Really struggling :(

Some of the resources I have already gone through: http://azure.microsoft.com/blog/2015/02/10/investigating-timeout-exceptions-in-stackexchange-redis-for-azure-redis-cache/

Timeout performing SET {Key}, inst: 0, mgr: Inactive, queue: 2, qu=1, qs=1, qc=0, wr=1/1, in=0/0

StackExchange.Redis with Azure Redis is unusably slow or throws timeout errors

Azure Redis cache - timeouts on GET calls

https://github.com/StackExchange/StackExchange.Redis/issues/122

https://github.com/StackExchange/StackExchange.Redis/issues/83

P.S: I get the very same behaviour running this locally or on azure.

Community
  • 1
  • 1
Andre Gallo
  • 2,261
  • 5
  • 23
  • 46
  • What sort of size data are you storing as an individual string? (I'm trying to assess whether I should *expect* this to fail or not; I'm assuming "not", but worth asking) – Marc Gravell Jun 02 '15 at 11:03
  • Pretty small data actually. Tried a string like "foo", then a bool value, then a simple User object with FirstName and LastName as public properties. It's interesting because I can see all keys/values stored and retrieve then quite quickly using the command line. – Andre Gallo Jun 02 '15 at 12:07
  • more info @MarcGravell: When I do db.StringSet("foo", "bar") and then db.StringGet("foo") I consistently get: **Timeout performing GET foo, inst: 1, mgr: ExecuteSelect, queue: 2, qu: 2, qs: 0, qc: 0, wr: 0, wq: 1, in: 0, ar: 0, IOCP: (Busy=0,Free=1000,Min=2,Max=1000), WORKER: (Busy=2,Free=4093,Min=2,Max=4095)** – Andre Gallo Jun 05 '15 at 14:30
  • Can you share your connection string (minus password) so that we can look at all your settings? – JonCole Jul 01 '15 at 17:35

1 Answers1

0

As much as I truly dislike this I have to say that the issue is long gone and I have no idea why. I believe this was some sort of intermittent issue related to my setup (MacBook Pro running windows on a parallels VM) which might have been maxed out at times making Redis reject or not process items on its queue. Well, that's the only thing I could come up with since I have been running the same software for days now and have not had any issues.

Andre Gallo
  • 2,261
  • 5
  • 23
  • 46