UUID Generator: Complete Guide
UUIDs (Universally Unique Identifiers) are 128-bit identifiers that can be generated independently on any computer and are guaranteed to be unique without coordination. They're essential for distributed systems, databases, and APIs. This comprehensive guide explains how UUIDs work, when to use different versions, and best practices for implementation.
What is a UUID?
A UUID (Universally Unique Identifier) is a 128-bit label used to uniquely identify information across systems without requiring central coordination. UUIDs are formatted as 32 hexadecimal digits displayed in five groups separated by hyphens: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx, like "550e8400-e29b-41d4-a716-446655440000". The key insight behind UUIDs is that the space of possible values is so vast that randomly generated IDs are effectively guaranteed to be unique. With 2^128 possible values (approximately 3.4 × 10^38), you could generate a billion UUIDs per second for a century and have a negligible probability of collision. UUIDs solve a fundamental problem in distributed systems: how can multiple computers independently generate unique identifiers without communication? Traditional sequential IDs require a central authority (like a database auto-increment) that becomes a bottleneck and single point of failure. UUIDs can be generated anywhere, anytime, with confidence they won't conflict. The UUID format is standardized in RFC 4122. The five groups contain 8, 4, 4, 4, and 12 hex digits respectively, totaling 36 characters (including hyphens). The format includes version and variant information encoded in specific bits, which we'll explore in the version comparison. Common applications include: primary keys in databases, especially distributed databases; session identifiers in web applications; tracking identifiers for API requests and events; unique filenames for uploaded content; device identifiers in mobile applications; and correlation IDs for distributed tracing. While "UUID" is the official term from RFC 4122, Microsoft uses "GUID" (Globally Unique Identifier) for the same concept. They're functionally identical; the terms are interchangeable.
UUID Versions
The UUID specification defines five versions, each with different generation methods and use cases. The version is encoded in bits 12-15 of the UUID (the first hex digit of the third group). Understanding these versions helps you choose appropriately for your application. Version 1 (time-based): Combines the current timestamp (100-nanosecond intervals since October 15, 1582) with the MAC address of the generating computer. V1 UUIDs are guaranteed unique and can be sorted chronologically. However, they expose when and where they were generated, which may be a privacy concern. The MAC address reveals the device manufacturer and can be used for tracking. Version 2 (DCE Security): A variant of Version 1 that incorporates POSIX UID/GID. Rarely used in practice and not widely supported. Version 3 (MD5 name-based): Generated by hashing a namespace UUID and a name using MD5. Given the same namespace and name, you always get the same UUID. Useful for creating reproducible IDs from known inputs. However, MD5 is cryptographically weak, so Version 5 is preferred for new applications. Version 4 (random): Generated using random or pseudo-random numbers. This is the most commonly used version because it's simple, has no privacy concerns, and provides excellent uniqueness guarantees. 122 bits are random (6 bits are used for version and variant), giving 2^122 possible values. Our generator produces V4 UUIDs. Version 5 (SHA-1 name-based): Like Version 3 but uses SHA-1 instead of MD5. Preferred over V3 for name-based UUIDs. Use when you need deterministic UUIDs from known inputs. For most applications, Version 4 (random) is the right choice. Use name-based versions (3 or 5) when you need reproducibility—the same inputs always produce the same UUID. Use Version 1 only if you specifically need time-based ordering and accept the privacy tradeoffs.
Use Cases
UUIDs serve diverse purposes across software development. Understanding these use cases helps you apply UUIDs effectively and choose the right approach for your specific needs. Database primary keys: UUIDs make excellent primary keys, especially in distributed databases or systems that might merge data from multiple sources. Unlike auto-increment IDs, UUIDs can be generated client-side before the record exists, IDs don't reveal order or count information, and merging databases doesn't cause ID collisions. However, UUIDs are larger than integers (16 bytes vs 4-8 bytes), which affects storage and index performance. Session identifiers: UUIDs are ideal for session tokens because they're unguessable (unlike sequential IDs) and unique without checking the database. A user can't deduce other valid session IDs from their own. Combine with server-side validation and secure storage. API request tracking: Including a UUID in each API request enables distributed tracing across microservices. The request ID propagates through all service calls, letting you correlate logs and debug issues across system boundaries. Tools like Jaeger and Zipkin use this pattern. File and resource naming: When storing uploaded files, a UUID filename prevents collisions (two users uploading "document.pdf") and prevents enumeration attacks (guessing valid filenames). The original filename can be stored as metadata while the UUID handles storage. Unique identifiers in URLs: UUIDs in URLs like /products/550e8400-e29b-41d4-a716-446655440000 don't reveal your data's scale (how many products exist) or order (which product was created first). This is useful for privacy and preventing scraping. Idempotency keys: When making API requests that shouldn't be duplicated (like payments), include a client-generated UUID. The server can use this to detect and reject duplicate requests, making the operation idempotent. Offline-first applications: Mobile apps that work offline can generate UUIDs for new records immediately. When syncing to the server, there's no conflict between different devices creating records simultaneously.
Prova verktyget
UUID-generator
Läs mer
Vanliga frågor
UUID-generator
Vanliga frågor →