GameParticles
Random & generators

UUID v4 vs v7: Random or Time-Ordered, Which to Pick

UUID v4 is 122 random bits; UUID v7 starts with a millisecond timestamp so IDs sort by creation time. See how each affects database indexes and which to use.

Updated

UUID v4 and UUID v7 are both 128-bit identifiers in the same 36-character format. Version 4 is entirely random, so consecutive IDs have no relationship to each other. Version 7 puts a millisecond timestamp in the first 48 bits and fills the rest with random data, so IDs sort in the order they were created. Use v7 for database keys in large tables, and v4 when an ID must reveal nothing about when it was made.

How each is laid out

A version 4 UUID such as f47ac10b-58cc-4372-a567-0e02b2c3d479 is random apart from two markers: the 4 that opens the third group, and the first digit of the fourth group, which is always 8, 9, a or b. That leaves 122 random bits.

A version 7 UUID such as 01a1e960-d600-7c3a-9f2b-4d5e6f708192 has three parts:

  1. The first 48 bits, which are the first 12 hex digits 01a1e960d600, hold the Unix time in milliseconds.
  2. The version digit 7 and the variant bits sit in the same positions as in every UUID.
  3. The remaining 74 bits are random.

In the example, 01a1e960d600 in decimal is 1794916800000, which is 12:00:00 UTC on November 17, 2026. Forty-eight bits of milliseconds will not run out until roughly the year 10889.

Both versions are defined in RFC 9562, published in May 2024, which replaced RFC 4122. Version 4 dates back to the older document; versions 6, 7 and 8 are new in RFC 9562. For the general structure and the other versions, see what a UUID is.

Side by side

PropertyUUID v4UUID v7
Random bits12274
TimestampNone48-bit Unix milliseconds
Sorts by creation timeNoYes, to the millisecond
Reveals creation timeNoYes
Index insert patternRandom positionsAppended at the end
Standardized inRFC 4122, kept in RFC 9562RFC 9562
Built-in supportNearly universalGrowing

Why ordering matters to a database

Most relational databases index primary keys with a B-tree, which keeps keys in sorted order across fixed-size pages. Where a new key lands depends on its value.

With an auto-increment integer, every new key is larger than the last, so inserts go to the rightmost page. That page stays in memory, fills up, and a new one is started. It is the cheapest possible pattern.

With version 4, each new key belongs at a random spot in the index. On a large table that has three effects:

  • Page splits. The target page is often full, so the database splits it in two, leaving both partly empty. The index grows larger than the data requires.
  • Cache misses. Any page is equally likely to be needed next, so once the index outgrows memory, inserts start waiting on disk reads.
  • More write volume. Scattered changes touch more pages, which increases logging and replication work.

In engines that store the table itself in primary key order, such as MySQL's InnoDB and SQL Server's default clustered index, the cost is higher because the row data is shuffled along with the key.

Version 7 restores the append pattern. New keys share a recent timestamp prefix and land at the right-hand edge of the index, like integers, while remaining globally unique without a central counter. The size of the gain depends on table size, hardware and workload, so measure rather than assume a figure, but the direction is consistent: the larger the table relative to memory, the more it matters.

Ordering has a second benefit. Sorting by ID approximates sorting by creation time, so "latest 50 rows" can use the primary key index, and a range of IDs corresponds to a range of time.

What v7 costs you

  • The creation time is public. Anyone holding the ID can decode when the record was made. That is harmless for an internal order row and less so for a public user ID if signup time is sensitive, or where it lets a competitor estimate activity.
  • Less randomness. 74 bits is still a vast space: a collision needs two IDs in the same millisecond that also match on all 74 random bits. But it is fewer than 122, and a v7 value is partly predictable. It is not a substitute for a secret token.
  • Order within a millisecond is not guaranteed by default. Two IDs created in the same millisecond may sort either way unless the generator uses one of the RFC's optional monotonic methods, such as a counter in the bits after the timestamp. Many libraries do; check yours if strict ordering matters.
  • Hot spots in some distributed databases. Systems that split data by key range can funnel every insert to the same node when keys are sequential. Those systems often recommend random keys, which is a case where v4 wins.

Which to pick

Pick v7 when:

  • The ID is a primary key or indexed column in a table that will grow large.
  • You want rows to sort by creation without a separate indexed timestamp.
  • IDs are generated across many services and still need to interleave in time order.

Pick v4 when:

  • The ID appears in public URLs or is handed to third parties and should carry no information.
  • You are using a range-sharded store that suffers from sequential keys.
  • You need the widest support with no extra library.
  • The table is small, in which case index locality will not be noticeable.

Do not mix the decision up with security. Neither version should be the only thing protecting a resource; use proper authorization, and generate real secrets with a cryptographic random source.

Generating each version

Version 4 is built in almost everywhere: crypto.randomUUID() in browsers and Node.js, uuid.uuid4() in Python, gen_random_uuid() in PostgreSQL, UUID.randomUUID() in Java, Guid.NewGuid() in .NET.

Version 7 support is newer. PostgreSQL 18 added a uuidv7() function, Python 3.14 added uuid.uuid7(), and .NET 9 added Guid.CreateVersion7(). On older runtimes, and in JavaScript, use a maintained library such as the uuid package on npm. Since both versions are ordinary UUIDs, they fit in the same column type, so a table can switch from v4 to v7 for new rows without a migration.

The UUID Generator creates version 4 UUIDs, up to 100 at a time, with uppercase and no-hyphen options. It does not produce version 7, so generate those in code with one of the functions above.

To read the time out of a v7 value by hand, strip the hyphens, convert the first 12 hex digits to decimal, and paste the result into the Unix timestamp converter, which reads a 13-digit value as milliseconds. Unix timestamps explained covers the seconds versus milliseconds distinction.

Try it

Generate a batch in the UUID Generator and sort the list: the order is arbitrary, which is exactly the property that scatters v4 keys across an index. Then compare with a few v7 values from your own runtime, where the first 12 digits barely change from one ID to the next.

Tools in this guide

Frequently asked questions

What is UUID v4?

UUID version 4 is an identifier made of 122 random bits plus 6 fixed bits that mark the version and variant. It carries no timestamp or machine information, and it is the default produced by most built-in UUID functions.

What is UUID v7?

UUID version 7 begins with a 48-bit Unix timestamp in milliseconds, followed by 74 random bits. Because the timestamp comes first, values generated later sort after earlier ones. It was standardized in RFC 9562 in May 2024.

Is UUID v7 better than v4 for primary keys?

Usually, for large tables with B-tree indexes. Time-ordered values are inserted at the end of the index instead of at random positions, which reduces page splits and keeps recent data in memory. For small tables the difference is negligible.

Can you extract the timestamp from a UUID v7?

Yes. Remove the hyphens, take the first 12 hexadecimal digits and convert them to a decimal number. The result is the creation time in milliseconds since the Unix epoch, which also means anyone who sees the ID can read when it was created.

More random & generators guides

JSON Formatter
Developer · Format, validate and minify JSON with syntax highlighting.
↵
Base64 Encode / Decode
Developer · Encode text to Base64 or decode Base64 back to plain text.
↵
URL Encoder / Decoder
Developer · Encode or decode URL components and query strings.
↵
Hash Generator
Developer · Generate MD5, SHA-1, SHA-256 and SHA-512 hashes.
↵
UUID Generator
Developer · Generate UUIDs v4 (random) in bulk with one click.
↵
Regex Tester
Developer · Test regular expressions against text with live match highlighting.
↵
Word Counter
Text · Count words, characters, sentences, paragraphs and reading time.
↵
Case Converter
Text · Convert text between UPPER, lower, Title, camelCase, snake_case and more.
↵
List Sorter
Text · Sort, reverse, deduplicate and shuffle lists of text.
↵
Markdown Preview
Text · Write Markdown and see a live rendered preview side by side.
↵
Percentage Calculator
Numbers · Calculate percentages, increases, decreases and what % one number is of another.
↵
Color Converter
Colors · Convert colors between HEX, RGB, HSL and HSV instantly.
↵
Snake
Arcade · Eat food, grow longer, don't hit the walls. A timeless classic.
↵
2048
Puzzle · Slide tiles and combine them to reach the legendary 2048 tile.
↵
Word Grid
Puzzle · Guess the hidden five letter word in six tries using color clues.
↵
Sudoku
Puzzle · Fill the 9x9 grid with logic. Four difficulties from easy to expert.
↵
Minesweeper
Strategy · Use number clues to clear the minefield. Classic logic, three levels.
↵
Breakout
Arcade · Smash every brick with paddle and ball. Power-ups, combos, endless levels.
↵
Typing Practice
Casual · Build your WPM and accuracy with timed drills from 30 seconds to 5 minutes. Find your weak keys.
↵
TypeRunner
Arcade · Multiplayer typing race. Race friends by link, beat computer runners, or chase your own ghost.
↵
Typing Blocks
Arcade · Type each word before its wooden block lands. Miss, and the pile climbs toward the line.
↵
Bubble Shooter
Arcade · Aim, bank shots off the walls, and pop groups of three or more bubbles.
↵
Road Rush
Arcade · Race seven rivals through traffic and punch your way to the front.
↵