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 are the common schemes for distributed unique ID generation

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

Share

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

Distributed unique ID generation commonly used solutions, I believe that many inexperienced people are helpless about this, this article summarizes the causes and solutions of the problem, through this article I hope you can solve this problem.

1. UUID generation using JAVA

The core idea of the algorithm is to combine the network card of the machine, the local time, and a random number to generate a UUID.

Advantages: Local generation, simple generation, good performance, no high availability risk

Disadvantages: long length, combination of letters and numbers, storage redundancy, and disorder unreadable, low query efficiency

2. Database self-increment ID

Use database id self-increment strategy, such as MySQL auto_increment, oracle sequence. In addition, two databases can be used to set different step sizes to generate a policy of non-duplicate ID to achieve high availability.

Advantages: IDs generated by the database are absolutely ordered, and the implementation of high availability is simple.

Disadvantages: need to deploy database instances independently, high cost, real-time operation of the database, there are performance bottlenecks in large concurrency

3. Database + program (batch ID)

Multiple IDs are generated on demand in batches at a time. Each generation requires accessing the database, modifying the database to the maximum ID value, and recording the current value and the maximum value in memory.

Benefits: Avoid the stress of accessing the database every time an ID is generated, improving performance

Disadvantages: Locally generated policy, there is a single point of failure, if the server goes down, restart the service causes ID discontinuity

4. Redis Generation ID

All command operations in Redis are single-threaded and provide self-increasing atomic commands such as incr and increby, so it is guaranteed that the generated ID must be uniquely ordered.

Advantages: Does not depend on the database, flexible and convenient, and performance is better than the database; digital ID natural sorting, paging or need to sort the results are very helpful.

Disadvantages: If there is no Redis in the system, new components need to be introduced, increasing the complexity of the system; the workload required for coding and configuration is relatively large.

Given the performance bottleneck of a single node, Redis clustering can be used to achieve higher throughput. Suppose there are three Redis in a cluster. You can initialize each Redis to 1, 2, 3 and then step 3. The ID generated by each Redis is:

A:1, 4, 7, 10, 13B:2, 5, 8, 11, 14C:3, 6, 9, 12, 15

Any load to which machine is determined well, it is difficult to make modifications in the future. Step size and initial values must be determined beforehand. Using Redis clusters can also approach single point failure problems.

In addition, it is more suitable to use Redis to generate serial numbers starting from 0 every day. For example, order number = date + self-growth number of the day. A Key can be generated daily in Redis and accumulated using INCR.

5. MongoDB generates IDs

MongoDB ObjectId is similar to snowflake algorithm. It is designed to be lightweight, and different machines can easily generate it in a globally unique way. MongoDB was designed from the beginning as a distributed database, and handling multiple nodes was a core requirement. Makes it much easier to generate in a sharded environment.

6. Some other commonly used programs

Baidu uid generator: github.com/baidu/uid-generator

Meituan Review Generator: tech.meituan.com/MT_Leaf.html

zookeeper generates unique UUID

Snowflake algorithm generates UUID

After reading the above content, do you know what methods are commonly used in distributed unique ID generation? If you still want to learn more skills or want to know more related content, welcome to pay attention to the industry information channel, thank you for reading!

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