Typed UUIDs for free
I like Stripe IDs. They are an encoding of strongly typed identifiers, IDs with a type prefix. For example, a customer ID cus_yV58jmuUEUGDDVThFIxuaw won’t be confused with a payment ID py_b518dLn270WLyIIe7zLMPw. And I am not alone. Others have implemented this as an open source library.
The examples shown are 128 bit UUIDs encoded as 22 characters in base64. But since each base64 character encodes 6 bits, we have actually encoded a total of 22*6=132 bits. 132-128=4 bits are wasted.
But what if we could use those 4 extra bits? 4 bits is 2^4=16 elements. If we have a fixed number of types less than 16, we could encode the type as 4 bits and prefix it to the UUID. This would result in 22 characters in base64.
While we would lose the ability to look IDs at a glance to see their type, APIs can still produce an useful error message when unexpected typed ID is provided.
