Our great sponsors
spec | pg-ulid | |
---|---|---|
62 | 2 | |
8,592 | 58 | |
2.0% | - | |
0.0 | 0.0 | |
3 months ago | over 4 years ago | |
C | ||
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.
spec
-
The UX of UUIDs
Can use ULID to "fix" some issues
-
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.
- FLaNK Stack Weekly for 12 September 2023
- You Don't Need UUID
-
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.
-
Analyzing New Unique Identifier Formats (UUIDv6, UUIDv7, and UUIDv8) (2022)
ULIDs, https://github.com/ulid/spec, are also already in widespread use and are essentially the same idea as v7 UUIDs.
-
Generate unique Id
You may also want to look at ULIDs
- Launch HN: Activepieces (YC S22) – Open-Source Zapier Alternative
-
Choosing a Postgres Primary Key
I use ULID, 128 bits, time and great sorting
-
Trade-offs from using ULIDs at incident.io
The ULID spec with many implementations: https://github.com/ulid/spec
pg-ulid
-
Lesser Known PostgreSQL Features
Here's one[1], not actively maintained though.
-
PostgreSQL UUID vs. Serial vs. Identity
Yeah, just use a UUID unless the bits to store the UUID really are your driving limitation (they're not), having a UUID that is non-linear is almost always the most straight-forward option for identifying things, for the tradeoff of human readability (though you can get some of that back with prefixes and some other schemes). I'm not going to rehash the benefits that people have brought up for UUIDs, but they're in this thread. At this point what I'm concerned about is just... what is the best kind of UUID to use -- I've recently started using mostly v1 because time relationship is important to me (despite the unfortunate order issues) and v6[0] isn't quite so spread yet. Here's a list of other approaches out there worth looking at
- isntauuid[1] (mentioned in this thread, I've given it a name here)
- timeflake[2]
- HiLo[3][4]
- ulid[5]
- ksuid[6] (made popular by segment.io)
- v1-v6 UUIDs (the ones we all know and some love)
- sequential interval based UUIDs in Postgres[7]
Just add a UUID -- this almost surely isn't going to be what bricks your architecture unless you have some crazy high write use case like time series or IoT or something maybe.
[0]: http://gh.peabody.io/uuidv6/
[1]: https://instagram-engineering.com/sharding-ids-at-instagram-...
[2]: https://github.com/anthonynsimon/timeflake
[3]: https://en.wikipedia.org/wiki/Hi/Lo_algorithm
[4]: https://www.npgsql.org/efcore/modeling/generated-properties....
[5]: https://github.com/edoceo/pg-ulid
[6]: https://github.com/segmentio/ksuid
[7]: https://www.2ndquadrant.com/en/blog/sequential-uuid-generato...
What are some alternatives?
ksuid - K-Sortable Globally Unique IDs
cuid - Collision-resistant ids optimized for horizontal scaling and performance.
dynamodb-onetable - DynamoDB access and management for one table designs with NodeJS
Hasura - Blazing fast, instant realtime GraphQL APIs on your DB with fine grained access control, also trigger webhooks on database events.
Dapper - Dapper - a simple object mapper for .Net
uuid6-ietf-draft - Next Generation UUID Formats
tbls - tbls is a CI-Friendly tool for document a database, written in Go.
timeflake - Timeflake is a 128-bit, roughly-ordered, URL-safe UUID.
kuuid - K-sortable UUID - roughly time-sortable unique id generator
ulid-lite - Generate unique, yet sortable identifiers
python-ksuid - A pure-Python KSUID implementation
shortuuid.rb - Convert UUIDs & numbers into space efficient and URL-safe Base62 strings, or any other alphabet.