13

I am using Jedis client for connecting to my Redis server. The following are the settings I'm using for connecting with Jedis (using apache common pool):

JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setTestOnBorrow(true);
poolConfig.setTestOnReturn(true);
poolConfig.setMaxIdle(400);

// Tests whether connections are dead during idle periods
poolConfig.setTestWhileIdle(true);
poolConfig.setMaxTotal(400);

// configuring it for some good max value so that timeout don't occur
poolConfig.setMaxWaitMillis(120000);

So far with these setting I'm not facing any issues in terms of reliability (I can always get the Jedis connection whenever I want), but I am seeing a certain lag with Jedis performance.

Can any one suggest me some more optimization for achieving high performance?

slm
  • 15,396
  • 12
  • 109
  • 124
pjain
  • 1,149
  • 2
  • 14
  • 28
  • Which part is "slow" to you? Could be network roundtrips which you can get lowered if you use pipelining or lua scripts. Redis is quite fast, so it shouldn't be that. Is your pool defined with many jedis instances? If too many components ask simutanously for a jedis instance, it will block tasks as there are not enough resources for all. Can you check that? – zenbeni Mar 28 '15 at 09:52
  • How do you arrive at the number 400 here? What are the advantages and disadvantages of using a larger number? I am using redis to fetch a data at regular internals using jedis. The users who are calling the request will not be uniform. Sometimes there might be 1000 requests per seconds and sometimes 50. What should be the ideal MaxTotal? To know this I wanted to get the advantages and disadvantages. – Programmer Mar 06 '19 at 04:14

1 Answers1

17

You have 3 tests configured:

  • TestOnBorrow - Sends a PING request when you ask for the resource.
  • TestOnReturn - Sends a PING whe you return a resource to the pool.
  • TestWhileIdle - Sends periodic PINGS from idle resources in the pool.

While it is nice to know your connections are still alive, those onBorrow PING requests are wasting an RTT before your request, and the other two tests are wasting valuable Redis resources. In theory, a connection can go bad even after the PING test so you should catch a connection exception in your code and deal with it even if you send a PING. If your network is stable, and you do not have too many drops, you should remove those tests and handle this scenario in your exception catches only.

Also, by setting MaxIdle == MaxTotal, there will be no eviction of resources from your pool (good/bad?, depends on your usage). And when your pool is exhausted, an attempt to get a resource will endup in timeout after 2 minutes of waiting for a free resource.

Ofir Luzon
  • 10,635
  • 3
  • 41
  • 52
  • Thanks Ofir for answering my question. I kept all these checks to always get a resource without fail. But it seems these are also making me slow for some of Jedis get/set requests.I understood your first three parameters usage, but still have doubt on usage of "max Idle" and "maxTotal" . say I keep 100 resources as "max Total" and 50 as max idle, will I get only 50 active connections or will I get 100 active connections with these settings ? – pjain Mar 30 '15 at 05:23
  • Also wanted to know how jedis pool manages its connections ? Say If I set 100 as active connections, will it maintain all the 100 active connections automatically(for the lifetime of application) or I need to reconnect for some resource in case one goes broken(if jedis.getResource() returns exception for these connections) ? – pjain Mar 30 '15 at 08:29
  • 1
    "Also, by setting MaxIdle == MaxTotal, there will be no eviction of resources from your pool (good/bad?, depends on your usage). " is wrong I guess. Even if you set maxidle == MaxTotal there will be eviction if you set timeBetweenEvictionRunsMillis() > 0. – Manu Jose Feb 07 '18 at 13:26