Does Using UUID as PK Hurt Index Performance?
Code Review Comment
While building the Gift API at Kakao Tech Campus, I used UUIDs as member identifiers. My mentor left this comment in a code review:
“Since you’re using UUID, you might want to look into ULID as well.”
After researching ULID, I had some questions and asked:
“Does using UUID as a PK really hurt performance at scale? I’ve heard UUIDv7 addresses this—is ULID still preferred over UUIDv7?”
Why Is UUID v4 Slow?
Mentor’s response:
“Yes, it does. At large scale, even small performance issues can cascade into service outages.”
UUID v4 is completely random, and that’s where the B-Tree index runs into problems.
Auto Increment (Sequential Insert)
- New data always appends to the end of the index
- Page splits rarely happen
UUID v4 (Random Insert)
- New data lands anywhere in the index
- Frequent page splits
- Fragmented index slows down reads too
So Is ULID the Answer?
Not necessarily. Mentor’s response:
“I wouldn’t say ULID is more recommended. I actually learned about UUIDv7 thanks to you. Looking into it, UUIDv7 is being standardized. I’d recommend UUIDv7 over ULID—though ULID is a bit shorter due to extra encoding.”
| Property | UUID v4 | ULID | UUIDv7 |
|---|---|---|---|
| Time-sortable | No | Yes | Yes |
| Standard | RFC 4122 | Non-standard | RFC 9562 |
| Length | 36 chars | 26 chars | 36 chars |
ULID is fine, but it’s non-standard. Since UUIDv7 is now a standard, there’s no strong reason to choose ULID instead.
When Randomness Is Actually Beneficial
My mentor shared an interesting perspective:
“If your goal is to distribute data, UUID v4’s randomness could actually be an advantage.”
In a sharding environment, random keys can prevent data from clustering on specific shards.
Summary
- UUID v4 is slower. But you need to understand why.
- In most cases, the standardized UUIDv7 is the better choice.
- If data distribution is the goal, UUID v4 is viable.
- Pick what fits your situation.
From Kakao Tech Campus 3rd cohort Gift API clone coding, summarizing discussions with my mentor.
