Understand the UUIDv7 bit layout and monotonicity
mainThe UUIDv7 implementation follows RFC 9562 with a specific bit layout designed for time-based sorting:
- unix_ts_ms (48 bits): Unix timestamp in milliseconds.
- ver (4 bits): Set to
0111(version 7). - counter (42 bits): Ensures increasing order within the same millisecond. If the counter overflows, the library increments the
unix_ts_msfield to maintain monotonicity. - var (2 bits): Set to
10. - rand (32 bits): Cryptographically strong random bits.
Monotonicity and Clock Rollbacks
UUIDv7 relies on the system clock. If the clock rolls back slightly, the library reuses the previous unix_ts_ms to maintain order. However, if the rollback is significant (default > 10 seconds), the generator resets, which may break the increasing order. Use V7Generator.generateOrAbort() to detect these events.