News for hackers. Stuff that matters.
Re:New UUID Formats – IETF Draft by jozvolskyef on Friday August 06, 2021 @12:33PM (#28089498)
What are the advantages of sortable UUIDs with embedded timestamps over random 128 bits and a created_at column?
Re:New UUID Formats – IETF Draft by mrighele on Friday August 06, 2021 @01:05PM (#28089991)
The linked RFC talks about this (it refers to UUIDv4, which is not much different from 128 random bits):

" First, most of the existing UUID versions such as UUIDv4 have poor database index locality. Meaning new values created in succession are not close to each other in the index and thus require inserts to be performed at random locations. The negative performance effects of which on common structures used for this (B-tree and its variants) can be dramatic. As such newly inserted values SHOULD be time-ordered to address this."

Re:New UUID Formats – IETF Draft by LukeShu on Friday August 06, 2021 @01:19PM (#28090215)
> UUIDv4, which is not much different from 128 random bits

To quantify how different: UUIDv4 has 6 fixed bits and 122 random bits.

Re:New UUID Formats – IETF Draft by bob1029 on Friday August 06, 2021 @02:13PM (#28090953)
Note that this totally-random key behavior is actually highly desirable when using dynamic programming techniques such as with the Splay Tree. It makes it statistically impossible for the tree to fall into a degenerate state.
Re:New UUID Formats – IETF Draft by pjscott on Friday August 06, 2021 @02:46PM (#28091318)
Splay trees aren't a dynamic programming thing (and aren't used in any database storage backends I know of). They are, however, conjectured to be no more than a constant factor slower than a dynamically optimal binary search tree.

https://en.wikipedia.org/wiki/Optimal_binary_search_tree

Re:New UUID Formats – IETF Draft by bob1029 on Friday August 06, 2021 @03:40PM (#28091864)
> Splay trees aren't a dynamic programming thing

My mistake - I meant to write "dynamic optimality".

As for the "not used in any database storage backends", I have personally started experimenting with them due to higher-order effects they can provide relative to their optimization technique.

The fact that the most recently accessed node is always at the root also means that incremental tree updates are well-bounded in terms of # of nodes that would need to be rewritten to disk. This allows for much more efficient usage of storage-friendly things like append-only log structures.

Re:New UUID Formats – IETF Draft by pjscott on Friday August 06, 2021 @03:50PM (#28091971)
Today I learned! That sounds fascinating; is there anything written down about it somewhere?
Re:New UUID Formats – IETF Draft by bob1029 on Friday August 06, 2021 @05:28PM (#28092954)
I actually don't think there is any specific reference I have ever encountered. It was something that just popped into my head one day when I was trying to figure out a way to write small batches of key-value data into a log.

Once you put 2+2 together on it, its like a lot of concepts seem to snap into place all at once. You also get incredible locality of access - each batch can sometimes fit within a single I/O block depending on system loading and data patterns, so traversing the tree to any node modified in the previous flush to disk would involve a single I/O.

Edit: If you search "splay tree append only log" on google, you will find one of my previous HN comments on the first page of results: https://news.ycombinator.com/item?id=28010167 This tells me there isnt much precedent for this idea.

Re:New UUID Formats – IETF Draft by jandrewrogers on Friday August 06, 2021 @01:09PM (#28090055)
There are two issues not mentioned in the other comments with a random 128-bit UUID in real systems, both related to data scale:

- They defeat data compression, which is an integral part of many database engines these days for both performance and scalability reasons.

- For some very large systems, there is a UUID collision tail risk that while small is large enough to be plausible.

Re:New UUID Formats – IETF Draft by neur4lnet on Friday August 06, 2021 @01:58PM (#28090738)
Its compression a legitimate concern for primary keys?
Re:New UUID Formats – IETF Draft by jandrewrogers on Friday August 06, 2021 @02:33PM (#28091174)
Definitely, I've run into this problem many times. UUIDs are pretty bulky data types in database terms, usually larger than most other types in a typical table on average and possibly larger than rest of the row after including the index overhead, which creates cache pressure. Logical columns in a table are commonly stored physically as a vector, of UUIDs in this case, expressly for the purpose of compressing the representation. This saves both disk cache and CPU cache. For queries, the benefit of scanning compressed vectors is that it reduces the average number of page faults per query, which is one of the major bottlenecks in databases.

Also, some data models (think graphs) tend to be not much more than giant collections of primary keys. Using the smallest primary key data type that will satisfy the requirements of the data model is a standard performance optimization in databases. It is not uncommon for the UUID that the user sees to be derived from a stored primary key that is a 32-bit or 64-bit integer.

Re:New UUID Formats – IETF Draft by NyxWulf on Friday August 06, 2021 @02:47PM (#28091332)
Uuids are bulky if you store them in text form, but in binary form they are only 128 bits.

The main feature of a uuid is it allows distributed generation. 32-bit or 64-bit integers are almost always sequential numbers. The sequential nature allows efficient page filling and index creation, but the contention involved in creating a sequence grows rapidly with scale.

So while a 128-bit uuid is larger than a 64 bit integer, this version allows for the bulk of the benefits of sequential integers while reducing the biggest drawback of contention at the point of creation.

Re:New UUID Formats – IETF Draft by jandrewrogers on Friday August 06, 2021 @04:36PM (#28092450)
I was assuming binary format. 128-bits is a pretty heavy data type in many data models with measurable impact on performance versus using something smaller.

You also do not need 128-bits to decentralize unique key generation, even though it is quite reasonable if you design your keys well. Many do it with 64-bit unique keys in massive scale systems.

A subtle point that you may be overlooking is that while large-scale distributed databases, including all the ones I work on, export globally unique 128-bit keys, in most systems I’ve worked with they are internally represented and stored as 64-bits or less even if the key space is much larger than 64 bits. There are many different techniques for doing key space elision and inference that are used inside distributed databases to save space. The 128-bit value is only materialized when sending it over the wire to some external client system. But you don’t need to store it.

Literally storing a primary key in a distributed system as a 128-bit value is all downside with few benefits. For small systems the performance and scaling cost may not matter that much but in very large systems it matters a lot. It can — literally! — cost you millions of dollars per year.

Re:New UUID Formats – IETF Draft by manigandham on Friday August 06, 2021 @05:23PM (#28092905)
You can generate any size number in a distributed fashion. The only difference is that 128-bits gives you enough scale that it's practically impossible to have collisions when randomly generating.

Unless you need to be completely disconnected, a little coordination can drastically improve things. In past companies, I used a simple counter with 64-bit integers and each distributed process would increment a billion-number range to use for IDs. Fast, efficient, compatible with everything, naturally ordered, and guaranteed to never have a collision.

Re:New UUID Formats – IETF Draft by cogman10 on Friday August 06, 2021 @03:05PM (#28091528)
And, to be clear, those benefits are the placement of new records in a DB.

If a UUID is completely random it means it can be inserted anywhere which can require the DB to reshuffle records and pages in order to make room for the new record.

Having a sequential element to the UUID makes it a lot easier to have an index where each record is inserted at the end. Which, like you said, makes page usage more efficient and decreases the amount of work a DB has to do on insertion.

All this is a compounded problem if you have a DB with frequent writes, a lot of indexes, or both.

Re:New UUID Formats – IETF Draft by krinchan on Friday August 06, 2021 @05:13PM (#28092800)
Twitter snowflakes are unsigned 64-bit ids designed to be created in a distributed fashion.
Re:New UUID Formats – IETF Draft by tatersolid on Wednesday August 11, 2021 @10:12PM (#28150853)
But they require a separate global distributed HA clustered service just for ID generation. This makes no sense from a cost or complexity perspective unless you’re Twitter-scale.
Re:New UUID Formats – IETF Draft by bob1029 on Friday August 06, 2021 @02:30PM (#28091143)
If you are getting good compression on your primary keys, this may be cause for alarm.

These should be high-entropy items.

Re:New UUID Formats – IETF Draft by jandrewrogers on Friday August 06, 2021 @02:46PM (#28091321)
The sole constraint on a primary key is that it is unique. Even in distributed systems, deterministically unique keys are almost always much more compressible than probabilistically unique keys, which is a double win.

Should you desire a high-entropy key, e.g. for hash tables or security, this can be trivially derived from the low-entropy key while still guaranteeing deterministic uniqueness.

Compressible primary keys are superior in every way.

Re:New UUID Formats – IETF Draft by lytedev on Friday August 06, 2021 @12:39PM (#28089587)
My understanding is that if the UUID is not sortable, inserting it into an index can be a performance hit. So if you have a write-heavy table with non-sorted primary keys, you will have a lower ceiling (higher overhead).
Re:New UUID Formats – IETF Draft by NyxWulf on Friday August 06, 2021 @02:51PM (#28091377)
The lack of sorting is what this rfc is fixing.
Re:New UUID Formats – IETF Draft by srcreigh on Friday August 06, 2021 @12:46PM (#28089704)
Know about primary vs secondary indexes?

Generally 1 index contains the data, all the other indexes have pointers to that index in their leaf nodes. Usually the PK index contains the data

So loading 1000 consecutive rows in a created_at secondary index takes two steps: 1) created_at index B-Tree traversal (maybe 10 pages from disk, max?) 2) Potentially loading 1000 randomly sorted pages from disk, dereferencing pointers (EXPENSIVE!)

Whereas, if your primary key is sorted by time, loading 1000 rows is 1) slightly larger primary B-Tree traversal 2) no 2, you're done :)

Re:New UUID Formats – IETF Draft by srcreigh on Monday August 09, 2021 @08:52PM (#28124100)
I don't expect to get an explanation for why this received downvotes - but maybe someone can offer one?

My best theory is that it's the Reddit "I don't need to understand how stuff works to be a programmer" crowd.

Re:New UUID Formats – IETF Draft by ignoramous on Friday August 06, 2021 @12:39PM (#28089590)
> What are the advantages of sortable UUIDs

I guess for use-cases like Instagram's: "Generated IDs should be sortable by time (so a list of photo IDs, for example, could be sorted without fetching more information about the photos)... If you use a timestamp as the first component of the ID, the IDs remain time-sortable... [but require] more storage space (96 bits or higher) to make reasonable uniqueness guarantees" [0]

[0] https://archive.is/xiMGZ

Re:New UUID Formats – IETF Draft by zimpenfish on Friday August 06, 2021 @04:17PM (#28092267)
cf also Twitter's Snowflake[1] which is [timestamp, worker number, sequence number] to give the same "roughly sortable by time" property.

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

Re:New UUID Formats – IETF Draft by causasui on Friday August 06, 2021 @12:52PM (#28089798)
I use KSUIDs a ton in DynamoDB; it saves me from having to create and fill another index to sort items by creation time.

The only case where I don't use them is if I don't want to create time of the item to be known to the user.

Re:New UUID Formats – IETF Draft by junon on Friday August 06, 2021 @02:57PM (#28091431)
FWIW the linked RFC's "Background" section is really easy to understand even for people who aren't well versed in dist-sys concepts. I suggest giving it a read - it's quite well written.
Re:New UUID Formats – IETF Draft by masklinn on Friday August 06, 2021 @01:03PM (#28089959)
Random primary keys play hell on database indexes.
Re:New UUID Formats – IETF Draft by BenoitEssiambre on Friday August 06, 2021 @12:46PM (#28089701)
On top of reasons given by others, if the data likely to be queried together tends to get stored together because of this, things will be much more cache efficient and queries potentially much faster since for db loads, cache efficiency (not just ram but CPU cache), alleviates some of the most important performance bottlenecks.
Re:New UUID Formats – IETF Draft by notamy on Friday August 06, 2021 @12:37PM (#28089560)
It works better for databases that can only sort on the primary key (iirc Cassandra is one of these)
Re:New UUID Formats – IETF Draft by parhamn on Friday August 06, 2021 @01:41PM (#28090510)
it depends what "created_at" means, if it means "persisted_at" then they're also a bit different because these sorts of UUIDs are generated before being persisted (often long before for offline-and sync systems).
Re:New UUID Formats – IETF Draft by orf on Friday August 06, 2021 @01:09PM (#28090066)
Imagine you’re not using a storage platform that supports that access pattern. Think S3, DynamoDB or even a directory of files. Being able to encode your sort key in your primary key is a pretty nice property.
Re:New UUID Formats – IETF Draft by datavirtue on Friday August 06, 2021 @02:00PM (#28090770)
Hmmm...Now I'm wondering why I never bothered to prepend a timestamp to a GUID.
Re:New UUID Formats – IETF Draft by orf on Friday August 06, 2021 @02:03PM (#28090806)
Because then it wouldn’t be a UUID in a format that other systems might expect or work more efficiently with.
Re:New UUID Formats – IETF Draft by weird-eye-issue on Friday August 06, 2021 @12:37PM (#28089557)
Not having a created_at column and the index bloat that goes with it?
Re:New UUID Formats – IETF Draft by ape4 on Friday August 06, 2021 @01:02PM (#28089949)
Isn't that a possible security issue. MACs used to be predictable but then became random.
Re:New UUID Formats – IETF Draft by jozvolskyef on Friday August 06, 2021 @01:51PM (#28090636)
I presume you are referring to UUIDv4/1, which has 122 random bits. It is a common misconception that this is insecure. In reality, the UUIDv4/1 is just a way to encode 122 random bits, which is more than enough for most needs. The property of being practically unguessable comes from the random generator, not from the encoding.

Secondly, whether or not being predictable is a problem depends on the use case. The keys used on this forum are predictable and it is not an issue.

Re:New UUID Formats – IETF Draft by lazide on Friday August 06, 2021 @02:09PM (#28090891)
I’m pretty sure you mean ‘stable’ not predictable here re:MACs?

The issue with using Mac addresses is it essentially leaked tracking information on the machine being used to generate the uuid - you could associate every ID it ever generated back to that same machine, correlate it to things like open WiFi AP logs, whatever. Which is pretty scary sometimes.

Many OS’s now randomly generate a new MAC on every disconnect/connect for this reason, to at least give some privacy.

Re:New UUID Formats – IETF Draft by runeks on Sunday August 08, 2021 @01:09PM (#28108245)
Follow-up question:

What are the advantages of sortable UUIDs with embedded timestamps over having an auto-incrementing id column, a created_at column, and using the composite primary key (created_at, id)?

Re:New UUID Formats – IETF Draft by rswail on Monday August 09, 2021 @10:32AM (#28117344)
Because an auto-incrementing id column has to be coordinated between processes that are writing to the database. That means allocating ids in blocks that are large enough that processes aren't blocked in getting an allocation, but they still need to request a block to maintain writing.

That sequence allocation needs to be ACID as well, so that there is no chance of any allocation being repeated.

Re:New UUID Formats – IETF Draft by scrollaway on Friday August 06, 2021 @12:38PM (#28089568)
One data point instead of two. Which may or may not have advantages depending on your situation.

At any rate it's definitely simpler, and you don't have to forego the created_at column either.

Re:New UUID Formats – IETF Draft by bilinguliar on Friday August 06, 2021 @12:59PM (#28089898)
This allows you to keep recent records in DB cache, for example. Contrary, random keys would constantly invalidate the cache unless all your data fits in memory.
Re:New UUID Formats – IETF Draft by cratermoon on Friday August 06, 2021 @01:05PM (#28089987)
Re:New UUID Formats – IETF Draft by gumby on Friday August 06, 2021 @01:07PM (#28090029)
That is addressed in the introduction -- random access inserts (i.e. specularity) can be quite inefficient depending on your backing store.
Re:New UUID Formats – IETF Draft by dheera on Friday August 06, 2021 @01:41PM (#28090513)
Searching records and returning the results time-sorted in O(n) time instead of O(n log n) time.
Re:New UUID Formats – IETF Draft by halfmatthalfcat on Friday August 06, 2021 @01:31PM (#28090364)
Using the uuid as a PK do to cursor based pagination for one.