← projects
$ Snowflake ID Java
Dependency-free Java implementation of the Snowflake distributed unique ID algorithm, with optional JPA and Spring Boot integration.
- →Split into three independent Maven modules along hexagonal boundaries: snowflake-core (the algorithm itself, zero runtime dependencies), snowflake-jpa (Hibernate integration, no Spring dependency), and snowflake-jpa-spring (Spring Boot auto-configuration) so a consumer pulls in exactly the layer their stack needs.
- →Generates a 64 bit id packed as a sign bit, a 41 bit timestamp relative to a configurable epoch, a 10 bit node id, and a 12 bit sequence, produced through a single AtomicLong compare and swap loop instead of a lock, so concurrent threads retry on contention rather than blocking each other.
- →Detects system clock rollback (for example an NTP resync) and throws instead of silently reusing a timestamp that could produce a duplicate id.
- →Bridges Spring dependency injection into Hibernate's reflection based generator instantiation with a static holder, since Hibernate creates the custom IdentifierGenerator itself and never asks the ApplicationContext for it, then publishes the generator through Spring Boot's standard META-INF AutoConfiguration.imports discovery so a consumer only adds the dependency and sets snowflake.node-id, no manual @Import required.
- →Chose Snowflake over UUID and database auto increment: ids are generated entirely in memory with no round trip to the database, sort roughly by creation time so B-tree index insertion stays sequential instead of fragmenting the way random UUIDs do, and take half the storage of a UUID.
- →Published on JitPack and verified by resolving the real published artifact from a separate throwaway consumer project rather than relying on the local build, confirming the library is actually installable and functional for outside users.
JavaSpring BootHibernateHexagonal ArchitectureDistributed SystemsJitPack