Scrivio Interview library
DesignIntermediate7 min read · updated 12 November 2026

A new order service must generate ids in the application (no database round trip), keep them sortable by creation time, and expose them in URLs. Compare UUIDv4, UUIDv7, ULID and Snowflake-style 64-bit ids, and choose one.

UUIDv4 needs no coordination and cannot be guessed, but its random order scatters index inserts and it takes 16 bytes. UUIDv7 puts a 48-bit millisecond timestamp in front of random bits, so it sorts by creation time, is standard (RFC 9562) and is supported by more and more databases and libraries, at the cost of revealing creation time. ULID is the same idea with a 26-character Crockford Base32 text form that sorts as text, and Snowflake ids pack a timestamp, worker id and sequence into 64 bits, which is compact but needs worker id assignment and careful clock handling. For this order service I would choose UUIDv7.

Background to this question

Choosing an id format feels like a small decision, but it is one of the hardest to change later: ids end up in URLs, logs, partner systems and every foreign key. The order service's three requirements, no database round trip, sortable by creation time and safe to expose in URLs, rule out auto-increment and point at a time-ordered scheme, but there are several and they differ in size, coordination and what they leak. In this answer, we will recap the requirements, look at the layout of each candidate, compare them on the things that matter, and then make and justify a choice.

1A Quick Recap of the Requirements

Generated in the application means each instance of the service creates ids itself, without asking a database sequence, so we can create an order and its events before anything is written and keep working if the database is slow. Sortable by creation time means ids created later compare as larger, which keeps database index inserts at the right edge and lets us page through orders by id. Exposed in URLs means the id is public: it should be URL-safe, reasonably short, and not let someone enumerate other customers' orders.

The last requirement deserves a note. An id being hard to guess is not access control; the service must still check that the caller owns the order. But ids that count up by one invite enumeration attempts and reveal business volume, so we prefer ids with some randomness.

2The Four Candidates and Their Layouts

All four schemes are ways to fill a fixed number of bits.

How each scheme fills its bits
SchemeSizeLayoutText form
UUIDv4128 bits122 random bits + version/variant36 chars, hex with hyphens
UUIDv7128 bits48-bit Unix ms + 74 random or counter bits36 chars, hex with hyphens
ULID128 bits48-bit Unix ms + 80 random bits26 chars, Crockford Base32
Snowflake64 bits41-bit ms since custom epoch + 10-bit worker + 12-bit sequenceup to 19 decimal digits
Three schemes are 128 bits; Snowflake halves the size by giving up randomness.

UUIDv4 is fully random. Generation needs nothing but a SecureRandom, and ids are unpredictable, but their order is random too, so a B-tree index on them takes inserts all over the tree, with page splits and poor cache use.

UUIDv7 keeps the UUID format but starts with a millisecond timestamp, so ids sort by creation time, and fills the rest with random bits, with an optional counter for ids created in the same millisecond. Because it is a standard UUID, every database uuid type, every UUID library and every tool that validates UUIDs accepts it. It reveals the creation time to the millisecond.

ULID has a similar layout, but defines its own text format: 26 characters of Crockford Base32, an alphabet without the confusing letters I, L, O and U, which sorts correctly as plain text. It is not an RFC standard, but its 128 bits can be stored in a uuid or BINARY(16) column.

3Snowflake-Style 64-Bit Ids

Snowflake ids, popularised by Twitter, fit in a single long: about 41 bits of milliseconds since a chosen epoch, which lasts around 69 years, a 10-bit worker id, allowing 1,024 generators, and a 12-bit sequence, allowing 4,096 ids per millisecond per worker.

Java
long id = (millisSinceEpoch << 22) | (workerId << 12) | sequence;
// millisSinceEpoch = 123_456_789, workerId = 7, sequence = 0
// id = 517815303929856 | 28672 | 0 = 517815303958528

Half the size of a UUID is attractive for very large tables and for indexes that must stay in memory. But the scheme has operational costs. Each running instance needs a unique worker id, which means a registry, a lease in ZooKeeper or a database, or a value derived from the pod name, and two instances with the same worker id produce duplicate ids. The clock must not go backwards: after an NTP correction, a generator must wait or refuse to issue ids, or it may repeat ones it already issued. And the ids have no randomness, so they are easy to enumerate and reveal volume. Finally, values above 2^53 lose precision in JavaScript, so they must travel as strings in JSON anyway.

4Comparing Them Against Our Requirements

Now we can compare the four directly.

Fit for the order service
ConcernUUIDv4UUIDv7ULIDSnowflake
No coordinationyesyesyesneeds worker ids
Sortable by timenoyesyesyes
Index localitypoorgoodgoodgood
Hard to guessyesmostly (random tail)mostly (random tail)no
Standard and toolingRFC 9562RFC 9562, growingown specown design
Storage16 bytes16 bytes16 bytes8 bytes
Leaks creation timenoyesyesyes
UUIDv7 meets all three requirements with standard tooling.

UUIDv4 fails the sorting requirement. Snowflake meets the sorting requirement but brings coordination and enumeration, which the other options avoid. ULID and UUIDv7 are close; the deciding factors are that UUIDv7 is an RFC standard, PostgreSQL's uuid type and Hibernate handle it as an ordinary UUID, and PostgreSQL 18 can even generate it with uuidv7(), while ULID needs its own libraries and validation everywhere.

5The Choice: UUIDv7, With Two Caveats

For most services, including this one, I would choose UUIDv7, generated in the application with a well-maintained library, since the JDK up to Java 25 has no built-in generator; check your version. In PostgreSQL it goes into a uuid column, in MySQL into BINARY(16) with the most significant bytes first so order is preserved. In URLs we can use the standard 36-character form, or encode the 16 bytes in Base32 or URL-safe Base64 for a shorter path segment.

The two caveats are about what the id reveals. First, it leaks creation time; for orders that is usually acceptable, and the order page shows the date anyway. Second, it is hard to guess only to the extent it carries randomness: some implementations use part of the random field as a counter, so ids generated in the same millisecond by one instance may be close together. So the API must always check authorisation, and if an id ever needs to be a secret, such as a guest checkout link, we generate a separate token from SecureRandom instead of relying on the id.

I would only move to a 64-bit scheme if measurements showed that index size really limits us, for example a table of tens of billions of rows whose indexes must fit in memory. Then the 8 bytes saved per key, multiplied across the table and every secondary index, may justify running a worker id service.

6Follow-up questions

What if two instances create UUIDv7 ids in the same millisecond?

They still differ, because the remaining 74 bits are random, so a collision is as unlikely as with version 4. Within one instance, good libraries use a counter or monotonic random increment for ids in the same millisecond, so they still sort in creation order.

Could we keep auto-increment internally and expose UUIDs publicly?

Yes, and it is a sound design when the database already exists: a BIGINT primary key for joins and a unique UUID column for the API. But it conflicts with the requirement to create ids without a database round trip, because the internal key only exists after the insert. For a new service, one UUIDv7 key is simpler.

How do clock problems affect UUIDv7?

Less than Snowflake. If the clock steps back, new ids may sort before earlier ones for a short while, but they remain unique because of the random bits. Ordering becomes approximate, which is fine for index locality and paging, but we should not use id order as the source of truth for business sequence; a created_at column does that.

7Conclusion

In this answer, we compared four ways to create ids in the application. UUIDv4 needs no coordination and is unpredictable but scatters index inserts. UUIDv7 adds a 48-bit millisecond timestamp, so it is time-sortable, standard under RFC 9562 and well supported, while revealing creation time. ULID is similar with a sortable Base32 text form but is not a UUID standard. Snowflake is compact at 64 bits but needs worker id assignment and clock care and is easy to enumerate. We chose UUIDv7, with authorisation checks and separate tokens where secrecy matters.

What to say in the interviewAbout 90 seconds aloud

Open with

The three requirements, no database round trip, time-sortable and safe in URLs, rule out auto-increment, so it comes down to these four. UUIDv4 needs no coordination and is unguessable, but its order is random, so a B-tree index takes inserts everywhere, with page splits and poor caching, and it fails the sortable requirement.

Then, if they want more

UUIDv7 keeps the 128-bit UUID format but starts with a 48-bit Unix millisecond timestamp, followed by random bits, so ids sort by creation time and index inserts go to the right edge. It is standardised in RFC 9562, works with every uuid column type, and PostgreSQL 18 can even generate it. The cost is that it reveals the creation time.

ULID is the same idea with a 26-character Crockford Base32 string that sorts as text, but it is not a UUID standard, so tooling is thinner. Snowflake ids fit in 64 bits, which halves index size, but every instance needs a unique worker id, the clock must never go backwards, and the ids are sequential enough to enumerate.

So I would choose UUIDv7, generated by a library since the JDK does not ship a generator up to Java 25, stored as uuid in PostgreSQL or BINARY(16) in MySQL.

I would still check ownership on every request rather than relying on the id being hard to guess, and I would only consider a 64-bit scheme if measurements showed index size was really limiting us.

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.
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 "A new order service must generate ids in the application (no database round trip), keep...": https://scrivio-interview-library.netlify.app/java/types-and-values/8992adb5de/