Generate UUID v1 / v4 / v5 with batch support
UUID (Universally Unique Identifier) is a 128-bit identifier used to generate globally unique IDs in distributed systems without requiring a central coordination server. The standard format is xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx (36 characters including hyphens). UUIDs are widely used for database primary keys, message queues, and inter-service communication.
| Version | Generation | Characteristics |
|---|---|---|
| v1 | Timestamp + MAC address | Sortable, but exposes device info |
| v4 | Fully random | Most common, 122 random bits |
| v5 | Namespace + SHA-1 | Same input always produces same UUID |
| v7 | Timestamp prefix + random | Time-sortable (RFC 9562, 2024) |
Collision probability: For v4, you'd need to generate roughly 10¹⁸ UUIDs to have a 50% collision chance — negligible in practice.
For time-sortable use cases, prefer UUID v7 or ULID (26 chars, timestamp prefix, lexicographically sortable).
Theoretically collisions are possible, but practically negligible. Generating 1 billion UUID v4s per second, it would take ~85 years to reach a 50% probability of a single collision. For virtually all applications, they can be treated as unique.
UUIDs work well in distributed systems since they need no coordination. The downsides are larger storage (16 bytes vs 4/8 bytes) and random insertion degrading B+ tree index performance. If you need UUID as a primary key, use UUID v7 to maintain time-ordering.
ULID is more compact (26 chars vs 36), naturally time-sorted, and has no hyphens making it URL-friendly. However, UUID has a more mature ecosystem with native support in virtually every language and database. For new projects, consider ULID or UUID v7.