[High concurrency solution] Jedis processes pipelines, transactions, and watches to cope with high concurrency. jediswatch

Source: Internet
Author: User

[High concurrency solution] Jedis processes pipelines, transactions, and watches to cope with high concurrency. jediswatch

For an Internet platform, high concurrency is a common scenario. The most representative are seckilling and flash sales. High concurrency has three features:

1. High-concurrency reading

2. High-concurrency write (consistency)

3. An overselling problem occurs.

How should the front-end respond?

1. cache static data, examples, html pages, js, etc.

2. Build a Server Load balancer cluster. Currently, nginx is widely used.

3. restrict the number of requests initiated by the same ip address per unit time. Or create an ip blacklist to avoid malicious attacks.

4. Consider system degradation. For example, when the system load is reached, a static processing page is returned.

How does the backend respond?

1. mysql read/write splitting is adopted, but mysql performance will decrease when high concurrency occurs. In general, the processing performance of MySQL will increase with the increase of concurrent threads, but after a certain degree of concurrency, there will be a significant inflection point, and then all the way down, in the end, the performance may be worse than that of a single thread. For example, for operations on stock addition and subtraction, the usual method of low concurrency is: update xxx set count = count-xx where curcount> xx; in this way, mysql transaction locks can be fully utilized to avoid overselling. However, when the concurrency is reached, the performance will be greatly reduced due to exclusive lock wait.

2. Use the redis database and move it to mysql. The idea is as follows:

2.1 After the system is started, initialize the sku information to the redis database and record the available quantity and lock quantity.

2.2 Use optimistic locks and the redis watch mechanism. Logic:

1. Define the variable ticket number and set the initial value to 0. Watchkey

2. watch this variable, watch (watchkey );

3. Use the redis transaction to add or subtract inventory. First, compare the available quantity with the purchased quantity. If curcount> buycount, perform the inventory reduction and lock quantification operations normally:

 

Redis Usage Details

1. Pipeline

Using pipeline to Package Multiple commands from the client and send them together, you do not need to wait for the response of a single command to return, the Redis server will pack the processing results of multiple commands and return them to the client after processing multiple commands. Therefore, pipeline is suitable for batch processing jobs to improve efficiency, for example:

 

[Java]View plain copy print?
  1. Public static void testMget (){
  2. Jedis jedis = RedisCacheClient. getpolicance (). getClient ();
  3. Set <String> keys = jedis. keys ("cvfeedBackHandl _*");
  4. List <String> result = Lists. newArrayList ();
  5. Long t1 = System. currentTimeMillis ();
  6. For (String key: keys ){
  7. Result. add (jedis. get (key ));
  8. }
  9. For (String src: result ){
  10. System. out. println (src );
  11. }
  12. System. out. println (System. currentTimeMillis ()-t1 );
  13. }
  14. Public static void testPipline (){
  15. Jedis jedis = RedisCacheClient. getpolicance (). getClient ();
  16. Set <String> keys = jedis. keys ("cvfeedBackHandl _*");
  17. List <Object> result = Lists. newArrayList ();
  18. Pipeline pipelined = jedis. pipelined ();
  19. Long t1 = System. currentTimeMillis ();
  20. For (String key: keys ){
  21. Pipelined. <span style = "font-family: Arial;"> get </span> ("testabcd ");
  22. }
  23. Result = pipelined. syncAndReturnAll ();
  24. For (Object src: result ){
  25. System. out. println (src );
  26. }
  27. System. out. println (System. currentTimeMillis ()-t1 );
  28. }

For example, the execution time of the first method is 82 ms.

 

The execution time of the second method is 9 ms.

Note: Both pipeline and transactions return results asynchronously, that is, they do not wait for the result to be returned immediately after each command is executed, but return the result after all commands are executed. Pipelined. syncAndReturnAll () returns the results of each command involved in packaging and execution. If it is changed:

 

[Java]View plain copy print?
  1. For (String key: keys) {// The length of keys is 5
  2. Pipelined. get (key );
  3. Pipelined. del ("testabcd ");
  4. }

The returned result will be

 

 

[Java]View plain copy print?
  1. "Test1"
  2. 1
  3. "Test2"
  4. 0
  5. "Test2"
  6. 0
  7. "Test4"
  8. 0
  9. "Test5"
  10. 0


2. Transactions

 

Commit c () commits a transaction. If the execution is successful, the returned result is the same as that of pipeline, if there are two commands in the transaction, the exec return value of the transaction will combine the return values of the two commands and return them together. If the transaction is canceled, null is returned.

3. watch

It is generally used together with the transaction. After a key is watched, if other clients change the key, the transaction will be canceled and the exec of the transaction will return null. Jedis. watch (key) returns OK
Eg:

 

[Java]View plain copy print?
  1. Public static void testWach (){
  2. Jedis jedis = RedisCacheClient. getpolicance (). getClient ();
  3. String watch = jedis. watch ("testabcd ");
  4. System. out. println (Thread. currentThread (). getName () + "--" + watch );
  5. Transaction multi = jedis. multi ();
  6. Multi. set ("testabcd", "23432 ");
  7. Try {
  8. Thread. sleep (3000 );
  9. } Catch (InterruptedException e ){
  10. E. printStackTrace ();
  11. }
  12. List <Object> exec = multi.exe c ();
  13. System. out. println ("---" + exec );
  14. Jedis. unwatch ();
  15. }
  16. Public static void testWatch2 (){
  17. Jedis jedis = RedisCacheClient. getpolicance (). getClient ();
  18. String watch = jedis. watch ("testabcd2 ");
  19. System. out. println (Thread. currentThread (). getName () + "--" + watch );
  20. Transaction multi = jedis. multi ();
  21. Multi. set ("testabcd", "125 ");
  22. List <Object> exec = multi.exe c ();
  23. System. out. println ("--->" + exec );
  24. }

Thread-2 -- OK
Thread-0 -- OK
---> [OK]

--- Null // cancel the transaction

4. Transactions and Pipelines

When you watch a key, if another client changes the key, you can cancel the transaction, but the pipeline is not.

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.