High concurrency under Unique order number generator 16-digit order number

Source: Internet
Author: User

The only order number generation thinking in high concurrency.

Order No. 3 properties: 1. Uniqueness  2. Non-speculative 3. Efficiency
Optional Option One
This scenario uses the current time, including the number of milliseconds, the number of nanoseconds, the database does not need to participate in the calculation, performance needless to say.

public static string GenID (String MachineID) {
    string orderId =
            MachineID +
                    (system.currenttimemillis () + "" ). substring (1) +
                    (system.nanotime () + ""). Substring (7, ten);
    System.out.println (orderId);
    return orderId;
}
Explain:
Parameter MachineID: is the machine code of the cluster, can be 1-9 arbitrary. At deployment time, manually modify this value for the deployed project to ensure that multiple machines in the cluster are inconsistent on the system time (there is no doubt that the number of milliseconds per machine is basically not the same).
Parameter System.currenttimemillis (): This is the number of milliseconds in Java that gets from 1970 to the present, is a 13-digit number, like the result of the Date.gettime () function, such as 1378049585093. After study, in 2013, the first three digits were 137, 2023 was 168, and 2033 was only 199. So, I decided the first digit 1 can be removed, not occupy the position. It is certain that most systems will not be used for 10 years and 20 years. In this way, parameter 2 becomes a 12-digit number, plus the parameter 1machineId is only 13 digits.
Parameter System.nanotime (): This is the number of nanoseconds in Java, after a thorough study, in the same millisecond, position 7,8,9 These three numbers will change. So I decided to intercept the three numbers and stitch them up into a 16-digit order number.
Summary: Theoretically, this scheme can deal with 1000*1000 order number in the same second, but after testing, there will be 2-10 repetitions at 2000 per second.

Optional Option II
This scenario is based on UUID.
A UUID is a number generated on a machine that guarantees that all machines in the same time and space are unique, and that this non-repetition is known to all the people of the world. Of course, since the string value is not duplicated, the corresponding hashcode is the same, not duplicated.
Algorithm: Java code orderid= machineid+ uuid.randomuuid (). toString (). Hashcode ();



Explain:
Parameter 1 is no longer explained.
Parameter 2 is the value that generates the UUID and then takes its hashcode value, tested, without a problem at all. You can open the 1000w concurrent to test the insertion bar, as long as the database does not report a unique error, then no problem.
Summarize:
Hashcode This algorithm from the start of software to now so many years, has not been put in handy, this time greatly used. Resolved the problem. Please make good use of this thing in the future.
5. Appendix: Algorithm Code of program two

public static string Getorderidbyuuid (String machineid) {

    int hashcodev = Uuid.randomuuid (). toString (). Hashcode ();
    if (Hashcodev < 0) {//There may be negative
        Hashcodev =-hashcodev;
    }
    0 for the preceding supplement 0//4 for the
    length 4
    //d for the parameter is positive
    String Orderid=machineid + string.format ("%015d", Hashcodev);
    System.out.println (orderId);
    return orderId;
}


Scenario Two is actually a function, very simple.

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.

Tags Index: