Eight ways to call Redis's Java client Jedis (transactional, pipeline, distributed ...) Introduced

Source: Internet
Author: User

[-]

  1. A common synchronization mode
  2. Two-Transaction Mode transactions
  3. Three-pipe pipelining
  4. Invoke transactions in four pipelines
  5. Five distributed Direct connect synchronous calls
  6. Six distributed direct-attached asynchronous calls
  7. Seven distributed connection pool synchronous calls
  8. Eight distributed connection pooling asynchronous calls
  9. Nine places to watch out for
  10. Ten Tests
  11. 11 Complete Test Code

    Redis is a well-known key-value storage System, and as its official recommended Java version of the client Jedis is also very strong and stable, supporting transactions, pipelines and Jedis itself implementation of the distributed.

    Here is a brief introduction and comparison of the Jedis about transactions, pipelines, and distributed invocation methods:

    One, common synchronization mode

    The simplest and most basic way to call,

    @Testpublic void test1Normal() {    Jedis jedis = new Jedis("localhost");    long start = System.currentTimeMillis();    for (int i = 0; i < 100000; i++) {        String result = jedis.set("n" + i, "n" + i);    }    long end = System.currentTimeMillis();    System.out.println("Simple SET: " + ((end - start)/1000.0) + " seconds");    jedis.disconnect();}

    It's simple, set you can return the result after each time, and the token is successful.

    Ii. mode of affairs (transactions)

    Redis's transaction is simple, and his primary purpose is to ensure that commands in a client-initiated transaction can be executed consecutively, without inserting other client commands.

    Look at the following example:

    @Testpublic void test2Trans() {    Jedis jedis = new Jedis("localhost");    long start = System.currentTimeMillis();    Transaction tx = jedis.multi();    for (int i = 0; i < 100000; i++) {        tx.set("t" + i, "t" + i);    }    List<Object> results = tx.exec();    long end = System.currentTimeMillis();    System.out.println("Transaction SET: " + ((end - start)/1000.0) + " seconds");    jedis.disconnect();}

    We call the jedis.watch(…) method to monitor the key, and if the key value changes after the call, the entire transaction fails. Additionally, an operation in the transaction fails, and no other operations are rolled back. This is a point to note. Also, we can use discard() methods to cancel transactions.

    Third, Pipeline (pipelining)

    Sometimes we need to send multiple instructions at once, asynchronously, and wait for the results to be returned. This can achieve very good execution efficiency. This is the pipeline, called by the following method:

    @Testpublic void test3Pipelined() {    Jedis jedis = new Jedis("localhost");    Pipeline pipeline = jedis.pipelined();    long start = System.currentTimeMillis();    for (int i = 0; i < 100000; i++) {        pipeline.set("p" + i, "p" + i);    }    List<Object> results = pipeline.syncAndReturnAll();    long end = System.currentTimeMillis();    System.out.println("Pipelined SET: " + ((end - start)/1000.0) + " seconds");    jedis.disconnect();}
    Iv. invoking a transaction in a pipeline

    As far as the method provided by Jedis is concerned, it is possible to use transactions in the pipeline with the following code:

    @Testpublic void test4combPipelineTrans() {    jedis = new Jedis("localhost");     long start = System.currentTimeMillis();    Pipeline pipeline = jedis.pipelined();    pipeline.multi();    for (int i = 0; i < 100000; i++) {        pipeline.set("" + i, "" + i);    }    pipeline.exec();    List<Object> results = pipeline.syncAndReturnAll();    long end = System.currentTimeMillis();    System.out.println("Pipelined transaction: " + ((end - start)/1000.0) + " seconds");    jedis.disconnect();}

    But after testing (see later in this article), it was found that its efficiency was similar to that of a single-use transaction, and even slightly nearly.

    Five, distributed direct connection synchronous call
    @Testpublic void test5shardNormal() {    List<JedisShardInfo> shards = Arrays.asList(            new JedisShardInfo("localhost",6379),            new JedisShardInfo("localhost",6380));    ShardedJedis sharding = new ShardedJedis(shards);    long start = System.currentTimeMillis();    for (int i = 0; i < 100000; i++) {        String result = sharding.set("sn" + i, "n" + i);    }    long end = System.currentTimeMillis();    System.out.println("[email protected] SET: " + ((end - start)/1000.0) + " seconds");    sharding.disconnect();}

    This is a distributed direct connection, and is a synchronous call, with each step returning execution results. Similarly, there are asynchronous pipeline calls.

    Vi. distributed, direct-attached asynchronous invocation
    @Testpublic void test6shardpipelined() {    List<JedisShardInfo> shards = Arrays.asList(            new JedisShardInfo("localhost",6379),            new JedisShardInfo("localhost",6380));    ShardedJedis sharding = new ShardedJedis(shards);    ShardedJedisPipeline pipeline = sharding.pipelined();    long start = System.currentTimeMillis();    for (int i = 0; i < 100000; i++) {        pipeline.set("sp" + i, "p" + i);    }    List<Object> results = pipeline.syncAndReturnAll();    long end = System.currentTimeMillis();    System.out.println("[email protected] SET: " + ((end - start)/1000.0) + " seconds");    sharding.disconnect();}
    Vii. Distributed Connection Pool synchronous invocation

    If your distributed calling code is running in the thread, then the above two direct connect calls are inappropriate because the direct connection is non-thread-safe, and at this point you will have to select the Connect pool call.

    @Testpublic void test7shardSimplePool() {    List<JedisShardInfo> shards = Arrays.asList(            new JedisShardInfo("localhost",6379),            new JedisShardInfo("localhost",6380));    ShardedJedisPool pool = new ShardedJedisPool(new JedisPoolConfig(), shards);    ShardedJedis one = pool.getResource();    long start = System.currentTimeMillis();    for (int i = 0; i < 100000; i++) {        String result = one.set("spn" + i, "n" + i);    }    long end = System.currentTimeMillis();    pool.returnResource(one);    System.out.println("[email protected] SET: " + ((end - start)/1000.0) + " seconds");    pool.destroy();}

    The above is synchronous, and of course there are asynchronous methods.

    Viii. Distributed Connection Pool asynchronous invocation
    @Testpublic void test8shardPipelinedPool() {    List<JedisShardInfo> shards = Arrays.asList(            new JedisShardInfo("localhost",6379),            new JedisShardInfo("localhost",6380));    ShardedJedisPool pool = new ShardedJedisPool(new JedisPoolConfig(), shards);    ShardedJedis one = pool.getResource();    ShardedJedisPipeline pipeline = one.pipelined();    long start = System.currentTimeMillis();    for (int i = 0; i < 100000; i++) {        pipeline.set("sppn" + i, "n" + i);    }    List<Object> results = pipeline.syncAndReturnAll();    long end = System.currentTimeMillis();    pool.returnResource(one);    System.out.println("[email protected] SET: " + ((end - start)/1000.0) + " seconds");    pool.destroy();}
    Nine, the need to pay attention to the place
      1. Both the transaction and the pipeline are asynchronous patterns. Query results cannot be synchronized in transactions and pipelines. For example, the following two calls are not allowed:

         Transaction tx = jedis.multi(); for (int i = 0; i < 100000; i++) {     tx.set("t" + i, "t" + i); } System.out.println(tx.get("t1000").get());  //不允许 List<Object> results = tx.exec(); … … Pipeline pipeline = jedis.pipelined(); long start = System.currentTimeMillis(); for (int i = 0; i < 100000; i++) {     pipeline.set("p" + i, "p" + i); } System.out.println(pipeline.get("p1000").get()); //不允许 List<Object> results = pipeline.syncAndReturnAll();
      2. Both the transaction and the pipeline are asynchronous, and the individual feels that it is not necessary to make a transaction call in the pipeline, instead of directly doing the transaction pattern.

      3. In distributed, the performance of the connection pool is slightly better than that of direct connect (see the next Test section).

      4. Transactions are not supported in distributed calls.

        Because transactions are implemented on the server side, and in the distributed, each batch of call objects can access different machines, so the transaction cannot be performed.

    Ten, testing

    Run the above code, test it, and the result is as follows:

    Simple SET: 5.227 secondsTransaction SET: 0.5 secondsPipelined SET: 0.353 secondsPipelined transaction: 0.509 seconds[email protected] SET: 5.289 seconds[email protected] SET: 0.348 seconds[email protected] SET: 5.039 seconds[email protected] SET: 0.401 seconds

    In addition, the more machines tested for distribution, the slower the calls will be. The top is 2 pieces, the following is 5 pieces:

    [email protected] SET: 5.494 seconds[email protected] SET: 0.51 seconds[email protected] SET: 5.223 seconds[email protected] SET: 0.518 seconds

    Here are 10 pieces:

    [email protected] SET: 5.9 seconds[email protected] SET: 0.794 seconds[email protected] SET: 5.624 seconds[email protected] SET: 0.762 seconds

    Here are 100 pieces:

    [email protected] SET: 14.055 seconds[email protected] SET: 8.185 seconds[email protected] SET: 13.29 seconds[email protected] SET: 7.767 seconds

    In distributed, connection pooling method calls are not only thread-safe, but also, according to the test data above, connection pooling is more efficient than direct connect.

    Xi. Complete Test Code
    Package Com.example.nosqlclient;import Java.util.arrays;import Java.util.list;import org.junit.afterclass;import Org.junit.beforeclass;import Org.junit.test;import Redis.clients.jedis.jedis;import Redis.clients.jedis.jedispoolconfig;import Redis.clients.jedis.jedisshardinfo;import Redis.clients.jedis.pipeline;import Redis.clients.jedis.shardedjedis;import Redis.clients.jedis.shardedjedispipeline;import Redis.clients.jedis.shardedjedispool;import Redis.clients.jedis.transaction;import Org.junit.fixmethodorder;import org.junit.runners.methodsorters;@    Fixmethodorder (methodsorters.name_ascending) public class Testjedis {private static Jedis Jedis;    private static Shardedjedis sharding;    private static Shardedjedispool pool; @BeforeClass public static void Setupbeforeclass () throws Exception {list<jedisshardinfo> shards = Arrays . Aslist (New Jedisshardinfo ("localhost", 6379), new Jedisshardinfo ("localhost", 6379));    Use the same ip:port for testing only    Jedis = new Jedis ("localhost");        sharding = new Shardedjedis (shards);    Pool = new Shardedjedispool (new Jedispoolconfig (), shards);        } @AfterClass public static void Teardownafterclass () throws Exception {Jedis.disconnect ();        Sharding.disconnect ();    Pool.destroy ();        } @Test public void Test1normal () {Long start = System.currenttimemillis ();        for (int i = 0; i < 100000; i++) {String result = Jedis.set ("n" + I, "n" + i);        } Long end = System.currenttimemillis ();    System.out.println ("Simple SET:" + ((End-start)/1000.0) + "seconds");        } @Test public void Test2trans () {Long start = System.currenttimemillis ();        Transaction tx = Jedis.multi ();        for (int i = 0; i < 100000; i++) {Tx.set ("T" + I, "T" + i);        }//system.out.println (Tx.get ("t1000"). get ());        list<object> results = tx.exec (); Long end = System.currenttimemilLis ();    System.out.println ("Transaction SET:" + ((End-start)/1000.0) + "seconds");        } @Test public void test3pipelined () {Pipeline Pipeline = jedis.pipelined ();        Long start = System.currenttimemillis ();        for (int i = 0; i < 100000; i++) {Pipeline.set ("P" + I, "P" + i);        }//system.out.println (Pipeline.get ("p1000"). get ());        list<object> results = Pipeline.syncandreturnall ();        Long end = System.currenttimemillis ();    System.out.println ("pipelined SET:" + ((End-start)/1000.0) + "seconds");        } @Test public void Test4combpipelinetrans () {Long start = System.currenttimemillis ();        Pipeline Pipeline = jedis.pipelined ();        Pipeline.multi ();        for (int i = 0; i < 100000; i++) {Pipeline.set ("" + I, "" + i);        } pipeline.exec ();        list<object> results = Pipeline.syncandreturnall ();        Long end = System.currenttimemillis (); SysTem.out.println ("pipelined Transaction:" + ((End-start)/1000.0) + "seconds");        } @Test public void Test5shardnormal () {Long start = System.currenttimemillis ();        for (int i = 0; i < 100000; i++) {String result = Sharding.set ("sn" + I, "n" + i);        } Long end = System.currenttimemillis ();    System.out.println ("[email protected] SET:" + ((End-start)/1000.0) + "seconds");        } @Test public void test6shardpipelined () {Shardedjedispipeline pipeline = sharding.pipelined ();        Long start = System.currenttimemillis ();        for (int i = 0; i < 100000; i++) {Pipeline.set ("sp" + I, "P" + i);        } list<object> results = Pipeline.syncandreturnall ();        Long end = System.currenttimemillis ();    System.out.println ("[email protected] SET:" + ((End-start)/1000.0) + "seconds"); } @Test public void Test7shardsimplepool () {Shardedjedis one = Pool.getresourcE ();        Long start = System.currenttimemillis ();        for (int i = 0; i < 100000; i++) {String result = One.set ("spn" + I, "n" + i);        } Long end = System.currenttimemillis ();        Pool.returnresource (one);    System.out.println ("[email protected] SET:" + ((End-start)/1000.0) + "seconds");        } @Test public void Test8shardpipelinedpool () {Shardedjedis one = Pool.getresource ();        Shardedjedispipeline pipeline = one.pipelined ();        Long start = System.currenttimemillis ();        for (int i = 0; i < 100000; i++) {Pipeline.set ("sppn" + I, "n" + i);        } list<object> results = Pipeline.syncandreturnall ();        Long end = System.currenttimemillis ();        Pool.returnresource (one);    System.out.println ("[email protected] SET:" + ((End-start)/1000.0) + "seconds"); }}
http://blog.csdn.net/zcgsdu/article/details/46912739

Eight ways to call Redis's Java client Jedis (transactional, pipeline, distributed ...) Introduction (GO)

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.