Lettuce Connection Pooling

3,695 views
Skip to first unread message

Veeraraghavan S

unread,
Jul 15, 2016, 8:45:58 AM7/15/16
to lettuce-redis-client-users
Hi, I am newbie to java itself and I am trying to access Redis using Lettuce but I was wondering is there is a sample Lettuce code which would look into existing open connections from same connecting client and reuse them, rather then creating connection , closing them. I am trying to a POC but once successful, we would hit redis a 100K times easily setting & getting values.
The client which will call redis will sit on the same server (as localhost) but will run as a different user. Hence wanted to know if there is an example code for connection polling or reuse connection which are already established.

Simple code which works
---------------------------------------
RedisClient client = new RedisClient("localhost");
RedisConnection<String, String> connection = client.connect();
String value=connection.get("key2");
connection.close();
client.shutdown();

What I do see on the client is a connection which was opened from my client PC, but I am not able to reuse this connection

127.0.0.1:6379> client list
id=170 addr=XX.XX.XX.153:55310 fd=6 name= age=258513 idle=258513 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=exec

Also when executed from the server I keep getting this warning. Will the connection polling also allocate a thread size and handle them efficiently.
Jul 15, 2016 2:41:53 PM com.lambdaworks.redis.resource.DefaultClientResources <init>
INFO: ioThreadPoolSize is less than 3 (2), setting to: 3
Jul 15, 2016 2:41:53 PM com.lambdaworks.redis.resource.DefaultClientResources <init>
INFO: computationThreadPoolSize is less than 3 (2), setting to: 3
Connected to Redis - Successful

Any help on this would be greatly appreciated

Mark Paluch

unread,
Jul 16, 2016, 4:15:00 AM7/16/16
to lettuce-redis-client-users
Hi Veeraraghavan, 

For your case (no blocking operations, no transactions) you don't need connection pooling at all. Lettuce is thread-safe with long-lived connections. Simply open the connection when your application starts/initializes and just use it. lettuce automatically reconnects in case the connection gets disconnected unintentionally.

Not sure I understand what you mean by

but I am not able to reuse this connection

Don't bother with the warnings. It basically means that your machine has two processors and lettuce tries to allocate the same number of Threads as you have processors. lettuce requires at least 3 worker threads per ThreadPool and that's what the message is saying.

Cheers, Mark

Veeraraghavan S

unread,
Jul 16, 2016, 4:49:15 AM7/16/16
to lettuce-redis-client-users
HI Mark,
Thank a lot for your response.
The number of SET/GET operation to/from REDIS would be every second throughout the day and the hit would easily cross 1000K a day. When I run a simple code as below on the server (as a different user , java -cp redis.jar GetValue), it takes visibly a second atleast or more to get back my result/response and this is not helping us. My understanding is that, each time even though on same server, we need to connect to port 6379 and that is getting costlier for us

1) Create a new connection instance
2) Connect to the connection
3) GET value
4) Close connection
5) Shutdown initiation

Simple code which works

------------------------------
RedisClient client = new RedisClient("localhost");
RedisConnection<String, String> connection = client.connect();
String value=connection.get("key2");
connection.close();
client.shutdown();


What I would like to do it

1) Try for open connection
2) GET & return values. Dont close anything
3) Leave the connection as open
4) If TRY fails, execute new connection initiation


I am not sure about the context of CLIENT LIST command with respect to programming. But I see a session on localhost left open if I dont give the 'connection.close()' command. However each time, I see a new connections created rather than reusing which is already open.


127.0.0.1:6379> client list
id=170 addr=XX.XX.XX.153:55310 fd=6 name= age=258513 idle=258513 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=exec


Hence was needing help for a sample code which would can be used for extremely reliable for higher number of transactions and get the results back in milli-seconds throughout the day / week / year.
Might be I missing something core in my understanding, but your help would be extremely appreciated.

Regards
Veera

Mark Paluch

unread,
Jul 16, 2016, 4:56:57 AM7/16/16
to lettuce-redis-client-users
Hi, 

I think your problem is located in a different area. Lettuce requires a while (100ms) to initialize and by default a while (2000ms) to shut down. Usually your application runs by an order of magnitude longer (hours, days, weeks). 

Invoking a Java program just to retrieve the Value from Redis always requires initialization and shutdown overhead. Each time a new connection is created, the data is read from Redis and the client is shut down afterward. There's no way of keeping a connection alive after the java -cp ... command terminates because the process terminates and all state is gone.

You need some long-living runtime context (such as a server process or a client application process) that keeps a reference to the client and so the connection state is preserved. You should take a look how applications are built with Java. That's not the right forum for basic Java questions but related to the lettuce Redis client. You'd run with every other client into the same issue.

Cheers, Mark

Veeraraghavan S

unread,
Jul 16, 2016, 5:20:59 AM7/16/16
to lettuce-redis-client-users
Hi Mark,
Thanks once again for your response. We dont have an easy option to connect to REDIS except for invoking a java code via a class file or a java node which again requires a class file. Our Main integrator is a software which allows us to use Java as extension to connect to REDIS applications and Lettuce was preferred for POC
Looking at the options that you have suggested, it seems to me that that there is no way for a faster data retrieval using a static Lettuce Redis client java call(our application limitation). I was hoping that connection pooling would be an option(or any example of how Lettuce is implemented in real world scenario instead of Oracle DB hit) where a program can be run continuously and an instance of that connection can be used for GETting the values rather than connecting and closing everytime. Anyways thanks once again for your response and time.

Regards
Veera

Mark Paluch

unread,
Jul 16, 2016, 4:26:52 PM7/16/16
to lettuce-redis-client-users
Thanks for the more context. Based on these details I propose two different approaches that should be more lightweight to implement:

1. Use Jedis: You don't benefit from the lettuce features in your case. Jedis is much more lightweight and the initialization/shutdown costs are lower
2. Use redis-cli: That's the simple-most possibility. You don't need to spin up a JVM. The command-line interface is very powerful and I think that's my favorite option.

Best regards, 
Mark

Veeraraghavan S

unread,
Jul 17, 2016, 5:01:01 AM7/17/16
to lettuce-redis-client-users
Hi Mark,

Thanks once again for taking out time to suggest further options. I will try both the redis client & Jedis route to see how it works out for me. I am in parallel working out how best 'long-living runtime context' can be implemented.
I will let you know how it goes. Thanks once again

Regards
Veera

Veeraraghavan S

unread,
Jul 29, 2016, 6:30:24 AM7/29/16
to lettuce-redis-client-users
Hi Mark,

Thanks for your suggestion. I did achieve lower latency with JEDIS as we only do simple GET / SET on the server and we managed to cache the connection as well.
I did have another problem that I see on the server now. Can you help me with some pointers please.

Normally if I open a Connection pool and don't close it (pool.close or shutdown) in the java code, I would see the client connection open (with IP address) if I run the the 'client list' command on the server. However I no longer see the behavior and the connections from the server keep closing. We didn't do anything on the server side configuration and the timeout on the server config file is set to 0.

Regards
Veera
Reply all
Reply to author
Forward
0 new messages