In the Internet business system, there are various IDS involved, such as payment ID and refund ID in the payment system. So what are the general solutions for generating ids? Especially in the complex business scenario of distributed systems, it is very important for us to adopt the right solution for ourselves. We’ll list them one by one, not all of them will fit, but these solutions are just for your reference and may work for you.
An ID generally has the following elements:
- Uniqueness: Ensure that the generated ID is unique across the network.
- Sequential increment: Ensures that the generated ID is sequential increment for a user or service.
- High availability: Ensure that ids are generated correctly at all times.
- With time: ID contains time, a glance to know which day of the transaction.
System time Milliseconds
We can use the current system time to be accurate to milliseconds + business attributes + user attributes + random number +… To ensure the uniqueness of ID, the disadvantage is that the order of ID is difficult to guarantee, to ensure the order must rely on 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 we can last N billion years, the guarantee of uniqueness is needless to say, but the disadvantages are that it does not contain time, business data readability is poor, and the sequential increment of ID is not possible.
This is a simple generation, simple and efficient, but I haven’t seen this generation in general business systems.
The ID of the database is automatically added
We all know how to set an increment number for a database primary key, with a tendency to increment, to ensure that the primary key ID is unique.
This scheme is very simple, but the main problem lies in the dependence on the database itself, which increases the invisible pressure and dependence on the database access, once the single library sub-database sub-table or data migration will be awkward.
So, this is not the right way to generate ids.
Batch Generation ID
Multiple ids are generated in batches as required 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. This avoids the stress of having to visit the database every time an ID is generated.
This solution is a single point of service. If the service is restarted, it will inevitably cause discontinuous ID loss, and this approach is not conducive to horizontal scaling.
The middleware
All command operations in Redis are single-threaded and provide auto-increment commands like INCr, so the generated ids are guaranteed to be uniquely ordered.
This approach does not rely on relational databases and is fast. However, the system has to introduce Redis middleware, which increases the maintenance cost, and the workload of coding and configuration is relatively large. Even with the Redis component in place, the high frequency of access to generate ids will inevitably have an impact on single-threaded Redis performance.
You can also use versions of the ZNode data in Zookeeper to generate serial numbers, and MongoDB’s ObjectId, which is not recommended.
Snowflake algorithm
As you can see above, Twitter’s Snowflake algorithm consists of the following parts:
- A 41-bit time series, accurate to the millisecond, lasts 69 years
- A 10-bit machine ID supports a maximum of 1024 nodes
- The 12-bit serial number supports 4096 ID numbers per millisecond for each node. The highest symbol bit is always 0.
This scheme has good performance and increments on a single machine, but due to the distributed environment, the clocks on each machine cannot be completely synchronized, and may sometimes not be increments globally.
The project was discontinued in 2010, but the same design philosophy has been applied to various other ID generators and variants.
UidGenerator
UidGenerator is baidu open source distributed ID generator, based on the Implementation of snowflake algorithm, looks ok. However, the project maintenance of domestic open source is really worried.
You can refer to the specific use:
Github.com/baidu/uid-g…
Leaf
Leaf is meituan open source distributed ID generator, which can ensure global uniqueness, trend increasing, monotonic increasing, and information security. It also mentioned the comparison of several distributed schemes, but it also needs to rely on relational database, Zookeeper and other middleware.
For details, please refer to the official website:
Tech.meituan.com/MT_Leaf.htm…
Well, that’s all. Different scenarios and systems are also different. If you have a better solution, you can leave a comment below, and we can discuss how you all did it.
Recommended reading
Dry goods: Free 2TB architect four-stage video tutorial
Interview: the most complete Java multithreaded interview questions and answers
Tools: Recommended an online creation flow chart, mind mapping software
Share Java dry goods, high concurrency programming, hot technology tutorials, microservices and distributed technology, architecture design, blockchain technology, artificial intelligence, big data, Java interview questions, and cutting-edge hot news.