Dam*_*vić 1 java multithreading lettuce
I'm trying to decide whether or not to use multiple stateful connections across more than one thread or only reuse one. I'm using java.
Even though it says for StatefulRedisClusterConnectionImpl that the connection is thread-safe to a Redis Cluster and that multiple threads may share one, I am getting better performance results when using one connection per thread.
This is my code testing code:
final int THREADS = 5;
List<RedisAdvancedClusterCommands<String, String>> cmdLIst = new LinkedList<>();
for (int j = 0; j < THREADS; j++) {
cmdLIst.add(redisClient.connect().sync());
}
RedisAdvancedClusterCommands<String, String> cmd = redisClient.connect().sync();
HashMap<Integer, List<String>> keysPerNode = new HashMap<>();
for (int i = 0; i < THREADS; i++) {
List<String> keys = generateKeys();
keysPerNode.put(i, keys);
Thread insertThread = new InsertClass(cmd, keys, startLatch, endLatch);
//Thread insertThread = new InsertClass(cmdLIst.get(i), keys, startLatch, endLatch);
insertThread.setName("Insert thread(" + i + ")");
insertThread.start();
}
Thread.sleep(5000);
for (int j = 0; j < THREADS; j++) {
startLatch.countDown();
}
long start = System.currentTimeMillis();
endLatch.await();
printElapsed((System.currentTimeMillis() - start) / 1000);
cmdLIst.forEach(RedisClusterCommands::close);
cmd.close();
Run Code Online (Sandbox Code Playgroud)
InsertClass extends Thread.
I was shifting between using cmd where I reuse connection for all threads and using cmdLIst where every connection uses it's own connection.
My setup is 4 masters on 4 remote (code is not running on the same machine as redis cluster) machines (+ slaves).
Every thread performs 80k set operations for random keys and than 80k get operations for these same keys. Also I tried to do that same thing, but with 1 key (80k set's for that one key + 80k get's for that same key, same key for all threads).
Result is always the same, when using multiple connections (1 connection per thread), I get around 20 second difference (95 sec vs 115 sec).
Is there some kind of drawback when using one connection per thread? Or some undesired consequences that I am not aware of, because it seems to me that this will be preferred way of using this API.
Lettuce 连接是线程安全的。单个连接可以在多个线程之间共享。不应共享连接的唯一例外是当您使用事务(不适用于 Redis 集群)或阻止 Redis 命令(例如BLPOP、BRPOP)时。
一个 Redis 集群连接最多创建
\n\n
永久 TCP 连接,其中n是 Redis 集群节点的数量。
如果您有一个小型集群,并且每个应用程序实例有合理数量的线程,则可以应用每线程连接模式。如果您的集群/应用程序规模达到了获得不合理连接数量的规模,那么您可能被迫为每个应用程序实例使用一个连接。
\n\n一般来说,使用单个连接涉及的资源较少。默认的管道操作模式一旦调用命令就从调用线程发送命令 \xe2\x80\x93 连接不会等到上一个命令完成。
\n| 归档时间: |
|
| 查看次数: |
2110 次 |
| 最近记录: |