Why ID choice matters
Identifiers are not just labels; in a database they are often the primary key. The format of that key affects index size, write performance and how easy it is to page through data. Modern IDs put a timestamp first so new rows append to the end of the index instead of scattering across it.
The contenders
| Type | Length | Sortable | Best for |
|---|---|---|---|
| UUID v4 | 36 chars | No | Legacy systems, pure randomness |
| UUID v7 | 36 chars | Yes | Standards-compliant sortable keys |
| ULID | 26 chars | Yes | Compact, URL-safe sortable keys |
| NanoID | 21 chars | No | Short unique tokens and slugs |
Why timestamp-first helps
Random keys force the database to insert rows at random points in the index, which causes page splits and slows writes. A timestamp as the leading bytes means new IDs are always larger than old ones, so inserts land at the end. As a bonus, sorting by the key roughly sorts by creation time, which simplifies pagination and debugging.
Choosing between them
- Need a standard format with hyphens? Use UUID v7.
- Want something shorter and URL-friendly? Use ULID.
- Just need a short unique token? Use NanoID.
Generate them safely
Use a cryptographically secure random source, not Math.random. Browser tools can call crypto.getRandomValues, which is what the UUID v7, ULID and NanoID Generator uses. If you only need a plain random UUID, the UUID Generator covers v4.
Do not use IDs as secrets
Sortable IDs are predictable in their timestamp portion, so never use them as access tokens or password-reset links. For that, generate a long random value and hash it - see the Hash Generator. Keep IDs for identification and secrets for security.