2

I want some suggestions on using PooledRedisClientManager() to connect to remote Redis Instance on Ubuntu virtual box.

I tried PooledRedisClientManager pCm = new PooledRedisClientManager(new[] {"xxx.xxx.x.x:6379"}); but getting exception "could not connect to redis Instance at xxx.xxx.x.x:6379"

Pritam
  • 1,288
  • 5
  • 23
  • 40

1 Answers1

4

Connection Exception:

First ensure that your Redis server is configured to run on a network reachable IP. Ensure you have changed redis.conf bind line from:

bind 127.0.0.1

to

bind 192.168.0.1

Where 192.168.0.1 is the hostname/IP of your Redis server.

Or you will only be able to access the Redis server locally. You should avoid using 0.0.0.0 in place of your local IP, to prevent Redis binding to any public interfaces, thus exposing it to the Internet. (Unless protected by your firewall)

If you have the Redis client installed on your application server, you can check that the Redis server is accessible by running this command from the terminal:

redis-cli -h 192.168.0.1 ping

This answer here steps through the process of diagnosing the connection issues.

If you are still getting exceptions while connecting ensure that your firewall on your application machine allows the outgoing connection to be established, and on your Redis service your firewall allows incoming connections on that port from your application server. Ubuntu uses IPTables for firewalling.


Using ServiceStack.Redis without the ServiceStack Platform:

You shouldn't have any issues using it the way you are doing it already, other than configuration problems of your actual Redis Server.

// Where 192.168.0.1 is the hostname/IP of your Redis server
var pcm = new PooledRedisClientManager(new[] {"192.168.0.1:6379"});
var client = pcm.GetCacheClient();

Using ServiceStack.Redis with the ServiceStack platform:

Configure the PooledRedisClientManager to be injected:

Assuming you are using ServiceStack's standard approach of dependancy injection, you will want to wire up your App Configuration to inject the client manager into your Service base class. This makes Redis available to each request. So in your AppHost Configuration:

// Where 192.168.0.1 is the hostname/IP of your Redis server
container.Register<IRedisClientsManager>(c => new PooledRedisClientManager("192.168.0.1:6379"));
// Register Redis cache client to be injected as ICacheClient, using pooled manager, registered above
container.Register<ICacheClient>(c => c.Resolve<IRedisClientsManager>().GetCacheClient());

Using Redis:

Once you have injected the pool, the standard ICacheClient and IRedisClient will be automatically wired up when you call base.Cache or base.Redis, respectively, within your Service. So there is no further configuration required to use Redis.

Community
  • 1
  • 1
Scott
  • 21,211
  • 8
  • 65
  • 72
  • It's not enough just to inject `IRedisClientsManager` in order to get `RedisClient` injected into `base.Cache`. It should be explicitly inejcted. For example: `container.Register(c => c.Resolve().GetCacheClient()).ReusedWithin(ReuseScope.Request)` – ajukraine Feb 22 '14 at 18:46
  • @ajukraine Not true. If you look at the [Service base class](https://github.com/ServiceStack/ServiceStack/blob/master/src/ServiceStack/Service.cs#L64) you will see that it will try and resolve the `IRedisClientsManager` that was injected. Then it calls `GetCacheClient()` on that resolve. So it definitely does not need to be more explicitly injected. – Scott Feb 22 '14 at 19:03
  • It's correct, but if you look at the [ServiceStackHost base class](https://github.com/ServiceStack/ServiceStack/blob/master/src/ServiceStack/ServiceStackHost.cs#L352), you'll see that `MemoryCacheClient` is injected in the case if `ICacheClient` is not configured. – ajukraine Feb 22 '14 at 19:18
  • @ajukraine In that case what you are saying is true for `base.Cache` only in as much as the resolver code for the `RedisClientManager` is unreachable. I think this is an oversight, I will submit a pull request to Mythz so that `ICacheClient` initialises as `RedisCacheClient` if `ICacheClient` is undefined and `RedisClientManager` is defined which I think is more inline with what the `base.Cache` method was trying to do. – Scott Feb 22 '14 at 20:10
  • @ajukraine On second thoughts, I don't think it will be possible, to default it in the ServiceStackHost, because that would introduce a dependency on the ServiceStack.Redis library. I'll check directly with Mythz and see what his thoughts are, just more out of curiosity as to the seemingly unreachable code in `base.Cache` – Scott Feb 22 '14 at 20:21
  • It doesn't require a dependency on ServiceStack.Redis library, because `IRedisClient` is located in `ServiceStack.Interfaces` package, which is a dependency of `ServiceStack`. Moreover there is already reference to `IRedisClient` in [Service class](https://github.com/ServiceStack/ServiceStack/blob/master/src/ServiceStack/Service.cs#L74), so I don't think it's problem to implement default/conventional resolving of `RedisClient`. Sure, @Mythz can provide more information on the decision. – ajukraine Feb 23 '14 at 00:09
  • @ajukraine Good point about the interface ... stupid me :) .. was too tired when I fired up the project yesterday. I have submitted a [pull request](https://github.com/ServiceStack/ServiceStack/pull/886). But it may be that the change is considered breaking. – Scott Feb 23 '14 at 10:46