Do you know four ways to write distributed ID?



In our life, ID is actually inseparable from our life.

ID card number, QQ, mobile phone number, bank card number, student ID number, even the number lying on your hard disk.

These ID marks are so important that every background programmer has to think about this problem? What are the advantages and disadvantages?

This article takes you to learn several common ID generation strategies in simple terms.

Introductory learning

Due to space priority, it is recommended to read the following article.

Distributed ID generation requirements

UUID strategy explanation

Random generation strategy

Explanation of snowflake algorithm

Open source tools

ID is a common ID implementation strategy for Java design.

So that you can use it out of the box in daily development and enjoy the happiness of leaving work early~

Creative intention

For ID generation, all background systems must face the problem, distributed ID generation is also a very common requirement.

Recently, the code written by my colleagues, in multiple machines and issued a serial number conflict.

I think Id strategies should be aggregated into a toolkit instead of building wheels every time. Sometimes there are problems.


  • Minimalist API, a line of code to do everything
  • Built in a variety of ID generation strategies, there is always one for you
  • The jar package is only 13K

Quick start

Maven introduction


Introductory examples

Test code

final String snowflake = IdHelper.snowflake();

  • result

Built in method

All IDS can be accessed through theIdHelperCall directly.

Serial number method example
1 uuid8() dUlmJiEq
2 uuid32() 3f9afd5773d143fb8e28d80de47f3d1a
3 random() 202005122127270743937635635
4 random(prefix, length) random("999", 5)It is 9992020051221340961522263
5 snowflake() 1260199914969849858
6 local() Starting from 0, gradually add 1 to return the ID


  • [] snowflake customization + code optimization
  • [] introduce comb algorithm
  • ] compatibility of other open source IDS
  • [] ID generation based on common databases
  • [] ID generation based on redis

Recommended Today

DK7 switch’s support for string

Before JDK7, switch can only support byte, short, char, int or their corresponding encapsulation classes and enum types. After JDK7, switch supports string type. In the switch statement, the value of the expression cannot be null, otherwise NullPointerException will be thrown at runtime. Null cannot be used in the case clause, otherwise compilation errors will […]