How to Generate UUIDs and Unique Identifiers
Unique identifiers are fundamental to distributed systems. Learn the differences between UUID v4, v7, ULID, and other ID formats and when to use each.
Key Takeaways
- In distributed systems, databases, and APIs, every record needs a unique identifier.
- UUID v4 generates 122 bits of randomness, producing identifiers like `550e8400-e29b-41d4-a716-446655440000`.
- UUID v7 embeds a Unix timestamp in the first 48 bits, making IDs sortable by creation time.
- ULID (Universally Unique Lexicographically Sortable Identifier) encodes time and randomness in a 26-character Crockford Base32 string.
Hash Generator
Generate SHA-1, SHA-256, SHA-384, SHA-512 hashes from text
Why Unique IDs Matter
In distributed systems, databases, and APIs, every record needs a unique identifier. Auto-incrementing integers work for single databases but fail in distributed environments where multiple systems create records simultaneously.
UUID v4: Random
UUID v4 generates 122 bits of randomness, producing identifiers like 550e8400-e29b-41d4-a716-446655440000. The collision probability is astronomically low — you'd need to generate 2.71 quintillion UUIDs to have a 50% chance of collision.
UUID v7: Time-Sorted
UUID v7 embeds a Unix timestamp in the first 48 bits, making IDs sortable by creation time. This is ideal for database primary keys because sorted inserts are more efficient for B-tree indexes.
ULID: Lexicographic Sorting
ULID (Universally Unique Lexicographically Sortable Identifier) encodes time and randomness in a 26-character Crockford Base32 string. ULIDs sort correctly as strings, avoiding the need for special comparison logic.
Choosing the Right Format
| Format | Sortable | Size | URL-safe |
|---|---|---|---|
| UUID v4 | No | 36 chars | No (hyphens) |
| UUID v7 | Yes | 36 chars | No |
| ULID | Yes | 26 chars | Yes |
| Snowflake | Yes | 19 digits | Yes |
Công cụ liên quan
Định dạng liên quan
Hướng dẫn liên quan
JSON vs YAML vs TOML: Choosing a Configuration Format
Configuration files are the backbone of modern applications. JSON, YAML, and TOML each offer different trade-offs between readability, complexity, and tooling support that affect your development workflow.
How to Format and Validate JSON Data
Malformed JSON causes silent failures in APIs and configuration files. Learn how to format, validate, and debug JSON documents to prevent integration errors and improve readability.
Base64 Encoding: How It Works and When to Use It
Base64 converts binary data into ASCII text, making it safe for transmission through text-based systems. Learn when Base64 is the right choice and when alternatives like hex encoding or URL encoding are more appropriate.
Best Practices for Working with Unix Timestamps
Unix timestamps provide a language-agnostic way to represent points in time, but they come with pitfalls around time zones, precision, and the 2038 problem. This guide covers best practices for storing and converting timestamps.
Troubleshooting JWT Token Issues
JSON Web Tokens are widely used for authentication but can be frustrating to debug. This guide covers common JWT problems including expiration errors, signature mismatches, and payload decoding issues.