Live data from Hacker News

Why Choose ULIDs over Traditional UUIDs or IDs for Database Identification?

sotergreco.com

31–40 of 77 posts

Re: Why Choose ULIDs over Traditional UUIDs or IDs for Database Identification?

#31
post #23

Earlier quoted context omitted.

Or just use ints and encrypt them when you want to return them to the user. https://sqids.org/

NB: may be more accurate to call this hashing, versus encryption

Actually, it's not hashing either, it's just encoding. Anyone who knows the alphabet can easily decode them.

Re: Why Choose ULIDs over Traditional UUIDs or IDs for Database Identification?

#33

Did the article provide any reason for ULID over UUID? - it’s slightly more complex than UUID, but not enough to be a problem - it’s sortable (in time?) which UUID can also be (but usually recommended not to be) - it can produce slightly more ids per second, but not enough to make a difference. So, it’s a tie, a tie and a tie. Why would you switch?

That was left as an exercise for the reader :-/

> I am not going to cover all the reasons why Ulid is better, but in the end, it's up to your personal preference.

Re: Why Choose ULIDs over Traditional UUIDs or IDs for Database Identification?

#36

Earlier quoted context omitted.

> your database is spending all it's time shuffling everything around to insert your new rows in the middle of everything Is it? The order of data on disk tends to be the insertion order. What might get shuffled around is the index for the primary key, but indexes can handle that quite well.

And the answer is "depends on the database tech". Here are some nice benchmarks: https://www.toomanyafterthoughts.com/uuids-are-bad-for-datab... I learned the lexically-sortable-id trick from wizened DBAs in the 90s. It's an old trick. I've applied it myself to speed things up and have used both the ULID types described in the article and UUIDv7 etc. I even advocate using UUID7 on BigQuery where there are no indexes.…

You should definitely bench your storage to confirm that it likes hotspots (some don't!)

Re: Why Choose ULIDs over Traditional UUIDs or IDs for Database Identification?

#37

Did the article provide any reason for ULID over UUID? - it’s slightly more complex than UUID, but not enough to be a problem - it’s sortable (in time?) which UUID can also be (but usually recommended not to be) - it can produce slightly more ids per second, but not enough to make a difference. So, it’s a tie, a tie and a tie. Why would you switch?

If your team hasn't engineered in monotonically increasing distributed clocks, the results of sorting are only partly correct. I'd rather not promote an option we can't rely on.

For many use cases, partially correct sorting is fine.

Eg messaging, tweets etc: if two messages are sent at pretty much the same time, it does not matter which order they show in.

Re: Why Choose ULIDs over Traditional UUIDs or IDs for Database Identification?

#39

At this point I think that ideal setup is to use numerical id for primary and foreign keys and maintaining a separate uuid field for everything else. The reason being that index size matters a lot (for caches and other things) and index size depends on underlying field size, obviously. Whether to use UUID or ULID is depends on tooling. While it's not hard to write ascending UUID generator and I did it myself few time…

would you still keep a uniqueness constraint on the uuid/ulid column?

I wouldn’t, it’s redundant for all intents and purposes unless you’re Google, which you are not. UUIDs/ULIDs provide strong uniqueness guarantees on their own.

Re: Why Choose ULIDs over Traditional UUIDs or IDs for Database Identification?

#40
I'm using KSUID (https://sotergreco.com/why-choose-ulids-over-traditional-uui...) for a while and totally happen with it One tiny note about using sortable id is you should not expose it to public use without authentication because it can be scan easily
Post reply on HN