In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly shows you how to use pipeline in redis. The content is simple and easy to understand. It is clearly organized. I hope it can help you solve your doubts. Let Xiaobian lead you to study and learn how to use pipeline in redis.
Redis is a cs-mode tcp server that uses a request-response protocol similar to http. A client can initiate multiple requests over a socket connection. After each request command is sent, the client usually blocks and waits for redis service to process it. After redis processes the request command, the result will be returned to the client through a response message. The basic communication process is as follows
Client: INCR X
Server: 1
Client: INCR X
Server: 2
Client: INCR X
Server: 3
Client: INCR X
Server: 4
Basically, four commands require eight tcp messages to complete. Since there is network latency for communication, suppose the packet transfer time between client and server takes 0.125 seconds. Then the above four orders and eight messages will take at least one second to complete. So even if redis can process 100 commands per second, our client can only issue four commands per second. This shows that redis processing power is not being fully utilized. In addition to commands that can handle multiple keys with a single command such as mget,mset, etc
We can also use pipeline to package multiple commands from the client and issue them together, without waiting for the response of a single command to return, while the redis server will process multiple commands and package the processing results of multiple commands together to return to the client. The communication process is as follows
Client: INCR X
Client: INCR X
Client: INCR X
Client: INCR X
Server: 1
Server: 2
Server: 3
Server: 4
Suppose tcp messages are not split because they are too long. Four commands may be completed in two TCP messages. The client may put four incr commands into one TCP message and send them together, while the server may put the processing results of four commands into one TCP message and return them. By pipeline mode when there is a large number of operations. We can save a lot of time wasted on network latency. Note that commands are sent in pipeline packages, and redis must cache the results of all commands before processing them. The more commands you pack, the more memory your cache consumes. So it's not like the more orders you have to pack, the better. How much is appropriate needs to be tested on a case-by-case basis. Here is a jredis client using pipeline test
package jredisStudy;
import org.jredis.JRedis;
import org.jredis.connector.ConnectionSpec;
import org.jredis.ri.alphazero.JRedisClient;
import org.jredis.ri.alphazero.JRedisPipelineService;
import org.jredis.ri.alphazero.connection.DefaultConnectionSpec;
public class PipeLineTest {
public static void main(String[] args) {
long start =System.currentTimeMillis();
usePipeline();
long end =System.currentTimeMillis();
System.out.println(end-start);
start = System.currentTimeMillis();
withoutPipeline();
end =System.currentTimeMillis();
System.out.println(end-start);
}
private static void withoutPipeline()
{
try {
JRedis jredis = new JRedisClient("192.168.56.55",6379);
for(int i =0 ; i < 100000 ; i++)
{
jredis.incr("test2");
}
jredis.quit();
} catch (Exception e) {
}
}
private static void usePipeline() {
try {
ConnectionSpec spec =DefaultConnectionSpec.newSpec("192.168.56.55", 6379, 0, null);
JRedis jredis = newJRedisPipelineService(spec);
for(int i =0 ; i
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.