Quick revision
The short answer
UUID.randomUUID() produces a version 4 UUID: 122 random bits from a shared SecureRandom, plus 6 fixed bits for the version and variant. UUID.nameUUIDFromBytes(bytes) produces a name-based version 3 UUID from an MD5 hash, so the same input always gives the same UUID; version 5 with SHA-1 needs a library or a few lines of our own code. Random UUIDs make poor clustered primary keys in MySQL InnoDB because InnoDB stores rows in primary key order, so random keys insert into random pages, causing page splits, poor cache use and larger indexes; time-ordered version 7 UUIDs stored as BINARY(16) avoid most of that.
What to remember
In this answer, we saw that UUID.randomUUID() creates a version 4 UUID with 122 random bits from a shared SecureRandom, and that UUID.nameUUIDFromBytes creates a deterministic version 3 UUID from an MD5 hash, while version 5 needs a library or our own code. Random keys hurt InnoDB because its clustered index keeps rows in key order, so random inserts cause page splits, half-empty pages, random I/O and larger secondary indexes. Time-ordered version 7 UUIDs from RFC 9562, stored as BINARY(16) or a native uuid column, avoid most of these costs.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
UUID.randomUUID() produces a version 4 UUID: 122 random bits from a shared SecureRandom, plus 6 fixed bits for the version and variant. UUID.nameUUIDFromBytes(bytes) produces a name-based version 3 UUID from an MD5 hash, so the same input always gives the same UUID; version 5 with SHA-1 needs a library or a few lines of our own code. Random UUIDs make poor clustered primary keys in MySQL InnoDB because InnoDB stores rows in primary key order, so random keys insert into random pages, causing page splits, poor cache use and larger indexes; time-ordered version 7 UUIDs stored as BINARY(16) avoid most of that.
Why this question is asked
UUIDs are the default answer to how do we create ids without asking the database, and Java makes them one method call away. But the call hides choices that matter later: which version we get, whether the same input should give the same id, and how the id behaves inside a database index. Interviewers combine these into one question because a candidate who knows the bit layout can also explain the performance problem. In this answer, we will recap what a UUID is, look inside randomUUID(), create name-based UUIDs, see how InnoDB's clustered index reacts to random keys, and then choose better key types and storage.
1A Quick Recap of UUIDs
A UUID, a universally unique identifier, is a 128-bit value usually written as 36 characters: 32 hexadecimal digits in five groups separated by hyphens, such as 4f416b0c-12aa-3c75-8209-da955fb755cc. The standard, now RFC 9562 from 2024, defines several versions that differ in how the 128 bits are filled. A few bits are always reserved: four bits for the version, which is the first digit of the third group, and two bits for the variant.
The versions we meet most are version 4, which is random; versions 3 and 5, which are computed from a name with a hash; and version 7, which starts with a timestamp so that ids sort by creation time. Java's java.util.UUID class can represent any version and creates versions 4 and 3.
| Version | How the bits are filled | Java support |
|---|---|---|
| 3 | MD5 hash of a name | UUID.nameUUIDFromBytes |
| 4 | 122 random bits | UUID.randomUUID |
| 5 | SHA-1 hash of namespace + name | library or own code |
| 7 | 48-bit Unix ms time + random bits | library; check your JDK version |
2Inside UUID.randomUUID()
randomUUID() asks a SecureRandom for 16 bytes, then overwrites the version and variant bits. The SecureRandom is created once, lazily, and shared by all callers in the JVM.
So of the 128 bits, 4 are the version and 2 are the variant, which leaves 122 random bits. That is enough that collisions are not a practical concern: we would have to generate around a billion UUIDs per second for several decades before the chance of even one duplicate reached 50 percent. Because the source is SecureRandom, version 4 UUIDs are also unpredictable, which is why they are acceptable in URLs where guessing an id should be hard.
3Creating Name-Based UUIDs
Sometimes we want the opposite of random: the same input should always give the same id, for example so that two services importing the same external record agree on its id without talking. That is what name-based UUIDs are for.
nameUUIDFromBytes hashes the bytes with MD5 and sets the version 3 bits. Notice one detail: the RFC defines name-based UUIDs as the hash of a namespace UUID followed by the name, so that the same name in two namespaces gives different ids. Java's method hashes only the bytes we pass, so to follow the standard we prepend the namespace's 16 bytes ourselves. Version 5 works the same way with SHA-1, which is preferred for new designs; the JDK has no factory for it, so we use a library or build it with MessageDigest and set the version bits by hand.
4Why Random Keys Hurt an InnoDB Clustered Index
InnoDB, MySQL's storage engine, stores each table as a clustered index: a B+ tree whose leaves are the table rows themselves, kept in primary key order in pages of 16 KB. With an auto-increment key, each new row has the largest key so far, so it goes at the end of the last leaf page. When that page fills, InnoDB starts a new one. Pages end up nearly full, and the pages being written are always the same few at the right edge of the tree, which stay in memory.
With a random UUID key, each new row belongs at a random place in the key order. InnoDB has to read that page into the buffer pool, its memory cache, if it is not already there, and insert into the middle. When a page in the middle is full, it splits into two half-full pages. So random keys cause three costs: many page splits, pages that average well below full, which makes the table and indexes larger, and random reads across the whole index once it no longer fits in memory, which is when insert speed falls sharply.
- Compute the new keyauto-increment, UUIDv4 or UUIDv7
- Key larger than all existing keys?YesAppend to the last pageNoFind the page in the middle
- Is that page in memory?YesUse itNoRead it from disk
- Is the page full?YesSplit it into two half-full pagesNoInsert the row
- Update secondary indexeseach stores a copy of the primary key
5 steps in order. Play them, or step through them one at a time.
Sequential keys follow the first branch almost every time; random keys take the slow branches.
There is a fourth cost that people miss: every secondary index in InnoDB stores the primary key of the row as its pointer. A 36-character string key adds 36 or more bytes to every entry of every secondary index, compared with 16 bytes for BINARY(16) and 8 for a BIGINT.
5Better Choices: Version 7 and Compact Storage
A version 7 UUID puts a 48-bit Unix timestamp in milliseconds in its first bits, followed by random bits. New ids are therefore larger than older ones, inserts go mostly to the right edge of the index, and we keep the benefit of generating ids in the application. The JDK's UUID class can hold a version 7 value, but releases up to Java 25 do not include a generator for it, so check your version or use a library such as java-uuid-generator or uuid-creator. Hibernate also has time-based UUID generation strategies; check your Hibernate version for the exact options. PostgreSQL 18 adds a uuidv7() function, if we prefer the database to generate them.
Storage matters as much as the version. In PostgreSQL, the native uuid type stores 16 bytes and Hibernate maps java.util.UUID to it directly. In MySQL, BINARY(16) stores 16 bytes; MySQL's UUID_TO_BIN and BIN_TO_UUID functions convert to and from text. A CHAR(36) column, the common default, more than doubles the key size and compares slowly as text.
Writing the most significant bits first keeps the byte order the same as the text order, so a version 7 UUID stored this way still sorts by time inside the index.
6Follow-up questions
Is randomUUID() a performance problem under high concurrency?
Usually not, but it can be. All callers share one SecureRandom, and its implementation serialises access, so at very high rates, hundreds of thousands of ids per second from many threads, threads queue on that lock. A profiler showing threads waiting inside SecureRandom confirms it. The fixes are to generate ids from a few SecureRandom instances, for example per thread, and build the UUID from their bytes, or to use a version 7 library designed for high rates. In most services, the database insert costs far more than the UUID.
Does a version 7 UUID leak information?
Yes, its creation time to the millisecond. For order or user ids exposed in URLs that is often acceptable, but for anything where timing is sensitive, such as account creation dates, a random version 4 public id may be better, possibly with a separate internal key.
Why not just use BIGINT auto-increment?
It is the smallest and fastest key for a single database, and still a good choice. The trade-offs are that ids need a database round trip, are guessable and reveal volume, and become awkward when merging data from several databases. UUIDs remove those problems at the cost of size.
7Conclusion
In this answer, we saw that UUID.randomUUID() creates a version 4 UUID with 122 random bits from a shared SecureRandom, and that UUID.nameUUIDFromBytes creates a deterministic version 3 UUID from an MD5 hash, while version 5 needs a library or our own code. Random keys hurt InnoDB because its clustered index keeps rows in key order, so random inserts cause page splits, half-empty pages, random I/O and larger secondary indexes. Time-ordered version 7 UUIDs from RFC 9562, stored as BINARY(16) or a native uuid column, avoid most of these costs.
Open with
UUID.randomUUID gives a version 4 UUID. It takes 16 bytes from a shared SecureRandom and overwrites four bits for the version and two for the variant, so 122 bits are random, which makes collisions practically impossible and the ids unpredictable.
Then, if they want more
For a name-based UUID, Java has UUID.nameUUIDFromBytes, which hashes the bytes with MD5 and gives a version 3 UUID, so the same input always gives the same id; to follow the RFC properly I would prepend a namespace UUID, and for version 5, which uses SHA-1, I would use a library because the JDK has no factory for it.
The problem with random UUIDs in InnoDB is that the table is a clustered index: rows are stored in primary key order in 16 KB pages. Sequential keys always append at the end, so pages fill up and the hot pages stay in memory.
Random keys land anywhere, so InnoDB reads random pages, splits full pages into half-empty ones, and once the index is bigger than the buffer pool, inserts slow down a lot. Every secondary index also stores the primary key, so a 36-character string key bloats all of them.
So I would use a time-ordered version 7 UUID from RFC 9562, generated by a library unless your JDK version provides one, and store it as BINARY(16) in MySQL or the native uuid type in PostgreSQL.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 25, PostgreSQL 18
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "How does UUID.randomUUID() work, what version does it produce, and how would you create...": https://scrivio-interview-library.netlify.app/java/types-and-values/8207d2ec92/