Developer Playground

UUID Generator

A UUID (Universally Unique Identifier) is used to generate a unique identifier. Each version has a different generation method and purpose.


UUID v4 (Random-based)

Completely randomly generated and widely used. The possibility of collision is extremely low.

Result:Click the button to generate a UUID v4

UUID v7 (Sortable by Time)

Generated by combining a Unix timestamp with a random value. It is sortable by time and advantageous for database indexing.

Result:Click the button to generate a UUID v7

UUID v1 (Time-based)

Generated based on the current time and MAC address. It is unique but may expose time information.

Result:Click the button to generate a UUID v1

UUID v5 (Name-based, SHA-1)

Generated by converting a namespace and name string into an SHA-1 hash. It is more secure than v3.

Result:Enter namespace and name, then click the button

UUID v3 (Name-based, MD5)

Generated by converting a namespace and name string into an MD5 hash. Less secure than v5 but faster.

Result:Enter namespace and name, then click the button

What is a UUID?

A UUID (Universally Unique Identifier) is a 128-bit label used for information in computer systems. The intent of UUIDs is to enable distributed systems to uniquely identify information without significant central coordination. In its canonical textual representation, the 16 octets of a UUID are represented as 32 hexadecimal digits, displayed in five groups separated by hyphens (8-4-4-4-12).

Understanding UUID Versions

UUID v4 (Random)

Version 4 UUIDs are generated using random or pseudo-random numbers. Out of the 128 bits, 122 bits are random. This is the most common version used in modern applications where simple uniqueness is required.

UUID v7 (Time-Ordered)

The new UUID v7 is specifically designed for use as database primary keys. It combines a 48-bit Unix timestamp with random bits. Because the first part is time-based, UUID v7 values are monotonically increasing, which drastically improves database B-tree indexing performance by reducing page splits.

UUID v1 (Time & MAC)

Version 1 uses the current timestamp and the MAC address of the generating computer. While extremely unique, it has fallen out of favor because it leaks the hardware identity and the exact time of creation.

UUID v3 & v5 (Deterministic)

These are name-based UUIDs. Version 3 uses MD5 hashing, and Version 5 uses SHA-1. They are useful when you need to generate a consistent ID for the same input string within a specific namespace.

Probability of Collision

How likely is it to generate two identical UUIDs? For UUID v4, the probability is astronomical. To have a 50% chance of a collision, you would need to generate 1 billion UUIDs per second for about 85 years. For most practical applications, the risk is effectively zero.

Database Optimization Tips

When using UUIDs as primary keys in databases like MySQL, PostgreSQL, or MongoDB:

  • Prefer UUID v7: As mentioned, its time-ordered nature is much friendlier to indexes than the completely random UUID v4.
  • Store as Binary: Instead of storing UUIDs as 36-character strings (which take 36 bytes), store them in a native UUID type or a BINARY(16) column to save space and improve performance.
  • Avoid v1 in Public APIs: Since v1 contains the MAC address, it can pose a security risk if exposed to users.