explaingit

baidu/uid-generator

5,576JavaAudience · developerComplexity · 3/5Setup · moderate

TLDR

UidGenerator is a Java library from Baidu for generating unique ID numbers across many servers at once without any coordination between them. Based on Twitter's Snowflake algorithm, with a cached mode that can produce over 6 million IDs per second.

Mindmap

mindmap
  root((repo))
    What it does
      Unique ID generation
      Distributed safe IDs
    Algorithm
      Snowflake based
      Timestamp bits
      Worker node bits
      Sequence counter
    Modes
      Default generator
      Cached generator
    Setup
      MySQL worker table
      Spring XML config
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

Things people build with this

USE CASE 1

Generate collision-free unique database IDs across many servers running in parallel

USE CASE 2

Replace a central auto-increment counter with a distributed, coordination-free ID generator

USE CASE 3

Use the cached mode to handle high-throughput ID generation for performance-critical Java services

USE CASE 4

Integrate a Snowflake-based ID generator into an existing Spring application with MySQL for worker assignment

Tech stack

JavaSpringMySQL

Getting it running

Difficulty · moderate Time to first run · 30min

Requires a running MySQL database and Spring XML configuration to register each server's worker node ID on startup.

License terms are not specified in the explanation.

In plain English

UidGenerator is a Java library from Baidu that generates unique ID numbers for use in distributed systems. In a distributed system, many servers run at the same time, and each needs to produce IDs that will never collide with IDs produced by any other server. This is a harder problem than it looks, because you cannot rely on a single central counter when you have many machines working in parallel. The library is based on an algorithm called Snowflake, originally published by Twitter. Each ID is a 64-bit number packed with three pieces of information: a timestamp in seconds, an identifier for the specific server (worker node) that produced it, and a sequence counter for IDs produced within the same second. Because each server has its own ID and each second has its own sequence range, two servers can produce IDs at the same moment without any coordination between them, and the results will never be identical. Baidu's version extends the basic Snowflake idea with a higher-throughput mode called CachedUidGenerator. Instead of generating each ID on demand, this mode pre-generates a batch of IDs and stores them in a circular buffer in memory. Consumer threads pull from the buffer rather than waiting for each individual ID to be computed. The README states that this approach allows a single instance to produce more than six million IDs per second. The buffer refills automatically when it starts running low, either based on a percentage threshold or on a fixed time interval. Setup involves creating a database table in MySQL, which the library uses to assign each server its unique worker node ID on startup. Configuration is done through Spring XML files. Two implementations are provided: a simpler one suitable for most workloads, and the cached version for performance-sensitive applications. The project requires Java 8 or later and is designed to be used as a component inside Spring-based Java applications.

Copy-paste prompts

Prompt 1
How do I add UidGenerator to a Spring Boot application and configure the MySQL table it uses to assign worker node IDs?
Prompt 2
What is the difference between DefaultUidGenerator and CachedUidGenerator in baidu/uid-generator, and when should I use each?
Prompt 3
Show me the Spring XML configuration needed to set up CachedUidGenerator with a custom ring-buffer size and padding factor
Prompt 4
How does UidGenerator assign a unique worker node ID to each server instance on startup using the MySQL table?
Prompt 5
Write Java code to generate a unique ID with UidGenerator and then parse it back into its timestamp, worker ID, and sequence components
Open on GitHub → Explain another repo

← baidu on gitmyhub — every repo by this author, as a profile.

Verify against the repo before relying on details.