Java Distributed Concurrency unique key conflict solution Summary

Source: Internet
Author: User

When processing concurrency on a single web server, we can use the keyword sychronized or related concurrent class libraries for convenient processing.

In the case of multiple Web servers, that is, Distributed Concurrency, the above processing method will not work, because there are multiple JVMs, the above method will become invalid.

Let's consider a scenario like this:

  • Business Background: Hold a lecture, and take the online ticketing line number, the number cannot be repeated, and the lecture ID and number form a unique key, such as the course table: ID, seatnum, other fields
  • Environment: multiple Web servers provide services
To ensure the normal operation of the above services, concurrency may occur, and the numbers grabbed by multiple people are the same. In this way, an exception will be thrown during database storage because a unique key is set and duplicate numbers are not allowed. So what can be done well?
  1. You can use the table lock method. The pessimistic lock method can solve this problem well, but the efficiency is not high, and there may be deadlocks. The optimistic lock is used, that is, when an exception is thrown during program implementation, it will continue to be processed. This method is more violent and does not solve this problem well.
  2. Add a table course_extra, and seatnum as the auto-incrementing primary key, course_id, etc. Insert a data record directly to each sub-number to ensure that there will be no duplicates in each auto-incrementing seatnum.
What if there are multiple lectures and the number of each lecture starts from 1? When an additional table is used, do you need to create an additional table for each lecture? This is a waste of database space.
  1. In this way, you can create another table course_sequence: ID, current_val, increment. Each time you obtain the value of current_val, you can obtain the value of current_val and use self-built functions such as next_val or stored procedure, each time you obtain the current value, the next value is automatically obtained to update the current value. It is similar to the transaction processing method and cannot be split for execution. Otherwise, concurrency also exists, leading to repetition.
  2. Using distributed cache and the built-in atomic increment method can solve this problem well. To prevent cache invalidation and other problems, you need to read and write the database regularly to ensure that the number is the largest. Otherwise, the cache will face similar concurrency problems once it becomes invalid. Note: The string type must be used for atomic increments, and the int type must be converted to the string type.
In conclusion, memcache is used to solve various problems in practical applications.

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: [email protected] 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.