Skip to content
useToolz online tools
UUID v4: What It Is, Why You Need It, and How to Generate One
Development

UUID v4: What It Is, Why You Need It, and How to Generate One

Александр Михеев

Александр Михеев

12 August 2024 · 5 min read

What Is a UUID

UUID (Universally Unique Identifier) is a 128-bit identifier that is guaranteed to be globally unique without a central coordinating authority. A UUID is written as 32 hexadecimal characters separated by hyphens in the 8-4-4-4-12 format:

550e8400-e29b-41d4-a716-446655440000

UUID is standardized in RFC 4122 and is used everywhere: databases, APIs, distributed systems, sessions, file systems. The main advantage of UUID is the ability to generate unique identifiers on any device without connecting to a server and without the risk of collisions.

UUID Versions

There are several UUID versions, each with a different generation mechanism:

UUID v1 — Based on Time and MAC Address

Generated from the current timestamp (100-nanosecond intervals since October 15, 1582) and the network interface MAC address. Guarantees uniqueness, but reveals information about the creation time and device. From a UUID v1, you can determine when and on which computer it was created — this is a potential privacy concern.

UUID v4 — Random

The most popular version. 122 out of 128 bits are randomly generated (6 bits are reserved for version and variant designation). UUID v4 contains no information about time or device — it is pure randomness. This is the variant used in most modern applications.

UUID v7 — Based on Unix Timestamp (New)

Proposed in the new specification (RFC 9562, 2024). The first 48 bits are a Unix timestamp in milliseconds; the rest are random. The advantage: UUID v7 values are sortable by time, which significantly improves performance when used as primary keys in databases (no B-tree index fragmentation).

UUID v4 Collision Probability

UUID v4 has 122 bits of randomness, yielding 2^122 (approximately 5.3 × 10^36) possible values. To estimate collision probability, the "birthday paradox" is used:

  • When generating 1 billion UUID v4 values, the probability of at least one collision is about 0.00000000000000003% (3 × 10^{-17}).
  • To reach a 50% collision probability, you would need to generate approximately 2.7 × 10^18 UUIDs — that is 2.7 quintillion identifiers.
  • If you generated 1 billion UUIDs per second, it would take 85 years for the collision probability to reach 50%.

In practice, UUID v4 collisions simply do not occur. This is mathematically proven reliability, not just "unlikely."

Where UUIDs Are Used

Databases

UUID is often used as a primary key instead of an auto-incrementing number. Advantages: IDs can be generated on the client before insertion, data from different databases is easy to merge, and there is no predictability (users cannot enumerate IDs). The downside of UUID v4: randomness degrades B-tree index performance. The solution is UUID v7 or ULID, which are time-sortable.

APIs and Distributed Systems

In a microservices architecture, each service can generate UUIDs independently. This eliminates the need for a central ID-issuing service and removes a single point of failure. UUIDs are used to identify requests (correlation IDs), transactions, sessions, and resources.

File Systems and Disks

In Linux, disk partitions are identified by UUID (the blkid command). This is more reliable than device names (/dev/sda1), which can change after a reboot or when new disks are connected.

UUID vs. Alternatives

  • Auto-increment — simple, compact, but requires a central database and is predictable. UUID is better for distributed systems.
  • ULID (Universally Unique Lexicographically Sortable Identifier) — time-sortable, more compact than UUID in text form. A good alternative to UUID v4 for databases.
  • Snowflake ID (Twitter/Discord) — a 64-bit ID, time-sortable, with a built-in machine ID. More compact than UUID, but requires machine ID coordination.
  • NanoID — a compact random ID with a customizable alphabet and length. Popular for URL-friendly identifiers.

How to Generate a UUID

Most languages have built-in tools:

  • JavaScript: crypto.randomUUID() (in modern browsers and Node.js 19+).
  • Python: import uuid; uuid.uuid4().
  • PHP: Str::uuid() (Laravel) or ramsey/uuid.
  • PostgreSQL: gen_random_uuid().
  • MySQL: UUID() (generates v1).

For quick UUID generation without code, use our UUID generator. It creates cryptographically secure UUID v4 values right in your browser.

Frequently Asked Questions

How many bytes does a UUID take?

A UUID is 128 bits, or 16 bytes in binary. In text format (with hyphens) — 36 characters. In databases, it is recommended to store UUIDs as a binary type (BINARY(16) in MySQL, UUID in PostgreSQL) rather than a string — this saves space and speeds up indexes.

How is a UUID different from a hash?

A UUID is a random (or time-based) identifier not tied to any data. A hash is the result of a deterministic function applied to input data. The same data always produces the same hash, but a different UUID is generated each time. UUIDs are used for identification; hashes are used for integrity verification.

Why does UUID v4 perform poorly as a primary key?

UUID v4 is completely random, which causes B-tree index fragmentation in databases: new records are inserted at random positions in the tree rather than at the end. This slows down inserts and increases disk I/O. The solution is to use UUID v7, ULID, or sortable UUID variants where the first bytes contain a timestamp.

How can I quickly generate a UUID?

Open our UUID generator and get one or more UUID v4 values with a single click. Identifiers are generated in your browser using a cryptographically secure random number generator.

Понравилась статья?

Оцените — это помогает нам делать контент лучше

Change rating

Your rating:

Thanks for your rating!

Comments

Log in to leave a comment

No comments yet. Be the first!

We use cookies for site operation and analytics. Подробнее

Upscaled image
Download

Log in to continue

or