UUID Generator: v4 vs v7 and Which One to Use

UUID generator output comparing a random UUID v4 and a time-ordered UUID v7 in the standard 8-4-4-4-12 hexadecimal format

Auto-increment IDs are fine until they aren’t. Move to a multi-region database, merge two systems, or expose /orders/1043 in a public URL, and sequential numbers become a liability: collisions across nodes, and a free business-metrics feed for anyone who can count. That’s where a uuid generator earns its place. It produces identifiers that are unique without asking a central database for permission, so any service, client, or region can mint IDs on its own.

This guide covers what those IDs actually contain, how the versions differ, why more teams are moving from v4 to v7, and the security habits that keep you out of trouble.

What a UUID Generator Actually Produces

A UUID (Universally Unique Identifier, called a GUID in Microsoft land) is a 128-bit value, written as 32 hexadecimal digits in five groups separated by hyphens: 8-4-4-4-12, 36 characters in total.

019535d9-3df7-79fb-b466-fa907fa17f9e   (a v7 example)
41db1265-8bc1-4ab3-992f-885799a4af1d   (a v4 example)

Two spots in that string carry metadata. The first digit of the third group is the version (7 and 4 in the examples above). The first digit of the fourth group encodes the variant, which for standard UUIDs is 8, 9, a, or b. Everything else is payload: random bits, a timestamp, or a hash, depending on the version.

The format is standardized in RFC 9562, published in May 2024, which replaces the older RFC 4122 and adds versions 6, 7, and 8.

Why collisions basically don’t happen

A v4 UUID has 122 random bits (6 of the 128 are fixed for version and variant). By the birthday-problem math, you’d need to generate roughly 2.7 quintillion of them before the odds of a single collision reach 50%. At a billion per second, that’s about 85 years of nonstop generation.

The real-world caveat isn’t math, it’s your random source. A broken or low-entropy generator can collide long before the theory says it should. Use one backed by a cryptographically secure random number generator (CSPRNG), which every mainstream standard library provides.

UUID Versions Compared

VersionGeneration basisSortable?Best real-world use
v160-bit timestamp + node ID (often a MAC address)Not directly (timestamp fields are stored low-bits first)Legacy systems that already depend on it
v4122 cryptographically random bitsNoOpaque public IDs, tokens, test data
v5SHA-1 hash of a namespace + a nameNoDeterministic IDs: same input always gives the same UUID
v748-bit Unix millisecond timestamp + random bitsYesModern database primary keys

A few notes. v3 is the MD5 cousin of v5, and v5 is generally preferred. v6 is a reordered v1 for systems that must stay compatible with it. v8 is a free-form slot for custom layouts. For most new work, you’re choosing between v4 and v7, with v5 as a niche tool when you need the same input to always map to the same ID.

UUID v4 vs UUID v7: Why Your UUID Generator Should Switch

For years, “use UUIDs as primary keys” came with an asterisk: they hurt database performance. The reason is how indexes work.

Most databases store primary keys in a B-tree index, kept in sorted order. When new keys arrive in increasing order, each insert lands at the right-hand edge of the tree: the same hot pages stay in cache, and pages fill up neatly.

Random v4 keys land everywhere. Each insert may touch a different leaf page, which has to be read from disk if it isn’t cached. When that page is full, it splits, producing two half-empty pages. The result is index fragmentation, lower cache hit rates, more write amplification (in PostgreSQL, a touched page can also trigger extra full-page writes to the write-ahead log), and a bigger, slower index.

v7 fixes this by putting the time first. Its first 48 bits are a millisecond Unix timestamp, so new IDs sort after older ones and inserts append to the right edge of the index, just like a sequence would. You keep everything good about UUIDs (generate anywhere, no coordination) and get most of the insert locality back. A uuid generator that outputs v7 by default is the simplest change that fixes the old “UUIDs are slow” complaint.

Support has arrived across the stack:

  • PostgreSQL 18 ships a built-in uuidv7() function.
  • Python 3.14 added uuid.uuid7() to the standard library.
  • JavaScript’s popular uuid package supports v7.

Some honest caveats:

  • v7 exposes creation time. Anyone holding the ID can read when the record was made. That’s usually fine internally and a bad idea for IDs you expose to users.
  • Ordering is by millisecond. IDs from different machines inside the same millisecond aren’t strictly ordered, and clock drift between servers can nudge things out of sequence. Keep clocks synced with NTP.
  • Store them natively. Use PostgreSQL’s uuid type (16 bytes), not a 36-character string. In MySQL, store as BINARY(16). This matters a lot for unique identifier database design, since every secondary index carries the primary key along with it.

How High-Volume Platforms Use a UUID Generator

Distributed systems running outreach, tracking, and verification at scale need every event to be traceable. Think of a platform like the one behind linksbuilderhub.com: pitch events, backlink verification jobs, client report snapshots, and partner webhook payloads all flow through different services.

A pattern that works well for that kind of workload:

  • Assign a correlation ID at the edge, the moment an event enters the system, and pass it through every service, queue, and log line. One search then reconstructs the full story of a single pitch or job.
  • Use time-ordered v7 IDs for event and job tables, so inserts stay fast and “show me the latest” queries stay cheap.
  • Use idempotency keys for webhooks. If a partner retries a payload, the same ID tells you it’s a duplicate instead of a new event.
  • Keep IDs immutable. Never reuse or rewrite them. They’re the join key for your whole audit trail.
  • Run a dedicated uuid generator routine in one shared library, so every service produces IDs the same way and you don’t end up with three formats across five microservices.

This describes a common architecture pattern for high-volume event pipelines, not the internals of any specific company.

Security Rules and One-Liners for Any UUID Generator

Don’t expose v1 in public endpoints. Version 1 can embed the generating machine’s MAC address along with a precise timestamp, which leaks both hardware identity and timing.

Be careful with v7 in public IDs too. It reveals creation time. A common approach is to use v7 as the internal primary key and a separate random v4 (or another opaque token) as the public identifier.

A UUID is not a secret. Even a random v4 shouldn’t be your only access control. “Unguessable URL” isn’t authorization. Always check permissions server-side.

Use a CSPRNG-backed generator. Avoid hand-rolled code built on Math.random().

Here are the quick ways to generate one. Most languages ship a built-in uuid generator, so you rarely need a library.

bash

# Linux (util-linux)
uuidgen -r          # random (v4)
uuidgen -t          # time-based (v1)
uuidgen --help      # newer versions may add more options

javascript

// Node.js: v4 from the built-in crypto module
import { randomUUID } from "node:crypto";
console.log(randomUUID());

// v7 via the uuid package
import { v7 as uuidv7 } from "uuid";
console.log(uuidv7());

python

import uuid
print(uuid.uuid4())   # random
print(uuid.uuid7())   # time-ordered, Python 3.14+

sql

-- PostgreSQL 18
SELECT uuidv7();
SELECT gen_random_uuid();  -- v4

If you just need a handful of IDs for testing or seeding, an online uuid generator is fine. Don’t paste anything sensitive into one, and prefer a local command for anything production-related. If you need to generate GUID online for a quick mock payload, the same rule applies.

Best practices checklist

  • Generate IDs in the application layer when you can, with one uuid generator library shared across services, so formats stay consistent.
  • Store as native 16-byte types, not strings.
  • Pick the version by exposure: v7 for internal keys, v4 for public tokens, v5 for deterministic mapping.
  • Never parse meaning out of IDs in application logic, even if v7 contains a timestamp. Store real timestamps in real columns.
  • Sync server clocks if you rely on v7 ordering.

Choosing Between v4 and v7: The Short Version

For most modern web apps, the choice comes down to two rules:

  1. Internal primary keys: use v7. You get distributed generation and index-friendly inserts.
  2. Anything public-facing or security-sensitive: use random v4 (or another opaque token), so you don’t leak timing.

Whichever you pick, make sure your uuid generator relies on a secure random source, and store the result in a native type. Get those three things right, and UUIDs stop being a performance debate and go back to being a boring, reliable tool.

Leave a Comment

Your email address will not be published. Required fields are marked *