In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "what is the unique ID generation strategy in the database". In the daily operation, I believe that many people have doubts about the unique ID generation strategy in the database. The editor has consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "what is the unique ID generation strategy in the database?" Next, please follow the editor to study!
Database self-increasing ID
The easiest way to do this is to use the database's id self-increment strategy, such as MySQL's auto_increment. If the two databases set different step sizes, you can generate ID that does not repeat, thus achieving high availability.
Advantages
The implementation is simple, easy to understand, monotonous and self-increasing, and absolutely orderly.
Shortcoming
Strongly dependent on DB, the whole system is not available when DB is abnormal, which is a fatal problem.
The bottleneck of ID numbering performance is limited to the read and write performance of a single MySQL.
UUID series
Combine the machine's network card, local time, and a random number to generate UUID. The existence of some variants of UUID is also a good implementation.
Advantages
Local generation, simple generation, very good performance, high availability.
Shortcoming
The length is too long, not easy to store, unordered and unreadable, and the query efficiency is low.
The information is not secure, and algorithms for generating UUID based on MAC addresses may cause MAC address leaks, a vulnerability that has been used to find the location of the maker of the Melissa virus.
The disorder of UUID may cause frequent changes in data location and seriously affect performance.
Implementing ID with Redis
All command operations of Redis are single-threaded and provide self-incrementing atomic commands such as incr and increby itself, so it is guaranteed that the generated ID must be unique and orderly.
For example, use Redis to generate a serial number starting at 0 every day. For example, order number = date + same-day self-growth number. You can generate a Key in Redis every day and accumulate it using INCR.
Advantages
Flexible and convenient, and the performance is better than the database.
Digital ID is naturally sorted, and a more expressive ID can be obtained through reasonable design.
Shortcoming
With the introduction of Redis, the workload of coding and configuration is relatively large.
If the ID is continuous, it is very easy for malicious users to pick up the URL, just download the specified URL in order; if it is the order number, it is even more dangerous, and the pairing can directly know the quantity of our order in a day. Therefore, in some application scenarios, ID will be required to be irregular and irregular.
Advantages of Twitter's snowflake algorithm for generating ID
The time sequence is orderly, the millisecond is high, the self-increasing sequence is low, and the whole ID is increasing.
Do not rely on databases and other third-party systems, deployed in the way of services, higher stability, the performance of generating ID is also very high.
Bit bits can be allocated according to their own business characteristics, which is very flexible.
Long type.
Shortcoming
Depending on the clock of the machine, if the clock on the machine is dialed back, it will cause the number to be duplicated or the service will be unavailable.
Baidu UidGenerator
An implementation of Baidu based on snowflake
Advantages
Generate ID by snowflake algorithm of Twitter as above
Shortcoming
MySQL is required (built-in WorkerID allocator, which is allocated through DB in the startup phase; for custom implementation, DB is not required)
Advantages of Meituan Leaf
Highly available disaster recovery.
The ID number is the 64-digit number of the trending 8byte, which meets the primary key requirements of the database storage.
Shortcoming
DB downtime may make the entire system unavailable.
It's complicated.
ObjectId of MongoDB
Through "time + machine code + pid+inc" with a total of 12 bytes, it is finally identified as a 24-length hexadecimal character by the way of 4 "3" 2 "3".
Advantages
Lightweight, different machines can easily generate it in the same way that is globally unique.
Locally generated, time-stamped, orderly, low cost.
High security.
Relatively short, 24 places, such as Nuggets' ID,juejin.im/editor/post …
Shortcoming
It is long and difficult to remember.
Using machine ID and process ID,64 bit Long cannot be stored, only special ObjectId objects can be generated.
Realize the snowflake algorithm by programming
Refer to Twitter's snowflake algorithm to generate ID, refer to MongoDB's ObjectId generation strategy, and use similar machine ID and process ID to ensure uniqueness.
At this point, the study on "what is the unique ID generation strategy for the database" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.