What is a UUID?
A UUID (Universally Unique Identifier), also called a GUID (Globally Unique Identifier), is a 128-bit label used to uniquely identify information. UUIDs are formatted as 32 hexadecimal digits in five groups separated by hyphens: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.
550e8400-e29b-41d4-a716-446655440000UUID Versions
UUID v1 — Time-based
UUID v1 is generated from the current timestamp and the MAC address of the generating machine. It is sortable chronologically but reveals information about when and where it was generated.
- Contains timestamp — sortable by creation time
- Contains MAC address — privacy concern
- Good for: distributed systems needing temporal ordering
UUID v4 — Random
UUID v4 is generated from random or pseudo-random numbers. It is the most widely used version due to its simplicity and privacy properties.
- 122 random bits — collision probability is negligible
- No information about when/where it was generated
- Good for: most use cases — database IDs, session tokens, API keys
UUID v5 — Name-based (SHA-1)
UUID v5 generates a deterministic UUID from a namespace UUID and a name using SHA-1 hashing. The same inputs always produce the same UUID.
- Deterministic — same name + namespace = same UUID
- Good for: converting existing identifiers to UUIDs consistently
Comparison
| v1 | v4 | v5 | |
|---|---|---|---|
| Source | Timestamp + MAC | Random | Name + Namespace |
| Sortable | Yes | No | No |
| Deterministic | No | No | Yes |
| Privacy | Low (leaks MAC) | High | Medium |
| Most common | No | Yes ✅ | Sometimes |
UUID in Databases
UUIDs are popular as database primary keys, especially in distributed systems where auto-increment IDs would conflict across multiple database nodes.
-- PostgreSQL
CREATE TABLE users (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
name VARCHAR(100)
);💡 For database performance, consider UUID v7 (time-ordered random UUID) which is sortable like v1 but uses random data instead of MAC address. It provides better index performance in most databases.