Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to design an order number correctly

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the relevant knowledge of "how to correctly design an order number". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Order number definition

Most of the order numbers we often mention are unique identification strings in e-commerce shopping scenarios. In fact, the order number does not just refer to the e-commerce system, as long as we need such a business scenario, we can use the order number mode to deal with. For example, our provincial certificate number requires unique readability and other characteristics, it can also be understood as an order number.

Order number rule

1. No repetition. No matter how complex or simple your order number is designed, the first thing we need to make sure is that the order number is unique in a system.

two。 Security. The order number needs to be made so that it is not easy to guess or speculate. For example, the order number contains system pipelining information, user information and other confidential related information.

3. Disable the random code. To some extent, random codes are safer, more non-repetitive, but less readable. For example, generating random codes like this (sdfsad12312sfsdf201), whether read from a system point of view or from an artificial point of view, is completely impossible to distinguish directly.

4. Prevent concurrency. In view of the system's concurrent business scenarios (such as second kill), it is necessary to achieve the requirements of fast generation and non-repetition of order numbers in concurrent scenarios.

5. Control the number of bits. The order number should be between 10 and 20 digits as far as possible. In the case of too short, if the trading volume is too large, it is difficult to prevent repetition, and too long is not readable and meaningful.

6. Try to use numbers. From the point of view of software, the order number of digital storage has the advantages of small space occupation and fast retrieval.

Taobao rules

The screenshot above shows the order number of an individual recharging on Taobao, and only a few have been captured here. In the actual process, it is found that all order numbers have a similar feature (red box). Personal guess, this should be related to the buyer's information, such as the buyer's ID number.

The following rules are the rules for the generation of Taobao order numbers (for reference only, there is a gap with the actual).

1. A total of 18.

two。 The first 14 digits are serial numbers.

3.15-16 sellers 1-2 from the bottom of ID.

The bottom 3-4 of the 17-18 buyers of ID.

The above rules have certain reference significance. From points 3 and 4, it is not difficult to analyze that to achieve the order number in this way, it is difficult to have duplicate order numbers to a certain extent. And why is that?

1. Both the seller's ID and the buyer's ID's are generated before the order is placed and are unique. Because these two ID are generated in advance, even if there are concurrent scenarios, it is difficult to generate duplicate order numbers through the unique identification of these two groups.

two。 To a large extent, it satisfies the situation of repeated order numbers in some business scenarios with high concurrency. You may consider scenarios like Singles Day, where most systems are unable to achieve such business scenarios.

Generation mode

I mentioned the generated rules earlier, so how would it be better to implement such rules? Here are a few common ways to deal with it.

UUID

UUID is the abbreviation of Universally Unique Indentifier, translated into universal unique identification code. As the name implies, UUID is used to uniquely identify a piece of data and record. It is calculated according to the standard specified by the Open Software Foundation (OSF), using Ethernet card address (MAC), nanosecond time, chip ID code and many possible numbers.

Generally speaking, the UUID code consists of the following three parts:

1. Current date and time.

two。 Clock sequence.

3. Globally unique IEEE machine identification number (how to have a network card, get it from a network card, and get it without a network card in other ways).

The standard form of UUID contains 32 hexadecimal digits, divided into five segments by a hyphen, and 32 characters in the form of 8-4-4-4-12. Example: 550e8400-e29b-41d4-a716-446655440000. For more information about UUID, you can refer to this article

Snowflake algorithm

Snowflake is an ID generation algorithm within Twitter, which can be guaranteed to generate unique ID numbers in large-scale distribution through some simple rules. Its composition is as follows:

The first bit is an unused symbol bit.

The second part consists of a 41-bit timestamp (milliseconds), whose value is the offset of the current time from a certain time.

The five bit bits in part 3 and part 4 represent the data center and machine ID, and the maximum value they can represent is 2 ^ 5-1 = 31.

The last part consists of 12 bit, which represents the sequence number ID generated by each worker node per millisecond, which can generate up to 2 ^ 12-1 or 4095 ID in the same millisecond.

It is important to note that:

In a distributed environment, 5 bit bits of datacenter and worker indicate that up to 31 data centers can be deployed and up to 31 nodes can be deployed per data center.

The 41-bit binary length can represent up to 2 ^ 41-1 milliseconds, that is, 69 years, so the snowflake algorithm can be used for up to 69 years. In order to maximize the use of the algorithm, you should specify a start time for it.

Database self-increment

In the database, you can set the order column as a self-incrementing column and set an initial value for that column. In this way, the order is self-increasing and no repetition is realized through the database. However, the ability to achieve concurrency through the database is low, there is only one self-increasing column in a single table, and the sub-table processing of the data in the later stage is not friendly enough.

Distributed component

Through the way of distributed components, we can also achieve the processing of order numbers. For example, Redis is used as a distributed component. Through the queue of Redis, incr and other functions to achieve.

Demonstration of UUID mode / / generation mode one by example

Function uuid ($prefix ='') {

$chars = md5 (uniqid (mt_rand (), true))

$uuid = substr ($chars,0,8). '-'

$uuid. = substr ($chars,8,4). '-'

$uuid. = substr ($chars,12,4). '-'

$uuid. = substr ($chars,16,4). '-'

$uuid. = substr ($chars,20,12)

Return $prefix. $uuid

}

Echo uuid ()

/ / output

/ / b2fa188c-23a8-d1b6-432d-649db4eb34c7

/ / Generation method 2 (using uuid generated within Linux)

Echo exec ("cat / proc/sys/kernel/random/uuid")

/ / output

/ / b2792783-7c9f-43d0-8d31-38411e17fc2f

/ / Generation method 3

Function uniqidReal ($lenght = 13) {

If (function_exists ("random_bytes")) {

Try {

$bytes = random_bytes (ceil ($lenght / 2))

} catch (Exception $e) {

}

} elseif (function_exists ("openssl_random_pseudo_bytes")) {

$bytes = openssl_random_pseudo_bytes (ceil ($lenght / 2))

} else {

Throw new Exception ("no cryptographically secure random function available")

}

Return substr (bin2hex ($bytes), 0, $lenght)

}

Echo uniqidReal ()

/ / output

/ / 9f39aa0ecd89d

Snowflake algorithm require_once _ DIR__.'/vendor/autoload.php'

$snowflake = new\ Godruoyi\ Snowflake\ Snowflake

Echo $snowflake- > id ()

/ / output

/ / 199778615951360000

/ / for more advanced usage and implementation principles, please refer to the original warehouse: https://github.com/godruoyi/php-snowflake/blob/master/README-zh_CN.md

Redis implements / / connects to Redis

$redis = new\ Redis ()

$redis- > connect ('192.168.0.112, 6379)

$cacheKey = date ('YRV mpurd')

$initVal = 10000

/ / implementation method 1 (using queues)

For ($I = 0; $I

< 10; $i++) { $redis->

LPush ($cacheKey, $initVal + $I)

}

$redis- > rPop ($cacheKey)

/ / implementation method 2 (using incr)

If ($redis- > get ($cacheKey)) {

/ / return the newly added value

Return $redis- > incr ($cacheKey)

} else {

/ / set a default initial value

$redis- > set ($cacheKey, $initVal)

Return $cacheKey

}

Database implementation

The database does not demonstrate directly, just by setting the table field properties. It mainly sets the initial value and offset of the field value.

Mysql root@127.0.0.1: (none) > show variables like'% auto_incr%'

+-+ +

| | Variable_name | Value |

+-+ +

| | auto_increment_increment | 1 | |

| | auto_increment_offset | 1 | |

+-+ +

2 rows in set

Time: 0.012s

Total and analysis

Through the demonstration of the above example, the following is an analysis and summary of these situations. Choose a reasonable way as much as possible.

To achieve the advantages and disadvantages of the scheme, UUID is simple and convenient to implement; low repeatability; low database query efficiency and low readability; too long snowflake algorithm based on memory and high speed; high performance; no additional network overhead; data increasing in turn depends on server time, such as changing server time, Redis is based on memory and speed library; easy to use Distributed data, strong expansibility need to build a set of services independently, which increases the maintenance cost; cross-application calls, the existence of network overhead database self-increasing code level does not need any special processing; the use of MySQL characteristics to achieve data incremental concurrency performance is poor; MySQL burden "how to correctly design an order number" is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report