UUID
spec
UUID | spec | |
---|---|---|
9 | 62 | |
12,338 | 8,671 | |
- | 1.4% | |
7.8 | 0.0 | |
4 days ago | 4 months ago | |
PHP | ||
MIT License | GNU General Public License v3.0 only |
Stars - the number of stars that a project has on GitHub. Growth - month over month growth in stars.
Activity is a relative number indicating how actively a project is being developed. Recent commits have higher weight than older ones.
For example, an activity of 9.0 indicates that a project is amongst the top 10% of the most actively developed projects that we are tracking.
UUID
-
Weekly help thread
I'd recommend using either Ramsey/uuid or generating a random number using random_bytes(32) then compressing it. 32 bits of randomness should be sufficient for most programs.
-
eli5 With billions and billions of people over time, how can fingerprints be unique to each person. With the small amount of space, wouldn’t they eventually have to repeat the pattern?
Of course, theoretical math and applied math often work out differently. Here's a thread with a guy claiming his team's software is running into "Several hundred [UUID] collisions per day"
-
weird php results microtime/hrtime
Or just use a library.
-
UUIDs are a wonderful invention
I'll leave this here https://github.com/ramsey/uuid/issues/80
-
What are types of bugs that only show up after thousands or millions of times of the code being run.
Not really in the realm of “thousands” of runs, but UUID collisions are possible and have been observed multiple times after only 1 million generations due to unknown reasons.
- Generating unique key code using PHP
-
Sortable Collision-Free UUIDs
There's also the risk of bad randomness sources and/or bugs.
One popular UUID library got a bug report stating: "We are generating about 1M UUID4 a day, and we are getting several hundred collisions a day". And so they were; turned out to be a bug/weird interaction between the OpenSSL library they were using for randomness and forking. (Details here, although it was all fixed years ago of course: https://github.com/ramsey/uuid/issues/80)
On paper, you should never, ever, ever see a collision when generating a mere million v4 UUIDs a day, much less hundreds of collisions. But that doesn't mean it can't happen!
This is also an interesting bit of analysis; comes from a company that processed a lot of UUIDs generated in browsers, checked, and discovered about 5 collisions per million UUIDs. Again, not what you'd naively expect! (Turned out to be mostly driven by misbehaving crawlers.) https://medium.com/teads-engineering/generating-uuids-at-sca...
-
Things You Should Do Now
just make sure you have everything configured correctly on your system if using UUIDs:
https://github.com/ramsey/uuid/issues/80
In the types of systems that need UUIDs there is probably no easy way to check for collisions. The prospect of mystery data corruption with no ability to trace it down frightens the hell out of me.
The only reason that issue was reported is because someone was actually doing the collision checking. That's not going to be the norm in UUID systems. Think about it.
-
A UUID can have so many combinations that UUIDs are effectively unique. But it's possible to generate the same one twice, however small the chance. Is it best practice to take this chance into account, checking to be sure you haven't used it?
In theory the risk of collision is so small it can be written off, but implementations can have bugs.
spec
-
The UX of UUIDs
Can use ULID to "fix" some issues
https://github.com/ulid/spec
- Ulid: Universally Unique Lexicographically Sortable Identifier
-
Ask HN: Is it acceptable to use a date as a primary key for a table in Postgres?
Both ULID and UUID v7 have a time code component which can be extracted.
It would be best for indexing to store the actual value in binary, though not strictly necessary as these later UUID standards (unlike conventional UUIDs) use time code prefixes (so indexing clusters.)
https://uuid7.com/
https://github.com/ulid/spec
-
Bye Sequence, Hello UUIDv7
UUIDv7 is a nice idea, and should probably be what people use by default instead of UUIDv4.
For the curious:
* UUIDv4 are 128 bits long, 122 bits of which are random, with 6 bits used for the version. Traditionally displayed as 32 hex characters with 4 dashes, so 36 alphanumeric characters, and compatible with anything that expects a UUID.
* UUIDv7 are 128 bits long, 48 bits encode a unix timestamp with millisecond precision, 6 bits are for the version, and 74 bits are random. You're expected to display them the same as other UUIDs, and should be compatible with basically anything that expects a UUID. (Would be a very odd system that parses a UUID and throws an error because it doesn't recognise v7, but I guess it could happen, in theory?)
* ULIDs (https://github.com/ulid/spec) are 128 bits long, 48 bits encode a unix timestamp with millisecond precision, 80 bits are random. You're expected to display them in Crockford's base32, so 26 alphanumeric characters. Compatible with almost everything that expects a UUID (since they're the right length). Spec has some dumb quirks if followed literally but thankfully they mostly don't hurt things.
* KSUIDs (https://github.com/segmentio/ksuid) are 160 bits long, 32 bits encode a timestamp with second precision and a custom epoch of May 13th, 2014, and 128 bits are random. You're expected to display them in base62, so 27 alphanumeric characters. Since they're a different length, they're not compatible with UUIDs.
I quite like KSUIDs; I think base62 is a smart choice. And while the timestamp portion is a trickier question, KSUIDs use 32 bits which, with second precision (more than good enough), means they won't overflow for well over a century. Whereas UUIDv7s use 48 bits, so even with millisecond precision (not needed) they won't overflow for something like 8000 years. We can argue whether 100 years us future proof enough (I'd argue it probably is), but 8000 years is just silly. Nobody will ever generate a compliant UUIDv7 with any of the first several bits aren't 0. The only downside to KSUIDs is the length isn't UUID compatible (and arguably, that they don't devote 6 bits to a compliant UUID version).
Still feels like there's room for improvement, but for now I think I'd always pick UUIDv7 over UUIDv4 unless there's an very specific reason not to.
-
50 years later, is Two-Phase Locking the best we can do?
I'd love for Postgres to adopt ULID as a first class variant of the same basic 128bit wide binary optimized column type they use for UUIDs, but I don't expect they will, while its "popular" its not likely popular enough to have support for them to maintain it in the long run... Also the smart money ahead of time would have been for the ULID spec to sacrifice a few data bits to leave the version specifying sections of the bit field layout unused in the ULID binary spec (https://github.com/ulid/spec#binary-layout-and-byte-order) for the sake of future compatibility with "proper" UUIDs... Performing one big bulk bitfield modification to a PostgreSQL column would have been much less painful than re-computing appropriate UUIDv7 (or UUIDv8s for some reason) and then having to perform a primary key update on every row in the table.
- FLaNK Stack Weekly for 12 September 2023
- You Don't Need UUID
- UUID Collision
-
Type-safe, K-sortable, globally unique identifier inspired by Stripe IDs
Many people had the same idea. For example ULID https://github.com/ulid/spec is more compact and stores the time so it is lexically ordered.
- ULID: Universally Unique Lexicographically Sortable Identifier
What are some alternatives?
cuid - Collision-resistant ids optimized for horizontal scaling and performance.
dynamodb-onetable - DynamoDB access and management for one table designs with NodeJS
php-ulid - A PHP port of alizain/ulid with some minor improvements.
uuid6-ietf-draft - Next Generation UUID Formats
ulid - Universally Unique Lexicographically Sortable Identifier (ULID) in Python 3
kuuid - K-sortable UUID - roughly time-sortable unique id generator
ksuid - Java implementation of K-Sortable Globally Unique IDs
python-ksuid - A pure-Python KSUID implementation
Slugify - Converts a string to a slug. Includes integrations for Symfony, Silex, Laravel, Zend Framework 2, Twig, Nette and Latte.
ulid-lite - Generate unique, yet sortable identifiers
Device Detector - The Universal Device Detection library will parse any User Agent and detect the browser, operating system, device used (desktop, tablet, mobile, tv, cars, console, etc.), brand and model.
shortuuid.rb - Convert UUIDs & numbers into space efficient and URL-safe Base62 strings, or any other alphabet.