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.

Show HN: UPID – a unique ID with built-in prefix

5 pointsby carderne9 months ago
The discussion about what to use for database keys has come up a few times on HN. Integers or UUID, or maybe ULID&#x2F;UUIDv7 with a time component. One that also comes up is prefixes (as used by Stripe, most famously).<p>The biggest downside (imo) of the Stripe approach is that you either have to store them as strings, which isn&#x27;t great, or strip&#x2F;prepend in your API layer.<p>So I made a new thing called UPID. It&#x27;s most similar to ULID, as it includes a time-component (but with lower precision) and also encodes to a base32 alphabet. But it also includes a 20-bit prefix (allowing four characters). So it can be stored as a UUID or anywhere a 128-bit blob goes, and it always knows what its prefix is because it&#x27;s baked in.<p>I&#x27;ve made implementations for Python, Rust and TypeScript, as well as a Postgres extension (with a upid datatype). Mentioned it a few weeks ago in a HN discussion and people seemed pretty interested so thought I&#x27;d post it as a submission.<p>In Rust it looks like this:<p><pre><code> use upid::Upid; Upid::new(&quot;post&quot;); &#x2F;&#x2F; post_2acqyme2ygscyguveuhj5a </code></pre> There are more examples in the repositories.<p>Repo (Python, Rust, Postgres): <a href="https:&#x2F;&#x2F;github.com&#x2F;carderne&#x2F;upid">https:&#x2F;&#x2F;github.com&#x2F;carderne&#x2F;upid</a><p>TypeScript: <a href="https:&#x2F;&#x2F;github.com&#x2F;carderne&#x2F;upid-ts">https:&#x2F;&#x2F;github.com&#x2F;carderne&#x2F;upid-ts</a><p>Demo site: <a href="https:&#x2F;&#x2F;upid.rdrn.me&#x2F;" rel="nofollow">https:&#x2F;&#x2F;upid.rdrn.me&#x2F;</a>

1 comment

nivertech9 months ago
Blog post:<p>UPID is as UPID does<p><a href="https:&#x2F;&#x2F;rdrn.me&#x2F;upid&#x2F;" rel="nofollow">https:&#x2F;&#x2F;rdrn.me&#x2F;upid&#x2F;</a><p>---<p>BTW: Hi&#x2F;Lo scheme for Primary Keys &#x2F; IDs was invented long before Stripe existed. Partcularly, I used short up to 3 letters prefixes to make IDs Human-readable (and writable!) more than 20 years ago. 5 years later Amazon started using similar scheme in their AWS product, i.e i-NNNNN for instances, etc.[1].<p>In theory there are 2 kinds of ID schemes: technical&#x2F;surrogate and natural&#x2F;business&#x2F;semantic (i.e. those derived from the natural properties of the object the ID refers to).<p>Adding a prefix with the type, makes ID scheme hybrid. &quot;Hi&quot; part (prefix) is natural, and &quot;Lo&quot; part is technical. In case of UPID&#x2F;ULID&#x2F;UUIDv7 even the &quot;Lo&quot; part is hybrid, since it includes an information when the entity was created (or at least inserted into the database&#x2F;system).<p>---<p>[1] List of some AWS ID prefixes:<p><pre><code> i- vol- snap- ami- eni- sg- vpc- subnet- acl- rtb- igw- dopt- vpn- vgw- cgw- nat- rds- lb-</code></pre>
评论 #41299523 未加载