π What is a UUID?
- UUID stands for Universally Unique Identifier.
- Itβs a 128-bit number (much bigger than Snowflakeβs 64-bit).
- Represented as a 36-character string (hexadecimal + hyphens).
π Example:
550e8400-e29b-41d4-a716-446655440000
π¦ UUID Versions
There are different standards for how UUIDs are generated (defined in RFC 4122):
-
UUID v1 (Time-based)
- Generated from timestamp + MAC address.
- Weakness: Can expose machine info (MAC).
-
UUID v3 (Name-based, MD5 hash)
- Deterministic: Same input β same UUID.
-
UUID v4 (Random-based) β most common
- 122 random bits β extremely unlikely to collide.
- Collision probability negligible (~1 in 2^122).
-
UUID v5 (Name-based, SHA-1 hash)
- Like v3, but uses SHA-1.
β‘ Why UUID?
- Globally unique β practically no chance of collision.
- No central coordination β any machine can generate safely.
- Language/framework support β built-in in Java, Python, databases (Postgres, MySQL, MongoDB).
π UUID vs Snowflake
| Feature | UUID | Snowflake ID |
|---|---|---|
| Size | 128-bit (36-char string) | 64-bit integer |
| Order | Not ordered (random) | Time-ordered (sortable) |
| Generation | Random or timestamp-based | Timestamp + machine ID + sequence |
| Use Case | General unique IDs, databases | High-scale distributed systems |
| Readability | Longer (harder to index) | Shorter, efficient for DB index |
π Example in Java
import java.util.UUID;
public class UUIDExample {
public static void main(String[] args) {
UUID uuid = UUID.randomUUID(); // generates v4 random UUID
System.out.println(uuid.toString());
}
}
Output:
c9b1b0b3-1c34-4c72-b33e-2a6a2d5f3e2a
β In short: A UUID is a 128-bit universally unique identifier (commonly random-based, v4), used when you need global uniqueness without coordination, but you donβt care about ordering or compact size.
πΉ UUID (Universally Unique Identifier)
- Format: 128-bit value, usually represented as a 36-character string (
550e8400-e29b-41d4-a716-446655440000). - Generation: Can be random (UUIDv4), time-based (UUIDv1), or name-based (UUIDv3/v5).
-
Pros:
- Easy to generate locally (no coordination needed).
- Globally unique.
- Widely supported in databases, libraries, and APIs.
-
Cons:
- Large (128-bit), so indexing/search is slower compared to smaller IDs.
- Not ordered β can cause index fragmentation in databases.
- String representation takes more storage.
π Use when:
- You need uniqueness across multiple systems with zero coordination.
- You donβt care about ordering or small size.
- Example: API keys, distributed systems where nodes generate IDs independently.
πΉ Snowflake ID (e.g., Twitter Snowflake)
- Format: 64-bit integer.
-
Structure:
- Timestamp (milliseconds since epoch) β ensures time-ordering.
- Machine/worker ID β identifies node.
- Sequence number β ensures uniqueness within the same millisecond.
-
Pros:
- Smaller (64-bit int β efficient for storage & indexing).
- Time-ordered β helps in sorting and querying.
- High throughput (many IDs per millisecond).
-
Cons:
- Requires coordination to assign worker IDs.
- Slightly more complex to implement than UUID.
- If clock goes backward β can cause collisions.
π Use when:
- You want ordered IDs (time-based sequence).
- Database efficiency matters (smaller, indexed integers are faster than UUIDs).
- You have a distributed system where many IDs are generated quickly (e.g., Twitter posts, ride IDs in Uber).
βοΈ Quick Rule of Thumb
- β Use UUID β when simplicity & universal uniqueness matter more than storage/indexing efficiency.
- β Use Snowflake ID β when you need scalable, ordered, numeric IDs that are efficient for databases.