idgenkit

Algorithms

This is the contract every implementation in this repository follows. testdata/ holds the conformance vectors that enforce it. Regenerate them with python3 testdata/generate_vectors.py; the invalid-ULID list is maintained by hand.

ULID

 0                   1                   2                   3
 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                      48-bit time (ms, Unix)                   |
+                               +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                               |       80-bit randomness       |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+                               +
|                                                               |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

UUIDv4 and UUIDv7

Both follow RFC 9562.

 0                   1                   2                   3
 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                    48-bit time (ms, Unix)                     |   v7 only;
+                               +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+   random in v4
|                               |  ver  |       rand_a          |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|var|                        rand_b                             |
+-+-+                                                           +
|                                                               |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

Relative ID

A relative ID is sortable and unique, and every ID generated for the same caller-supplied key (a customer, tenant or device id) starts with the same tag:

3F8KQZ-01J8Y4Q9M3-X3B0N6E3P8
|----| |--------| |--------|
 tag    48-bit ms  50-bit random / counter

Snowflake

 63                                        22 21        12 11          0
+--------------------------------------------+------------+-------------+
|   42-bit ms since epoch                    | 10-bit mid | 12-bit seq  |
+--------------------------------------------+------------+-------------+

Nano ID