Java implementation of Redis pipeline comparison

Source: Internet
Author: User

Start experimenting with this because of the Redis combat. pdf above is an example.

It was a org.jredis bag, but found it wasn't in Maven's public repository. You need to download and then place it locally to import maven dependencies. See:

http://blog.csdn.net/zhu_tianwei/article/details/44900955

Https://github.com/alphazero/jredis

There are also examples on the Redis combat. pdf.

The MAVEN public Warehouse offers a Jedis package. There are also examples on the Internet:

Http://www.tuicool.com/articles/aeuAfaN

Let's experiment with this bag.

The code is as follows, note that Auth password authentication is added:

 PackageCom.myapp.redisdemo;ImportRedis.clients.jedis.Jedis;ImportRedis.clients.jedis.Pipeline;ImportRedis.clients.jedis.Response;ImportJava.util.HashMap;ImportJava.util.Map;ImportJava.util.Set;/*** Created by Baidu on 16/10/16.*/ Public classRedisdemo { Public Static voidMain (string[] args) {String Redisip= "10.117.146.16"; intRedisport = 6379;        Jedis Jedis; Try{Jedis=NewJedis (Redisip, Redisport); Jedis.auth ("[User name]"); Jedis.select (8); }        Catch(Exception e) {e.printstacktrace (); System.out.printf ("Initializing Redis connection error:%s,%d", Redisip, Redisport); return;        } jedis.flushdb (); LongStart =System.currenttimemillis ();        Notusepipeline (Jedis); LongEnd =System.currenttimemillis (); System.out.printf ("When pipeline is not used:%d milliseconds", end-start);        Jedis.flushdb (); Start=System.currenttimemillis ();        Usepipeline (Jedis); End=System.currenttimemillis (); System.out.printf ("Time to use pipeline:%d milliseconds", end-start); }    Private Static voidnotusepipeline (Jedis jedis) {Map<string, string> MP =NewHashmap<string, string>(); Try {             for(inti=0; i<10000; i++) {mp.clear (); Mp.put ("K" +i, "V" +i); Jedis.hmset ("Keys" +I, MP); }        }        Catch(Exception e) {e.printstacktrace (); }    }    Private Static voidusepipeline (Jedis jedis) {Map<string, string> MP =NewHashmap<string, string>(); Try{Pipeline PL=jedis.pipelined ();  for(inti=0; i<10000; i++) {mp.clear (); Mp.put ("K" +i, "V" +i); Pl.hmset ("Keys" +I, MP);        } pl.sync (); }        Catch(Exception e) {e.printstacktrace (); }    }}

Run (run without configuration, direct command line), error:

Initializing Redis connection error: 10.117.146.16, 6379

Judging because the port needs to be set to more than 8000.

So the Redis main library port is changed to 8379, the port from the library remains the same (pay attention to change from the library master-slave synchronization of the port).

In addition, 10,000 operations are too slow, so changed to 1000 times. (During the run, you can select 8 and then dbsize to see how many bars have been inserted).

It is now possible to run successfully with the following results:

0

Without pipeline, ran for more than 72 seconds, the average operation of more than 10 data per second.

The pipeline approach, which only runs for 173 milliseconds, improves throughput by a lot.

This also shows that: a lot of time is in the network interaction, Redis itself processing power is very strong.

Try pipeline time with a larger amount of data.

10,000: Start running using pipeline in a way that uses pipeline: 769 ms 50,000: When you start to run using pipeline in a pipeline way : 1275 MS 100,000: Start running using pipeline in a way that uses pipeline: 7297 milliseconds

Note that in a very large amount of time, the previous experience with Python is that pipeline will go wrong. Do not know Jedis itself will do optimization. Still need to use, some attention.

A good balance in reducing network transmission consumption, increasing throughput, and ensuring the success of each operation.

Now there are 100,000 data in db 8, you can flushdb to delete this DB data, then you can use the command bgrewriteaof you learned earlier to compress the AoF file.

Java implementation of Redis pipeline comparison

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.