"Talk about common distributed ID design solutions"!

A classmate who has worked for 7 years was asked such a question.

The question is not difficult, but in the actual interview, if you just answer 1, 2, 3

It is difficult to pass the interview, because as a senior programmer, you also need to have your own understanding and thinking.

Hi, my name is Mic, a Java programmer who has been working for 14 years.

The expert answer to this question, I sorted it into the interview document of 15W words, you can privately message me to receive it.

Check out the experts' answers below

Expert:

First of all, there are many solutions for distributed global ID, such as:

  • Using Mysql's global table
  • Ordered nodes using Zookeeper
  • Using MongoDB's objectid
  • auto-increment id of redis
  • UUIDs etc.
  • ......

These solutions only solve the basic id uniqueness problem. In the actual production environment, it is necessary to build a globally unique id

There are more factors to consider:

  • Ordered, ordered IDs can better confirm the location of data, and in the storage structure of B+ numbers, range queries are more efficient, and can improve the efficiency of B+ tree data maintenance.
  • Security, avoid malicious data leakage caused by crawling data
  • Availability, the availability requirements of the ID generation system are very high, once a failure occurs, it will cause the problem of business unavailability
  • Performance, the global id generation system needs to meet the business needs of the entire company, involving billion-level calls, and has high performance requirements

Therefore, if we choose the global table of the database, you need to update the database without obtaining the id once, and the performance upper limit is obvious.

And it is very difficult to build a highly scalable and high-performance solution based on the database.

Therefore, the current mainstream solution on the market is based on Twitter's early open source Snowflake algorithm.

It is a global id generation algorithm composed of 64-bit length. It expresses different meanings by dividing 64-bits into intervals to achieve uniqueness.

img

Its benefits are:

  • The algorithm is simple to implement
  • There are not too many external dependencies
  • Can generate meaningful ordered numbers
  • Based on bit operations, performance is also good, with the Twitter test peaking at 100,000 per second.

In addition, Meituan has open sourced a globally unique id generation system leaf, which also uses the snowflake algorithm to construct a globally unique id

And in terms of high performance and high availability, many optimizations have been made, providing hundreds of millions of calls per day for Meituan's internal business.

Summarize

It is clear that this is a hot issue, and it is widely used in practical applications.

It is recommended that fans do some deeper thinking and research in this field, so as to respond to further questions from the interviewer

Remember, the design and implementation details of the global ID itself are important.

Everyone remember to like, favorite and follow

file

Copyright notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless otherwise stated. Please indicate the source for Mic带你学架构 !
If this article is helpful to you, please help to follow and like, your persistence is the driving force for my continuous creation. Welcome to follow the WeChat public account of the same name to get more technical dry goods!

跟着Mic学架构
810 声望1.1k 粉丝

《Spring Cloud Alibaba 微服务原理与实战》、《Java并发编程深度理解及实战》作者。 咕泡教育联合创始人,12年开发架构经验,对分布式微服务、高并发领域有非常丰富的实战经验。