UUID
nanoid
UUID | nanoid | |
---|---|---|
9 | 83 | |
12,338 | 23,227 | |
- | - | |
7.8 | 8.4 | |
5 days ago | 3 days ago | |
PHP | JavaScript | |
MIT License | MIT License |
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.
nanoid
-
Next.js and Bunny CDN: Complete Guide to Image Uploading with Server Actions
Last thing left is to use our new upload function in our server action. Since I like to upload images in single format and have some more control over them, I will additionally use sharp library. For file name, I'll generate some random string using nanoid:
- Nano ID Collision Calculator
-
Why we chose Bun
Our API is in node. And God, how I suffered to import nanoid in an esmodule project. I had to vendor it, since using a previous version was not ideal. With bun, we can no longer worry about that. Just import what you need and done.
-
UUIDv7 is coming in PostgreSQL 17
No thread about UUID is complete without a plug for NanoID! https://github.com/ai/nanoid/blob/main/README.md
-
Building a File Storage With Next.js, PostgreSQL, and Minio S3
Generate a unique file name using the nanoid library.
-
Building a Multi-Tenant App with FastAPI, SQLModel, and PropelAuth
The syntax should read similar to SQL itself. We’re using a Python port of nanoid to generate our IDs. There’s only one thing missing… how do we actually create the table?
-
You Don't Need UUID
I usually go for Nano Id for new projects https://github.com/ai/nanoid
-
Enhance Your Web Apps: Best JS Libraries 🔧
Nano ID
-
Analyzing New Unique Identifier Formats (UUIDv6, UUIDv7, and UUIDv8) (2022)
In another comment I mentioned I use nanoid in my projects now. It has a default space of 64^21 and has an a page where you can play with key lengths and alphabet sizes and see the probability of collisions :
https://zelark.github.io/nano-id-cc/
At the default 64 character alphabet with a 21 character key length it would take ~41 million years in order to have a 1% probability of at least one collision if you generated 1000 ids per second.
-
How I use Nano ID in Rails
Using randomly generated IDs like Nano ID could be a good alternative, however, as a developer, we must understand what Nano ID really does in our application. Defining the number of characters in the generated IDs is also important, to help with that Nano ID has a Collision Calculator to give us how many years in order to have a 1% probability of collision.
What are some alternatives?
cuid - Collision-resistant ids optimized for horizontal scaling and performance.
snowflake - Snowflake is a network service for generating unique ID numbers at high scale with some simple guarantees.
php-ulid - A PHP port of alizain/ulid with some minor improvements.
ksuid - K-Sortable Globally Unique IDs
ulid - Universally Unique Lexicographically Sortable Identifier (ULID) in Python 3
typedorm - Strongly typed ORM for DynamoDB - Built with the single-table-design pattern in mind.
ksuid - Java implementation of K-Sortable Globally Unique IDs
pg_random_id - Provides pseudo-random IDs in Postgresql databases
Slugify - Converts a string to a slug. Includes integrations for Symfony, Silex, Laravel, Zend Framework 2, Twig, Nette and Latte.
jest - Delightful JavaScript Testing.
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.
Numeral-js - A javascript library for formatting and manipulating numbers.