TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

ULID – Sortable Unique Identifier

33 pointsby orobinsonover 2 years ago

5 comments

preillymeover 2 years ago
This reminds me a little bit of Twitter&#x27;s snowflake: To generate the roughly-sorted 64 bit ids in an uncoordinated manner, we settled on a composition of: timestamp, worker number and sequence number.<p>Sequence numbers are per-thread and worker numbers are chosen at startup via zookeeper (though that’s overridable via a config file).<p><a href="https:&#x2F;&#x2F;blog.twitter.com&#x2F;engineering&#x2F;en_us&#x2F;a&#x2F;2010&#x2F;announcing-snowflake" rel="nofollow">https:&#x2F;&#x2F;blog.twitter.com&#x2F;engineering&#x2F;en_us&#x2F;a&#x2F;2010&#x2F;announcing...</a>
评论 #34282715 未加载
sophaclesover 2 years ago
Or just use the standards-track orderable UUID variants: <a href="https:&#x2F;&#x2F;uuid6.github.io&#x2F;uuid6-ietf-draft&#x2F;" rel="nofollow">https:&#x2F;&#x2F;uuid6.github.io&#x2F;uuid6-ietf-draft&#x2F;</a>
评论 #34282716 未加载
vbezhenarover 2 years ago
&gt; If more than one ULID is generated within the same millisecond(so, the timestamp component is the same), the algorithm should increment the previously generated random by 1 bit.<p>This is bad approach. If you know one ULID, you can with high probability deduce next one. Don&#x27;t use this approach.<p>Why with high probability? Because generating several ids within the same millisecond is extremely common case when you&#x27;re doing batching.<p>I recently gave thought to it and actually implemented several algorithms and compared them each to each other. I don&#x27;t care about standards, sorry (I think that standard UUID is oxymoron, UUID is 128 bit and that&#x27;s about it).<p>So the best approach I&#x27;ve found is:<p>6 bits for version&#x2F;variant (if you don&#x27;t care about standards you can use those bits for better randomness)<p>first 48 bits is unix milliseconds.<p>Then you have 12 more bits in the fist 64 bits. There&#x27;re two approaches to use them:<p>either use them as a nanoseconds (nanoseconds_part * 4096 &#x2F; 1_000_000).<p>Or use them as a counter if several ids are generated in the same millisecond. It allows for 4096 values per millisecond. Counter should be used when you can&#x27;t access nanosecond timer like with browser JavaScript.<p>Then you have 2-3 bits for variant and rest 62-61 bits for pure randomness. Or just 64 bits for randomness. This is enough for security.<p>If you need to generate more than one ID per 1&#x2F;4 of microsecond (approach one) or more than 4096 ids per millisecond (approach two), you can just keep generating random part until it&#x27;s greater than previously generated one. It slightly reduces randomness but not by much.<p>I&#x27;m pretty much sure that my approach is the best approach. It allows for high speed of generation (like 10 000 000 &#x2F; second with nanosecond approach with unoptimized Java) and good security.<p>If you insist on using this ULID approach, I suggest to apply the aforementioned approach: don&#x27;t just increment random bits, generate random bits until you&#x27;ve got higher value.<p>Of course one should only use this ascending UUID thing (that&#x27;s what I call it) when you need it. Random UUIDs should be used by default and ascending UUIDs only when you use those as primary keys for RDBMS.<p>And of course keep in mind that you&#x27;re leaking generation timestamp which might be a bad thing.
评论 #34283205 未加载
samwillisover 2 years ago
Discussed many time before: <a href="https:&#x2F;&#x2F;hn.algolia.com&#x2F;?q=ulid" rel="nofollow">https:&#x2F;&#x2F;hn.algolia.com&#x2F;?q=ulid</a>
erik_seabergover 2 years ago
A lot of distributed systems’ clocks can’t tell you within 1 ms when an event happened, leading to assumptions that aren’t true. I don’t think we should commit to IDs being even partially ordered unless we have a set of monotonic clocks with sequence number generators, and record a timestamp, a sequence number, and which clock they came from.
评论 #34283151 未加载
评论 #34283048 未加载