UUID
Ready to try UUID?
Access the free online tool now - no registration required
UUID - Complete Guide
UUID (Universally Unique Identifier) is a 128-bit identifier standard (RFC 4122) that generates IDs guaranteed to be unique across space and time without central coordination. UUIDs are essential for distributed systems, database primary keys, and any scenario requiring unique identifiers without a central authority.
How It Works
UUIDs contain 128 bits displayed as 32 hex digits in 8-4-4-4-12 format (e.g., 550e8400-e29b-41d4-a716-446655440000). Version 4 (random) uses cryptographically random numbers. Version 1 uses timestamp + MAC address. The version number and variant bits are embedded in specific positions.
Technical Details
UUID versions: v1 (timestamp + MAC, sortable but reveals info), v4 (random, most common), v5 (namespace + name hash, deterministic), v7 (timestamp + random, sortable). v4 UUIDs have: version "4" at position 13, variant bits (8, 9, a, or b) at position 17. Probability of v4 collision: 1 in 2^122, essentially zero in practice.
Common Use Cases
- Database Primary Keys: Generate unique IDs without auto-increment counters, enabling distributed inserts.
- Example:
550e8400-e29b-41d4-a716-446655440000
- Example:
- Distributed Systems: Each service generates IDs independently without coordination.
- Session Tokens: Unguessable session identifiers for web applications.
- File/Resource Naming: Unique names for uploads, avoiding collisions.
- Correlation IDs: Track requests across microservices in logs.
Code Examples
JavaScript / Node.js
// Browser (crypto API)
const uuid = crypto.randomUUID();
// "a1b2c3d4-e5f6-4a7b-8c9d-0e1f2a3b4c5d"
// Node.js (crypto module)
const { randomUUID } = require('crypto');
const uuid = randomUUID();
// Using uuid package (supports all versions)
const { v4: uuidv4, v5: uuidv5 } = require('uuid');
const random = uuidv4();
const deterministic = uuidv5('hello', uuidv5.URL);
crypto.randomUUID() is built-in and uses secure random source. uuid package provides all versions.
Python
import uuid
# Version 4 (random) - most common
id = uuid.uuid4()
# UUID('a1b2c3d4-e5f6-4a7b-8c9d-0e1f2a3b4c5d')
# Version 1 (timestamp + MAC)
id = uuid.uuid1()
# Version 5 (namespace + name hash)
id = uuid.uuid5(uuid.NAMESPACE_URL, 'https://example.com')
# Convert to string (without dashes)
id_hex = uuid.uuid4().hex # 'a1b2c3d4e5f64a7b8c9d0e1f2a3b4c5d'
Python's uuid module includes all versions. Use .hex for string without dashes.
SQL (PostgreSQL)
-- Enable extension
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
-- Generate UUID
SELECT uuid_generate_v4();
-- As primary key
CREATE TABLE users (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
name VARCHAR(100)
);
-- PostgreSQL 14+: gen_random_uuid() built-in
CREATE TABLE items (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
name VARCHAR(100)
);
PostgreSQL supports UUID natively as a column type with built-in generation.
Tips & Best Practices
- Use UUIDv4 for general purposes, UUIDv7 for database primary keys
- Store as binary (16 bytes) instead of string (36 chars) in databases
- Consider ULID or NanoID if you need shorter, sortable IDs
- Always use cryptographically secure random sources for v4
- Index performance: consider sequential UUIDs (v7) for high-write tables
Common Mistakes to Avoid
- Using UUIDv1 for security (timestamps and MACs are predictable)
- Storing as 36-char string instead of binary (wastes 2x space)
- Using random UUIDs as primary keys in high-write databases (fragmentation)
- Assuming UUIDs are sequential or sortable (only v1, v6, v7 are)
When to Use This Tool
Use UUIDs when you need guaranteed-unique identifiers without central coordination: distributed databases, API resource IDs, session tokens, correlation IDs. Choose version based on needs: v4 for general use, v7 for sortable database keys.
Frequently Asked Questions
Are UUIDs truly unique?
Practically yes. UUIDv4 has 122 random bits, giving 5.3x10^36 possibilities. You'd need to generate 2.7 quintillion UUIDs to have a 50% chance of one collision. Random.org generates billions daily with no collisions ever recorded.
Which UUID version should I use?
v4 (random) is most common and recommended for general use. v7 (timestamp + random) is better for databases as it's sortable. v1 reveals MAC address and time. v5 is deterministic - same input always produces same UUID.
Are UUIDs secure enough for tokens?
UUIDv4 generated with crypto-random sources is unguessable. However, for sensitive tokens (password reset, API keys), consider additional entropy or purpose-built token generators. Never use v1 for security - timestamps and MACs are predictable.
Why are UUIDs bad for database performance?
Random UUIDs (v4) cause index fragmentation in B-tree indexes, leading to slower writes and more disk I/O. Solutions: Use UUIDv7 (time-ordered), ULID, or store as binary (16 bytes vs 36 char string). Or use auto-increment + UUID for external exposure.
Can I make UUIDs shorter?
Yes: Base64 encoding reduces 36 chars to 22. Base62/NanoID gives ~22 chars. You can also truncate, but collision probability increases. Short IDs like YouTube (11 chars) use different algorithms optimized for length.
Get Started
Ready to try UUID? Use the tool now - it's free, fast, and works right in your browser.
Last updated: June 27, 2026
Start using UUID
Free, fast, and privacy-focused - try it now
Share this article
Related Tools
Crontab
Crontab - Professional online developer tool. Fast, accurate, and free. Perfect for developers, designers, and technical professionals. Works offline and respects your privacy.
Luhn
Luhn - Professional online developer tool. Fast, accurate, and free. Perfect for developers, designers, and technical professionals. Works offline and respects your privacy.
Number Base
Number Base - Professional online developer tool. Fast, accurate, and free. Perfect for developers, designers, and technical professionals. Works offline and respects your privacy.
Responsive Tester
Responsive Tester - Professional online developer tool. Fast, accurate, and free. Perfect for developers, designers, and technical professionals. Works offline and respects your privacy.