1 unstable release
new 0.2.2 | Nov 17, 2024 |
---|---|
0.2.1 |
|
0.1.2 |
|
#56 in Database implementations
432 downloads per month
14KB
209 lines
Small UID
⚠️ This project is in experimental phase, the API may may be subject to change.
This is a Rust implementation of Small UIDs, generating unique identifiers with short strings and lexicographically sortable. Check that repository for implementation in other languages.
UUIDs are frequently used as database Primary Key in software development. However, they aren't the best choice mainly due to their random sorting and the resulting fragmentation in databases indexes.
Using ULIDs is generally a very good alternative, solving most of UUID flaws.
Twitter's Snowflake is another option if you want to generate roughly sortable uid. But, Snowflake is not using random numbers instead it used machine id to generate the uid. It's a good choice if you integrate it into a distributed systems and doesn't really need randomness.
Small UIDs are also an ideal alternative when you do not need as much uniqueness and want shorter "user-friendly" encoded strings.
Introduction
Small UIDs are short unique identifiers especially designed to be used as efficient database Primary Key:
- Half smaller than UUID / ULID (64-bit)
- Lexicographically sortable
- Encodable as a short user-friendly and URL-safe base-64 string (
a-zA-Z0-9_-
) - User-friendly strings are generated in a way to be always very different (no shared prefix due to similar timestamps)
Small UID | ULID | UUID v4 | |
---|---|---|---|
Size | 64 bits | 128 bits | 128 bits |
Monotonic sort order | Yes *** | Yes | No |
Random bits | 20 | 80 | 122 |
Collision odds ** | 1,024 / ms* | 1.099e+12 / ms* | 2.305e+18 |
* theorical number of generated uids before the first expected collision.
** the uid includes a timestamp, so collisions may occur only during
the same millisecond.
*** monotonic sort order, but random order when generated at the
same millisecond.
They are internally stored as 64-bit integers (44-bit timestamp followed by 20 random bits):
|-----------------------| |------------|
Timestamp Randomness
44 bits 20 bits
The random number suffix still guarantees a decent amount of uniqueness when many ids are created in the same millisecond (up to 1,048,576 different values) and you may only expect collision if you're generating more than 1024 random ids during the same millisecond.
Sorting
Because of the sequential timestamp, Small UIDs are naturally sorted chronologically. It improves indexing when inserting values in databases, new ids being appended to the end of the table without reshuffling existing data (read more in this article).
However, sort order within the same millisecond is not guaranteed because of the random bits suffix.
Examples of usage
Generating Small UIDs
let smalluid1 = SmallUid::new();
let smalluid2 = SmallUid::try_from("GSntNvOw6n8".to_string()).unwrap();
Converting Small UIDs
let smalluid = SmallUid::new();
let uid_string = smalluid.to_string();
Dependencies
~1.2–1.8MB
~27K SLoC