当前位置:网站首页>Distributed ID
Distributed ID
2022-06-30 09:28:00 【Xiao Lu, a migrant worker in Beijing】
Distributed ID What conditions need to be met ?
2. Database auto increment primary key
3. Database cluster dual master mode
4. Segment mode based on Database
6. Snowflake algorithm (Snowflake) Pattern
Distributed ID What conditions need to be met ?
- Globally unique : Must ensure ID It's global and unique , The basic requirements
- High performance : High availability and low delay ,ID Generate response blocks , Otherwise, it will become a business bottleneck
- High availability :100% 's availability is deceptive , But also to be infinitely close to 100% The usability of
- Good access : We should adhere to the design principle of "use as you go" , The system design and implementation should be as simple as possible
- The trend is increasing : Best trend increasing , This requirement depends on the specific business scenario , Generally not strict
How to generate unique keys :
1.UUID
2. Database auto increment primary key
Implement a simple , But there is a single point problem , Unable to resist high concurrency scenarios
3. Database cluster dual master mode
Set different starting values and step sizes for each database

Solve a single problem , But it is not conducive to subsequent expansion , It still can't solve the new concurrency problem
4. Segment mode based on Database
Segment mode is now distributed ID One of the main ways to implement generator , The number segment mode can be understood as obtaining auto increment from database batch ID, One segment range at a time from the database , for example (1,1000] representative 1000 individual ID, Specific business services , Generate 1~1000 Self increasing of ID And load it into memory .
5. be based on Redis Pattern
Redis It can also be achieved , The principle is to use redis Of incr Command implementation ID Atomic self increasing of .
6. Snowflake algorithm (Snowflake) Pattern

Snowflake The generated is Long Type of ID, One Long Type account 8 Bytes , Each byte takes up 8 The bit , That is to say a Long Type account 64 A bit .
Snowflake ID Make up the structure : Positive digit ( Occupy 1 The bit )+ Time stamp ( Occupy 41 The bit )+ machine ID( Occupy 5 The bit )+ Data Center ( Occupy 5 The bit )+ Self value added ( Occupy 12 The bit ), in total 64 One of the bits Long type .
- first bit position (1bit):Java in long The highest bit of is the sign bit representing the positive and negative , A positive number is 0, Negative number is 1, General generation ID All positive , So the default is 0.
- Time stamp part (41bit): Time in milliseconds , It is not recommended to save the current time stamp , It's about using ( Current timestamp - Fixed start timestamp ) The difference between the , Can make ID Start with a smaller value ;41 Bit timestamps can be used 69 year ,(1L << 41) / (1000L 60 60 24 365) = 69 year
- Work the machine id(10bit): It's also called
workId, This can be configured flexibly , Machine room or combination of machine numbers . - Serial number part (12bit), Self increment supports the generation of the same node in the same millisecond 4096 individual ID
According to the logic of this algorithm , Just use this algorithm Java Language is realized , Encapsulation as a tool method , Then each business application can directly use the tool method to obtain the distributed ID, Just make sure that each business application has its own working machine id that will do , It doesn't need to build a single access distribution ID Application .
边栏推荐
- Treatment process record of Union Medical College Hospital (Dongdan hospital area)
- Idea shortcut key settings
- Express の Hello World
- Influencing factors of echo cancellation for smart speakers
- Applet learning path 2 - event binding
- Esp32 (7): I2S and I2C drivers for function development
- 8.8 heap insertion and deletion
- Handwriting sorter component
- Opencv learning notes -day 11 (split() channel separation function and merge() channel merge function)
- Solution to pychart's failure in importing torch package
猜你喜欢

Opencv learning notes -day3 (mat object and creation related operations mat:: clone(), mat:: copyto(), mat:: zeros(), mat:: ones(), scalar()...)

Abstract factory pattern

Flink Sql -- toAppendStream doesn‘t support consuming update and delete changes which

Interviewer: do you understand the principle of recyclerview layout animation?

Challenge transform() 2D

Express の Hello World

Use Huawei performance management service to configure the sampling rate on demand

桂林 稳健医疗收购桂林乳胶100%股权 填补乳胶产品线空白

Wechat development tool (applet)

12. problem set: process, thread and JNI architecture
随机推荐
Deep Learning with Pytorch- A 60 Minute Blitz
Esp32 (7): I2S and I2C drivers for function development
How to format an UTC date to use the Z (Zulu) zone designator in php?
Get to know handler again
Opencv learning notes -day1 (image reading display imread, imshow, namedwindow)
Flink Sql -- toAppendStream doesn‘t support consuming update and delete changes which
Invalid update: invalid number of sections. The number of sections contained in the table view after
[JPEG] how to compile JPEG turbo library files on different platforms
5. Messager framework and imessager interface
Challenge transform() 2D
2020-11-02
Deeply understand the working principle of kotlin collaboration suspend (beginners can also understand it)
Talk about writing
Terminal -- Zsh of terminal three swordsmen
Esp32 things (I): Preface
Tclistener server and tcpclient client use -- socket listening server and socketclient use
Rew acoustic test (I): microphone calibration
I once met a girl whom I most wanted to take care of all my life. Later... No later
Electron, which can wrap web page programs into desktop applications
Opencv learning notes -day3 (mat object and creation related operations mat:: clone(), mat:: copyto(), mat:: zeros(), mat:: ones(), scalar()...)