Order number generation strategy in distributed environment

Leaf - a distributed ID generation system from Meituan Dianping



 

Description of important fields: biz_tag is used to distinguish services, max_id represents the maximum value of the ID number segment currently allocated to the biz_tag, and step represents the length of the number segment allocated each time. It used to be that you need to write the database every time to get the ID. Now you only need to set the step to be large enough, such as 1000. Then only when 1000 numbers are consumed will the database be read and written again.

 

test_tag is the number segment from 1 to 1000 on the first Leaf machine. When this number segment is used up, another number segment with a length of step=1000 will be loaded. Assuming that the other two number segments have not been updated, this time The newly loaded number segment of the first machine should be 3001~4000. At the same time, the max_id of the data corresponding to the biz_tag in the database will be updated from 3000 to 4000. The SQL statement to update the number segment is as follows:

Begin
UPDATE table SET max_id=max_id+step WHERE biz_tag=xxx
SELECT tag, max_id, step FROM table WHERE biz_tag=xxx
Commit

This mode has the following advantages and disadvantages:

advantage:

  • Leaf service can be easily extended linearly, and its performance can fully support most business scenarios.

  • The ID number is a 64-bit number of 8 bytes that is increasing in trend, and meets the primary key requirements for the above-mentioned database storage.

  • High disaster tolerance: The Leaf service has an internal number segment cache. Even if the DB goes down, Leaf can still provide services normally in a short period of time.

  • You can customize the size of max_id, which is very convenient for businesses to migrate from the original ID method.

shortcoming:

  • The ID number is not random enough, it can reveal the information of the number issued, and it is not very secure.

  • The TP999 data fluctuates greatly. When the number segment is used up, it will still hang on the I/O of updating the database, and the tg999 data will have occasional spikes.

  • DB downtime can make the entire system unavailable.

Optimization plan: 1. Do not fetch a new number segment when the number segment is exhausted. For example, if the number segment consumes 70%, then load the new number segment.

 

                 2. Add a random number to the end of the obtained number segment

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326443026&siteId=291194637