A UUID v4 follows the pattern xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx, where each x is a random hexadecimal digit and y is one of 8, 9, a, or b.
xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
128 bits total
A UUID is a 128-bit value, displayed as 32 hexadecimal characters (0-9, a-f) separated by hyphens into five groups (8-4-4-4-12).
Version nibble: 4
The 13th hex digit is always 4, identifying this as a version 4 (random) UUID. This occupies bits 48-51.
Variant bits: 8-b
The 17th hex digit is constrained to 8, 9, a, or b, marking it as an RFC 4122 variant. This uses 2 of the 128 bits.
122 random bits
After accounting for the 4 version bits and 2 variant bits, 122 bits are cryptographically random - yielding 5.3 × 1036 possible UUIDs.
What Are UUIDs?
A universally unique identifier (UUID) is a 128-bit number used to identify information in computer
systems.
Wikipedia
→
We use IDs to identify things, and usually sequential numbers are used (e.g. #1,
#2, #3).
But sequential IDs require knowing the previous value and a central authority to assign them - inviting
race conditions and extra coordination.
UUIDs are a popular alternative. Version 4 UUIDs are generated from 122 bits of cryptographically secure
randomness,
making collisions so unlikely that you're more likely to
be hit by a meteorite
than to generate the same UUID twice. They can be created independently by any system, at any time, with
no coordination required.
BTW, want to see something cool? Someone built everyuuid.com which lists every possible UUID v4. All 5.3 × 1036
of them.
UUID Version Comparison
UUID v4 is the most widely used version, but other versions serve different purposes.
UUID v1
UUID v4
UUID v7
Basis
Timestamp + MAC address
Random
Timestamp + random
Sortable
Partially
No
Yes (time-ordered)
Privacy
Leaks MAC & time
Fully opaque
Leaks creation time
Collision risk
Extremely low
Extremely low
Extremely low
Random bits
~14
122
~62
Best for
Legacy systems
General purpose IDs
Database primary keys
Spec
RFC 4122
RFC 4122
RFC 9562
Common Use Cases
Database Primary Keys
UUIDs let you generate IDs client-side before inserting into the database, avoiding round-trips and enabling offline-first architectures.
Distributed Systems
Multiple services can independently generate unique IDs without coordination, eliminating single points of failure in ID generation.
Session Tokens
The 122 random bits in UUID v4 provide sufficient entropy for session identifiers that are practically impossible to guess.
Idempotency Keys
Attach a UUID to each API request so servers can detect and safely deduplicate retries without processing the same operation twice.
File & Resource Naming
Generate collision-free filenames for uploads, temporary files, or cloud storage objects without checking for existing names.
Testing & Fixtures
Use UUIDs in test data to ensure each test run has unique identifiers, preventing flaky tests caused by ID conflicts.
Generate UUIDs in Code
import uuid
id= uuid.uuid4()
print(id) # e.g. 7c9e6679-7425-40de-944b-e07fc1f90ae7
// Node.js & modern browsersconst id = crypto.randomUUID();
console.log(id); // e.g. 7c9e6679-7425-40de-944b-e07fc1f90ae7
package main
import (
"fmt"
"github.com/google/uuid" // go get github.com/google/uuid
)
func main() {
id := uuid.New()
fmt.Println(id) // e.g. 7c9e6679-7425-40de-944b-e07fc1f90ae7
}
require 'securerandom'
id = SecureRandom.uuid
puts id # e.g. 7c9e6679-7425-40de-944b-e07fc1f90ae7
# Single UUID
curl uuid4.com
# Multiple UUIDs
curl "uuid4.com/?count=5"
# JSON format
curl "uuid4.com/?format=json"
Frequently Asked Questions
Is a UUID v4 truly unique?
For all practical purposes, yes. With 122 random bits there are over 5.3 × 1036 possible values. You would need to generate about 2.7 × 1018 UUIDs (2.7 quintillion) before there is even a 50% chance of a single collision. To put that in perspective, if you generated one billion UUIDs per second it would take over 85 years to reach that point.
Can I use UUIDs as database primary keys?
Yes, and it's a common pattern. The main trade-off is that UUID v4 values are random, so they don't sort chronologically and can cause B-tree index fragmentation in some databases. If sort order matters, consider UUID v7 which embeds a timestamp. Many databases (PostgreSQL, MySQL 8+) have native UUID column types that store them efficiently as 16 bytes rather than 36-character strings.
What is the collision probability?
Vanishingly small. The probability of generating two identical UUID v4 values is approximately 1 in 2122. In concrete terms: if every person on Earth generated 600 million UUIDs, the probability of any two being the same would be about 50%. You are significantly more likely to be struck by a meteorite than to generate a duplicate UUID.
UUID vs ULID vs Nano ID?
UUID v4 is the universal standard - supported everywhere, well understood, and defined by RFC 4122. ULID (Universally Unique Lexicographically Sortable Identifier) adds time-ordering and is Crockford Base32 encoded, making it more compact and sortable. Nano ID is a smaller, URL-friendly alternative often used in frontend applications. For most backend and API use cases, UUID v4 remains the safest choice due to its ubiquity.
Are UUID v4 values sequential or predictable?
No. UUID v4 is generated from a cryptographically secure random number generator (CSPRNG). There is no pattern, sequence, or way to predict the next UUID from previous ones. This makes them suitable for use as security tokens, though for high-security applications a purpose-built token generator may be preferable.
How are UUID v4 values generated?
A UUID v4 is produced by generating 128 random bits from a cryptographically secure source, then setting 4 bits to the version number (0100 for v4) and 2 bits to the variant (10 for RFC 4122). The remaining 122 bits are pure randomness. The result is formatted as 32 hex digits in the 8-4-4-4-12 pattern.
What RFC defines UUIDs?
RFC 4122 (2005) originally defined UUID versions 1-5. RFC 9562 (2024) supersedes it, adding versions 6, 7, and 8 while maintaining backwards compatibility with existing UUID versions.
About This Site
Built during a hack day as a way to provide "UUIDs as a service" - a quick way to demonstrate and
generate
UUIDs for use in code samples without needing a library. This service is not
intended for serious or critical use.
Built by Alun Davey. Ads are included with the hope of covering domain
and hosting costs.