Generate collision-free unique database IDs across many servers running in parallel
Replace a central auto-increment counter with a distributed, coordination-free ID generator
Use the cached mode to handle high-throughput ID generation for performance-critical Java services
Integrate a Snowflake-based ID generator into an existing Spring application with MySQL for worker assignment
Requires a running MySQL database and Spring XML configuration to register each server's worker node ID on startup.
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.
← baidu on gitmyhub — every repo by this author, as a profile.
Verify against the repo before relying on details.