martinthenth
Hello everybody ![]()
Recently, some of my colleagues talked about database ids and uuids and their problems, and I remembered the pain of working with randomly distributed primary keys. They’re nice at first but then you have to index and order by a different field like created_at, while serial and bigserial already have an order and are indexed as the primary key.
UUIDv6, UUIDv7 and UUIDv8 are new standards to deal with issues found in UUIDv4 and earlier. I especially liked this post that analyzes the new standards: https://blog.devgenius.io/analyzing-new-unique-identifier-formats-uuidv6-uuidv7-and-uuidv8-d6cc5cd7391a
My favourite is UUIDv7 because they’re like UUIDv4 but the first characters are based on a millisecond timestamp; it seems like a very small change.
Elixir doesn’t have a common implementation of UUIDv7 yet, and Ecto is based on UUIDv4. So I decided to build one based on a Rust package crates.io: Rust Package Registry, which is relatively mature.
The new library is called UUIDv7 and is available on Hex UUIDv7 - Hex.
Because it’s based on Rust, the UUID generation is a whopping 72% faster than the default Ecto.UUID version 4 generator. NIFs are precompiled and generated for most platforms.
It’s easy to set up, you only have to change one line:
def App.Schemas.User do
use Ecto.Schema
@primary_key {:id, UUIDv7, autogenerate: true}
end
You can verify the UUIDs are ordered by running a small test:
uuid1 = UUIDv7.generate()
uuid2 = UUIDv7.generate()
uuid3 = UUIDv7.generate()
uuid4 = UUIDv7.generate()
assert uuid1 < uuid2
assert uuid2 < uuid3
assert uuid3 < uuid4
Though you may have to add Process.sleep(1) between the generations to skip one millisecond (UUIDv7 is based on milliseconds and ends with random bits)
Since the performance difference between Rust-based UUID generation and Ecto.UUID is so large, maybe it could be a motivation to write other more commonly used functions as NIFs from more compute-efficient languages?
You can check the benchmark here: UUIDv7 - Benchmark
GitHub: UUIDv7 - GitHub
Hex: UUIDv7 - Hex
Trending in Announcing
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
typesend
Thank you for making this!
sbuttgereit
New proposed standards. While it looks like UUID v6+ have made a lot of progress, they haven’t been adopted as standards yet: RFC 9562 - Universally Unique IDentifiers (UUIDs)
This isn’t to say that what ends up being adopted will be much different than has appeared in the various drafts, but for some the formality can matter.
As an aside, it looks like the scope of the proposed standard has expanded as they’ve progressed through the process to actually revising the UUID v1 through v5 standards as well: Revise Universally Unique Identifier Definitions (uuidrev)… the revisions to previous standards are very limited to doing things like correcting errata (unsurprisingly).
AndyL
Also there is
uniqfor UUIDv7 genertion - pure Elixir afaik.https://github.com/bitwalker/uniq
mayel
I wonder what’s the difference between that and ULID?
hauleth
First of all, ULID is not valid UUID. It has compatible length, but not compatible format.
martinthenth
Yup,
UUIDv7andUniqare compatible because of the shared standard, butuniqis a bit slower because it’s 100% Elixir. It’s comparable withEcto.UUID.Benchmark:
slouchpie
Are there any plans for this to be made part of the Ecto standard library?
v0idpwn
I can’t say for everyone, but I don’t think so.
You can always send a proposal, but since it can be used easily as an external library, I don’t think there’s a valid reason.
acrolink
I think using
UUIDv7can make it easier to do horizontal scaling (using multiple DB servers, i.e. sharding). Correct? Is there any tutorial on the topic? Thanks.c4710n
A quote from https://uuid7.com/