zoukankan      html  css  js  c++  java
  • redis管道(pipeline)

    redis pipeline

    redis是一个cs模式的tcp server,使用和http类似的请求响应协议。一个client可以通过一个socket连接发起多个请求命令。每个请求命令发出后client通常 会阻塞并等待redis服务处理,redis处理完后请求命令后会将结果通过响应报文返回给client。基本的通信过程如下:

    (Client)127.0.0.1:6379> incr x

    (Server)(integer) 1

    (Client)127.0.0.1:6379> incr x

    (Server) (integer) 2

    (Client)127.0.0.1:6379> incr x

    (Server) (integer) 3

    (Client)127.0.0.1:6379> incr x

    (Server) (integer) 4

    基 本上四个命令需要8个tcp报文才能完成。由于通信会有网络延迟,假如从client和server之间的包传输时间需要0.125秒。那么上面的四个命 令8个报文至少会需要1秒才能完成。这样即使redis每秒能处理100个命令,而我们的client也只能一秒钟发出四个命令。这显示没有充分利用 redis的处理能力。除了可以利用mget,mset 之类的单条命令处理多个key的命令外
    我们还可以利用pipeline的方式从client打包多条命令一起发出,不需要等待单条命令的响应返回,而redis服务端会处理完多条命令后会将多条命令的处理结果打包到一起返回给客户端。通信过程如下:

    (Client)127.0.0.1:6379> incr x

    (Client)127.0.0.1:6379> incr x

    (Client)127.0.0.1:6379> incr x

    (Client)127.0.0.1:6379> incr x

    (Server)(integer) 1

    (Server) (integer) 2

    (Server) (integer) 3

    (Server) (integer) 4

    假 设不会因为tcp 报文过长而被拆分。可能两个tcp报文就能完成四条命令,client可以将四个incr命令放到一个tcp报文一起发送,server则可以将四条命令 的处理结果放到一个tcp报文返回。通过pipeline方式当有大批量的操作时候。我们可以节省很多原来浪费在网络延迟的时间。需要注意到是用 pipeline方式打包命令发送,redis必须在处理完所有命令前先缓存起所有命令的处理结果。打包的命令越多,缓存消耗内存也越多。所以并是不是打 包的命令越多越好。具体多少合适需要根据具体情况测试。下面是个jredis客户端使用pipeline的测试:

    public class RedisTest {

        Jedis j=null;

       

     

        public RedisTest() {

             j = new Jedis("192.168.168.128",6379);

            // TODO Auto-generated constructor stub

        }

     

        public static void main(String[] args) {

            // TODO Auto-generated method stub

            int count = 10000;

            long start = System.currentTimeMillis();

            new RedisTest().withOutPipeline(count);

            long end = System.currentTimeMillis();

            System.out.println("withOutPipeline:"+(end-start));

            start = System.currentTimeMillis();

            new RedisTest().usePipeline(count);

            end = System.currentTimeMillis();

            System.out.println("usePipeline:"+(end-start));

           

        }

       

       

        public void usePipeline(int count){

            Pipeline p = j.pipelined();

            try {

                for(int i=0;i<count;i++){

                    p.incr("usePipe");

                }

                p.sync();

            } catch (Exception e) {

                // TODO: handle exception

            }finally{

                if(j!=null){

                    j.disconnect();

                }

            }

     

        }

       

        public void withOutPipeline(int count){

            try {

                for(int i=0;i<count;i++){

                    j.incr("usePipe");

                }

            } catch (Exception e) {

                // TODO: handle exception

            }finally{

                if(j!=null){

                    j.disconnect();

                }

            }

     

        }

     

    }

    输出:

    withOutPipeline:2302

    usePipeline:130

    测试结果还是很明显有较大的差距,所以多次操作用pipeline还是有明显的优势。我用的是Win7中的Jedis Java客户端程序连接局域网的Linux虚拟机上的Redis Server。

  • 相关阅读:
    字符数组+数组复习
    C语言博客作业05-指针
    C语言博客作业04 数组
    C语言博客作业03 函数
    Java与C# socket通信
    JDBC复制数据库(sqlite)
    mysql Connector/net不能更新或删除(转载)
    MATLAB回归、插值、逼近、拟合【转载】
    前端请求RestController发送数据方法汇总
    elementUI el-input 输入框 设置高度和宽度
  • 原文地址:https://www.cnblogs.com/yangml/p/4117366.html
Copyright © 2011-2022 走看看