Stop using UUIDv4 for your primary keys. Use UUIDv7 instead.

As someone who has spent the better part of the last decade architecting and designing data platforms, I have often found myself in discussions where a solution technically works but still feels… out of place.
Using GUIDs. Specifically UUIDv4. As primary keys in database tables was one of those moments that made me instinctively go, “Eww brother”.
That reaction alone was not enough reason to dismiss the idea though. In engineering, most choices exist because they solve a real problem in a specific context. So instead of judging it outright, I went down the rabbit hole. I tried to understand why this pattern was popular, what advantages it actually offered, and where the trade-offs started to show up in real systems.
The Randomness
To be fair, UUIDv4 does solve a real problem. It gives you globally unique identifiers without any central coordination, which is valuable in distributed systems, offline writes, and multi-writer architectures. You can generate IDs anywhere, merge data later, and never worry about collisions or sequence contention. In systems where write volumes are low, indexes remain small, or performance characteristics are not critical, UUIDv4 can work perfectly well and stay a simple, robust default.
The core idea behind UUIDv4 is randomness. There is no ordering. Every generated value is effectively a random GUID. And this is where databases start to suffer.
Most database indexes are implemented using B-trees. If you have ever studied data structures, you will know that B-trees behave best when new keys arrive in roughly increasing order, allowing inserts to land near the leaf end of the tree. This keeps pages dense, minimises splits, and preserves locality.
With UUIDv4, every insert is a coin toss into the middle of the tree. The result is increased page splits, higher write amplification, and fragmented indexes. You still get uniqueness. You just pay for it with efficiency.
“Randomness is great for entropy, not for databases”
The new world order
Enter UUIDv7.
It is time-ordered, and encodes unix timestamp in milliseconds plus adds some random bits.

That one change makes a big difference!
- New IDs are mostly increasing over time
- Inserts become append-like in B-trees landing in those leaf nodes
- Index locality improves, reduced fragmentation
It is not “perfectly sequential” like a database sequence, but it is sequential enough for indexes to behave as they should.
Drawbacks?
In my view, UUIDv7 almost always makes sense for internal systems.
Where you need to pause is when IDs are exposed publicly. UUIDv7 encodes time, which means creation order and approximate timestamps can be inferred, down to millisecond granularity.
That can be undesirable in some contexts. For example, when UUIDs appear in public APIs or URLs, they can reveal record ordering and activity patterns, making it possible to infer traffic volumes, business events, or system usage over time. In multi-tenant or regulated environments, this may unintentionally leak operational signals across customers. It also slightly narrows the search space for ID enumeration if access controls or rate limiting are imperfect.
The trade-off is not about correctness. It is about whether that extra information is acceptable outside your organisation’s trust boundary.
Try it out!
For python 3.14 or later, You can generate a UUIDv7 directly using the built-in uuid module.

If you are using python versions earlier than 3.14, you will need to install uuid-v7 library.

Final Thoughts
You don’t have to undertake an exercise to switch everything over to UUIDv7. A practical approach I use is as below:
- New tables: default to UUIDv7 if you need UUIDs
- Existing tables with UUIDv4 PKs: only change if you have a real pain signal (write latency, bloated indexes, high fragmentation, hot tables)
Changing primary keys is expensive for production systems, and it leaks into foreign keys, replication, and downstream systems. If you do not have a performance problem, do not create a migration project just for the sake of vanity.
But if you are building new systems, UUIDv4 is the wrong default. If you care about index performance, UUIDv7 is the first UUID that feels like it was designed with databases in mind. And once you have used it for a while, going back to v4 as a primary key feels like choosing pain on purpose.
Simplest mental model to use would be
- UUIDv4: “unique, but chaotic”
- UUIDv7: “unique, and roughly ordered”
Don’t be the Engineer that says “It worked fine in the diagram.”!