- What is a UUID?
- A UUID (Universally Unique Identifier) is a 128-bit identifier designed to be unique across all devices and time without requiring central coordination. UUIDs are formatted as 32 hexadecimal digits in 5 groups separated by hyphens (8-4-4-4-12): e.g., 550e8400-e29b-41d4-a716-446655440000. They are defined by RFC 4122 and used in databases, distributed systems, APIs, and software to uniquely identify records, sessions, and resources.
- What is the difference between UUID versions?
- UUID v1 uses timestamp and MAC address (can reveal device info). UUID v4 is fully random (most common, privacy-friendly). UUID v3/v5 are namespace-based using MD5/SHA-1 hashing for deterministic IDs. UUID v6 reorders v1 fields for better sorting. UUID v7 uses Unix timestamp + random bits for time-ordered, database-optimized IDs. For most applications, use v4 for simplicity or v7 for database primary keys.
- Can UUIDs collide or duplicate?
- UUID collisions are theoretically possible but astronomically unlikely. UUID v4 has 122 random bits, providing 5.3×10^36 possible values. To have a 50% probability of collision, you would need to generate 2.71 quintillion UUIDs—more than the grains of sand on Earth. In practice, you would run out of storage space before generating a duplicate. For all practical purposes, treat UUIDs as unique.
- Should I use UUID for database primary keys?
- UUIDs can be excellent primary keys, especially in distributed systems. Advantages: no coordination needed between servers, merge-friendly, prevents enumeration attacks, works offline. Disadvantages: 16 bytes vs 4-8 for integers (more storage/memory), random UUIDs (v4) fragment B-tree indexes causing slower inserts. Solution: use UUID v7 (time-ordered) or store as binary(16) instead of varchar(36).
- What is the difference between UUID and GUID?
- GUID (Globally Unique Identifier) and UUID (Universally Unique Identifier) are the same thing—just different names. Microsoft coined 'GUID' in the 1990s for COM/Windows, while the industry standard uses 'UUID' per RFC 4122. Both follow identical specifications and formats. Use whichever term your platform prefers: GUID in .NET/Windows, UUID everywhere else.
- When should I use UUID v7 instead of v4?
- Use UUID v7 when IDs will be used as database primary keys or need to be sortable by creation time. UUID v7 embeds a Unix timestamp in the first 48 bits, making IDs naturally chronological. This dramatically improves database insert performance (no B-tree fragmentation) and allows rough time-based queries. Use v4 when you need maximum randomness or have privacy concerns about revealing creation time.
- How do I generate UUIDs in different programming languages?
- JavaScript: crypto.randomUUID() or uuid package. Python: import uuid; uuid.uuid4(). Java: UUID.randomUUID(). C#/.NET: Guid.NewGuid(). PHP: Ramsey\Uuid\Uuid::uuid4(). Go: github.com/google/uuid. Ruby: SecureRandom.uuid. All produce RFC 4122 compliant v4 UUIDs. For v7, use dedicated libraries as native support is still limited.
- UUID vs ULID vs nanoid: which should I use?
- UUID: universal standard, wide support, 36 characters. ULID: sortable, URL-safe, 26 characters, Crockford Base32. nanoid: shortest (21 chars default), URL-safe, customizable alphabet, fastest generation. Choose UUID for interoperability and standards compliance. Choose ULID for sortable IDs with better readability. Choose nanoid for compact URL-safe IDs in modern web apps.
- Are UUIDs secure and safe for authentication tokens?
- UUID v4 provides 122 bits of randomness—sufficient for most use cases but not ideal for security tokens. For authentication tokens, session IDs, or API keys, prefer cryptographically secure random bytes (256+ bits) using crypto.getRandomValues() or similar. UUIDs are predictable in v1 (timestamp-based) and have less entropy than purpose-built token generators.
- How should I store UUIDs in a database?
- Best practice: store as BINARY(16) or native UUID type (PostgreSQL). This uses 16 bytes vs 36 for varchar, improves index performance, and reduces storage by 55%. If using varchar(36), always use lowercase with hyphens for consistency. PostgreSQL has a native UUID type. MySQL 8+ has UUID_TO_BIN()/BIN_TO_UUID() functions. For maximum compatibility, use varchar(36) with proper indexing.