Live data from Hacker News

Avoid UUID Version 4 Primary Keys in Postgres

andyatkinson.com

71–80 of 463 posts

Re: Avoid UUID Version 4 Primary Keys in Postgres

#71
post #51
post #29

Earlier quoted context omitted.

The article mentions microservices, which can increase the likelihood of collisions in sequential incremental keys. One more reason to stay away from microservices, if possible.

The 'collision' is two service classes both trying to use one db. If you separate them (i.e. microservices) the they no longer try to use one db.

There is nothing stopping multiple microservices from using the same DB, so of course this will happen in practice.

Sometimes it might even be for a good reason.

Re: Avoid UUID Version 4 Primary Keys in Postgres

#72
The is article is about a solution in search of a problem, a classic premature optimization issue. UUIDv4 is perfectly fine for many use cases, including small databases. Performance argument must be considered when there’s a problem with performance on the horizon. Other considerations may be and very often superior to that.

Re: Avoid UUID Version 4 Primary Keys in Postgres

#73
post #48

Earlier quoted context omitted.

Sticking with sequences and other integer types will cause problems if you need to shard later.

Especially in larger systems, how does one solve the issue of reaching the max value of an integer in their database? Sure for unsigned bigint thats hard to achieve but regular ints? Apps quickly outgrow that.

OK... but that concern seems a bit artificial.. if bigints are appropriate: use them. If the table won't get to bigint sizes: don't. I've even used smallint for some tables I knew were going to be very limited in size. But I wouldn't worry about smallint's very limited number of values for those tables that required a larger size for more records: I'd just use int or bigint for those other tables as appropriate. The reality is that, unless I'm doing something very specific where being worried about the number of bytes will matter... I just use bigint. Yes, I'm probably being wasteful, but in the cases where those several extra bytes per record are going to really add up.... I probably need bigint anyway and in cases where bigint isn't going to matter the extra bytes are relatively small in aggregate. The consistency of simply using one type itself has value.

And for those using ints as keys... you'd be surprised how many databases in the wild won't come close to consuming that many IDs or are for workloads where that sort of volume isn't even aspirational.

Now, to be fair, I'm usually in the UUID camp and am using UUIDv7 in my current designs. I think the parent article makes good points, but I'm after a different set of trade-offs where UUIDs are worth their overhead. Your mileage and use-cases may vary.

Re: Avoid UUID Version 4 Primary Keys in Postgres

#74
post #19

Earlier quoted context omitted.

Out of curiosity, why is it an issue if you leak creation time?

The issue will be very context specific. In other words to (reasonably) answer the question, we'd have to judge each application individually. For one example, say you were making voting-booth software. You really don't want a (hidden) timestamp attached to each vote (much less an incrementing id) because that would break voter confidentiality. More generally, it's more a underlying principle of data management. Not…

[deleted]

Re: Avoid UUID Version 4 Primary Keys in Postgres

#75
Personally my approach has been to start with big-ints and add a GUID code field if it becomes necessary. And then provide imports where you can match objects based on their code, if you ever need to import/export between tenants, with complex object relationships.

But that also adds complexity.

Re: Avoid UUID Version 4 Primary Keys in Postgres

#77
A much simpler solution is to keep your tables as they are (with an integer primary key), but add a non sequential public identifier too.

id => 123, public_id => 202cb962ac59075b964b07152d234b70

There are many ways to generate the public_id. A simple MD5 with a salt works quite well for extremely low effort.

Add a unique constraint on that column (which also indexes it), and you'll be safe and performant for hundreds of millions of rows!

Why do we developers like to overcomplicate things? ;)

Re: Avoid UUID Version 4 Primary Keys in Postgres

#79

A prime example of premature optimization. Permanent identifiers should not carry data . This is like the cardinal sin of data management. You always run into situations where the thing you thought, "surely this never changes, so it's safe to squeeze into the ID to save a lookup". Then people suddenly find out they have a new gender identity, and they need a last final digit in their ID numbers too. Even if nothing c…

It sounds to me like you’re just arguing for premature optimization of another kind (specifically, prematurely changing your entire architecture for edge cases that probably won’t ever happen to you).

Re: Avoid UUID Version 4 Primary Keys in Postgres

#80
post #36
post #19

Earlier quoted context omitted.

Out of curiosity, why is it an issue if you leak creation time?

Well you're leaking user data. I'm sure you can imagine situations where "the defendant created an account on this site on this date" could come up. And the user could have created that account not knowing that the creation date is public, because it's not listed anywhere in the publicly viewable part of the profile other than the UUID in the URL.

Pretty much every social media app has a "Member since X" visible on public profiles. I don't think it's an issue.
Post reply on HN