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

Example Analysis of Global ID Generation Strategy in distributed Business system

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

Share

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

This article mainly shows you the "example analysis of global ID generation strategy in distributed business systems", which is easy to understand and well-organized. I hope it can help you solve your doubts. Let me lead you to study and learn the article "sample analysis of global ID generation strategy in distributed business systems".

I. introduction to global ID

In the actual development, almost all the data generated by business scenarios need a unique ID as the core identity for process management. Such as the common ones:

Order: order-id, check order details, logistics status, etc.

Payment: pay-id, payment status, transaction management based on ID

How to generate a unique identity can be solved by general methods in ordinary scenarios, such as:

Import java.util.UUID;public class UuidUtil {public static String getUUid () {UUID uuid = UUID.randomUUID (); return String.valueOf (uuid) .replace ("-", ");}

This method can solve most of the scenario business with unique ID requirements, but all kinds of UUID repeat scenario description posts on the Internet, as if the API is not easy to use.

To talk about a real business scenario, it is about 30 million of the data pipelined in half a year, using UUID's API. The problem of ID repetition has not been caught for reference only.

2. Snowflake algorithm 1. Concept introduction

Twitter's open source distributed ID generation algorithm strategy, the generated ID follows the chronological order.

1 is a bit identifier, always 0, not available

41-bit time cut, the difference between the stored time cut (current time cut-start time cut)

10-bit machine identification, 10-bit length supports the deployment of up to 1024 nodes

12-bit sequence, counting within milliseconds, 12-bit counting sequence numbers support each node to generate 4096 ID sequence numbers per millisecond

The advantage of SnowFlake is that it is sorted by time increment as a whole, and there are no ID collisions (distinguished by data center ID and machine ID) in the entire distributed system, and it is more efficient.

2. Coding implementation

There are many tools that can be customized, such as start time, machine ID configuration, etc.

/ * * Snowflake algorithm ID generates * / public class SnowIdWorkerUtil {/ / start time cut (2020-01-02) private final long timeToCut = 1577894400000L; / / the number of digits occupied by machine ID private final long workerIdBits = 2L; / / the number of digits occupied by data identification ID private final long dataCenterIdBits = 8L / / the largest machine supported, ID, the result is 31 (this shift algorithm can quickly calculate the maximum decimal number that several binary numbers can represent) private final long maxWorkerId =-1L ^ (- 1L)

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