Live data from Hacker News

UUIDs are popular, but bad for performance (2019)

percona.com

61–70 of 246 posts

Re: UUIDs are popular, but bad for performance (2019)

#61

Slightly related question- does anyone know what data type Reddit uses for their post/comment id? It seems like a short alphanumeric yet unique identifier and much shorter than a uuid. Also what does twitter use for their post/comment id? Seems like some sort of big int?

Twitter created Snowflake many years ago to generate IDs. Unsure whether it’s still in use.

https://blog.twitter.com/engineering/en_us/a/2010/announcing...

Re: UUIDs are popular, but bad for performance (2019)

#62
post #23

A lot of things that we do for security or privacy are bad for performance, but I think they are still good tradeoffs.

Agreed. Using UUID-s for keys is useful to exclude entire classes of security issues. Most notable are many kinds of enumeration attacks.

Also entire classes of bugs. You screwed up a JOIN, or an application-level lookup for that matter? With UUIDs you'll get no results, with sequential ints you'll get a valid but wrong result.

Or worse, the right result for the wrong reason. I've actually seen a case where creating a new entity in the application populated X records in X child tables, each with a sequential ID, and as a result all of them had the same surrogate PK. They were 1:N relationships in principle, but the software wasn't feature complete yet so the actual records were all 1:1.

Years later one of those tables finally received some extra records, and it caused a really weird bug because a query had accidentally used the PK instead of the FK as a join key, but for years it had happily chugged along because the two columns were in sync.

Re: UUIDs are popular, but bad for performance (2019)

#63
post #51
post #28

I am very much not a database person, so forgive me if this is a dumb question. I'm reading this article and it says that UUID are compared byte by byte, and seems to be indicating they're stored as string. Is that actually the case? I would have assumed that SQL supported 128 bit ints, but this seems to imply it does not. Another question: if a column is set to char(fixed size) do the various sequel engines really n…

It often doesn't matter if you use a 128bit int or a string since either way you're loading 4K/8K/16K from ssd/hdd. Database people do all sorts of silly things like storing datetimes as normalized ISO strings (2022-01-08T08:18:20) instead of using 64bit unix time. They get away with it because both the backend (durable storage) and the clients (webapps responding to a user 10ms away) are incredibly slow compared to…

> Database people do all sorts of silly things like storing datetimes as normalized ISO strings (2022-01-08T08:18:20) instead of using 64bit unix time

That's super weird. I can't think of a major RDBS that doesn't have a native date/time data type, unless you count SQLite.

Re: UUIDs are popular, but bad for performance (2019)

#64

Earlier quoted context omitted.

That's why the pattern is eventual consistency. You receive a message "{entity}Created" and it contains the Id of the full object.

It's like, _maybe_ eventual consistency. Hopefully the client doesn't try to do anything important with the ID/new object

If the client needs to use the object right there and then, it can start polling the GET for that ID until it returns 200.

A slightly more sophisticated pattern is to have the initial creation command return an ID of the creation event. Then the client can poll that ID and just check that the event has been processed successfully. This requires the client to trust the server a little, but it means it can just poll a small, temporary, probably in-memory "recent events" table instead of hammering the main API.

A more efficient design is to YOLO, submit the second command while the first is still being processed, and trust the server to handle the commands in the right order. This is fine for backend services, unfortunately human users get annoyed at their frontend when it says "sorry you have to do the purchase finalization again because an earlier 'add to cart' command failed and I only noticed it now".

Re: UUIDs are popular, but bad for performance (2019)

#65
post #26

Earlier quoted context omitted.

If you order the data based on the uuid and your uuid is randomly distributed, then you will almost always be writing the data in the middle of your table, physically. You can cut the impact somewhat by using spare tables (leaving lots of empty space) but eventually you'll be re-writing the data. SQL Server has a sequential uuid type which avoids exactly this problem.

Why use a sequential UUID over integers as an id and then associating them with a random UUID? Integers are a superior way to record sequential data.

Because you don't want your record IDs to be perfectly sequential data.

You only care about them being sequential enough that your database engine will write them down quickly and efficiently (for engines like InnoDB or MSSQL which have such behaviour). But as a developer you typically want them to be random so that you can generate them in a decentralised manner, prevent guessing or accidental joins, safely merge records from multiple tables, etc.

Sequential UUIDs usually preserve enough randomness for the latter (as long as you don't do stuff like generating a trillion IDs exactly at midnight), while providing enough sequentiality for the former.

Re: UUIDs are popular, but bad for performance (2019)

#66
post #7

Bad for performance as primary keys . But, still provide strong value as a unique identifier which is what makes them popular. I’ve used integers as primary keys, with UUIDs as alternate keys for external-to-the-data-store queries.

I do something similar. It works well.

Re: UUIDs are popular, but bad for performance (2019)

#67
post #7

Bad for performance as primary keys . But, still provide strong value as a unique identifier which is what makes them popular. I’ve used integers as primary keys, with UUIDs as alternate keys for external-to-the-data-store queries.

"Bad for performance as primary keys" -> "Bad for performance as primary keys in MySQL". This isn't an issue in PostgreSQL and perhaps the lesson here is that as you scale, you need to understand more about the internals of the DB system you've chosen. This isn't limited to RDBMS as it's pretty easy to show trade-offs in choosing a NoSQL as well.

Re: UUIDs are popular, but bad for performance (2019)

#68
post #7

Bad for performance as primary keys . But, still provide strong value as a unique identifier which is what makes them popular. I’ve used integers as primary keys, with UUIDs as alternate keys for external-to-the-data-store queries.

Yeah. The main problem is people using them as primary keys in naïve systems like relational databases. You can't just expect a relational database to magically become a distributed system just by using UUIDs. There is a bit more work to do than that.

Is it too much to ask people to explain what's wrong with this well-written and on-topic comment instead of downvoting it?

Re: UUIDs are popular, but bad for performance (2019)

#69
post #50

Isn't this easily solved by supporting 128 bit keys and using UUIDs as intended, i.e. as integers and not in their string serialization? This is as nonsensical as storing IPv4 as strings instead of 32 bit integers.

It should be noted that some database providers already provide a UUID data type which is a 128bit integer. https://www.postgresql.org/docs/14/datatype-uuid.html

[deleted]

Re: UUIDs are popular, but bad for performance (2019)

#70
post #65
post #26

Earlier quoted context omitted.

Why use a sequential UUID over integers as an id and then associating them with a random UUID? Integers are a superior way to record sequential data.

Because you don't want your record IDs to be perfectly sequential data. You only care about them being sequential enough that your database engine will write them down quickly and efficiently (for engines like InnoDB or MSSQL which have such behaviour). But as a developer you typically want them to be random so that you can generate them in a decentralised manner, prevent guessing or accidental joins, safely merge re…

But now the ID has a random concatenation of data in it, like the MAC address of the node that inserted it into the database and a timestamp.

It'd be much more orderly for each node to have a sequential ID, the ID of the node that created it, a timestamp when it was created and a real UUID v4 for if someone wants to prevent collisions. Same data, but the primary key on an individual node is a lot smaller (half the size) and the metadata is available for use if someone wants it. There is natural room to build up namespaces and such. It is better to keep these observations separate instead of munging them all into the record ID.

Post reply on HN