Skip to the content.

πŸ”‘ What is a UUID?

πŸ‘‰ Example:

550e8400-e29b-41d4-a716-446655440000

πŸ“¦ UUID Versions

There are different standards for how UUIDs are generated (defined in RFC 4122):

  1. UUID v1 (Time-based)

    • Generated from timestamp + MAC address.
    • Weakness: Can expose machine info (MAC).
  2. UUID v3 (Name-based, MD5 hash)

    • Deterministic: Same input β†’ same UUID.
  3. UUID v4 (Random-based) βœ… most common

    • 122 random bits β†’ extremely unlikely to collide.
    • Collision probability negligible (~1 in 2^122).
  4. UUID v5 (Name-based, SHA-1 hash)

    • Like v3, but uses SHA-1.

⚑ Why UUID?


πŸ”„ 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)

πŸ‘‰ Use when:


πŸ”Ή Snowflake ID (e.g., Twitter Snowflake)

πŸ‘‰ Use when:


βš–οΈ Quick Rule of Thumb