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

What is the solution of distributed ID generator

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

What is the solution of distributed ID generator? in order to solve this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

Generally speaking, an ID has the following elements:

Uniqueness: ensure that the generated ID is unique across the network.

Orderly increment: make sure that the generated ID is incremented sequentially according to a certain number for a user or business.

High availability: ensure that ID is generated correctly at all times.

Time: time is included in ID, and you can tell which day's trade will be made at a glance.

System time milliseconds

We can use the current system time to accurate to milliseconds + business attributes + user attributes + random numbers +. The other parameter combination form ensures the uniqueness of ID, but the disadvantage is that it is difficult to guarantee the order of ID, which depends on the database or other intermediate storage media.

UUID

Java's own way of generating UUID can generate a string of unique random 32-bit length data, and it will last us 100 million years, so it must be needless to say to ensure uniqueness, but the disadvantage is that it does not include time, the readability of business data is too poor, and it cannot be sequentially incremented by ID.

This is a simple generation method, simple and efficient, but I have never seen it in a general business system.

Database self-increasing ID

We all know that setting a self-increasing sequence number for the database primary key will increase itself with a certain trend to ensure the uniqueness of the primary key ID.

This scheme is very simple, but the main problem is to rely on the database itself, which invisibly increases the access pressure and dependence on the database. Once the database is divided into tables or data migration, it will be awkward.

Therefore, this is not the appropriate method for ID generation.

Batch generation of ID

Multiple ID are generated in batches on demand at a time. Each generation requires accessing the database, modifying the database to the maximum ID value, and recording the current and maximum values in memory. This avoids the pressure of accessing the database every time an ID is generated.

This kind of solution service is a single point, and if the service is restarted, it will inevitably cause ID loss and discontinuity, and this way is not conducive to horizontal expansion.

Middle ware

All command operations in Redis are single-threaded and provide self-incrementing commands such as incr itself, so it is guaranteed that the generated ID must be unique and orderly.

This approach does not rely on relational databases and is fast. However, the middleware Redis should be introduced into the system to increase the maintenance cost, and the workload of coding and configuration is relatively large. Even if you already have Redis components, the high frequency of access to generating ID is bound to have an impact on single-threaded Redis performance.

You can also use versions of znode data such as Zookeeper to generate serial numbers, and ObjectId of MongoDB, which is not recommended.

Snowflake algorithm

As shown in the figure above, Twitter's snowflake algorithm consists of the following parts:

41-bit time series, accurate to milliseconds, can be used for 69 years

A 10-bit machine ID that supports the deployment of up to 1024 nodes

A 12-bit sequence number that supports each node to generate 4096 ID sequence numbers per millisecond. The highest bit is that the symbol bit is always 0.

This scheme has good performance and is incremental on a single machine, but because of the distributed environment, it is impossible to fully synchronize the clock on each machine, and sometimes it may not be global increment.

And the project stopped maintenance in 2010, but this design idea still applies to other ID generators and variants.

UidGenerator

UidGenerator is Baidu's open source distributed ID generator. Based on the implementation of the snowflake algorithm, it looks OK. However, the maintainability of domestic open source projects is really worrying.

Leaf

Leaf is Meituan's open source distributed ID generator, which can ensure global uniqueness, increasing trend, monotonous increasing, and information security. It also mentions the comparison of several distributed schemes, but it also needs to rely on relational database, Zookeeper and other middleware.

The answer to the question about the solution of the distributed ID generator is shared here. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

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

Internet Technology

Wechat

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

12
Report