Aug 06, 2021
Colin Sidoti
Learn how Clerk generates resource IDs with inspiration from Stripe and Segment.
On the shoulders of giants, as they say...
Early in Stripe's lifetime, they launched a new style of resource ID's to a great deal of fanfare. The ID's are prefixed with an abbreviation of the object they represented. For example:
ch_
represents a Charge objectcus_
represents a Customer objectPatrick Collison offered the motivation for these prefixes on Quora:
At Clerk, we're big fans of the prefix and knew early on that we wanted to offer their convenience to our own customers.
That was the easy part of generating our IDs— we spent a lot more time deciding what to put after the prefix.
For the uninitiated, ID generation is a surprisingly wide subject area with many important considerations.
For a long time, it was commonplace for developers to rely on sequential IDs generated by the database. There are artifacts of this practice all over the web - but these days, they tend to be avoided because:
To mitigate these issues, randomness has been introduced into ID generation. Enough randomness ensures that IDs cannot be guessed and that collisions are avoided in distributed systems.
But purely random IDs - like UUIDs - also eliminate a trait of sequential IDs that developers love: they're sortable.
Finding a middle ground between sortable and unique IDs was a primary motivation behind two newer ID generators:
To generate the roughly-sorted 64 bit ids in an uncoordinated manner, we settled on a composition of: timestamp, worker number and sequence number.
It borrows core ideas from the ubiquitous UUID standard, adding time-based ordering and more friendly representation formats.
After considering both Snowflake and KSUID, we decided to use KSUID for our primary ID generator. The key factor in our decision was that Twitter's Snowflake included a worker number, which we did not need.
Combined with Stripe-like prefixes, our IDs look like this:
Start completely free for up to 10,000 monthly active users and up to 100 monthly active orgs. No credit card required.
Learn more about our transparent per-user costs to estimate how much your company could save by implementing Clerk.
The latest news and updates from Clerk, sent to your inbox.