Crates.io | sulid |
lib.rs | sulid |
version | |
source | src |
created_at | 2024-09-07 06:25:48.965915 |
updated_at | 2025-01-11 04:16:04.023542 |
description | SULID is a unique ID generation algorithm that combines the benefits of ULID and Snowflake. |
homepage | |
repository | https://github.com/andeya/sulid |
max_upload_size | |
id | 1366963 |
Cargo.toml error: | TOML parse error at line 18, column 1 | 18 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
SULID is a unique ID generation algorithm that combines the benefits of ULID and Snowflake. It offers a highly efficient, reliable, and lexicographically sortable identifier, which ensures uniqueness across multiple data centers and machines, making it ideal for high-concurrency distributed environments.
SULID is based on the ULID (Universally Unique Lexicographically Sortable Identifier) format but incorporates additional bits for a data center ID and machine ID, similar to Snowflake. This design ensures uniqueness in distributed environments and maintains time-ordering characteristics, making it suitable for applications requiring both high concurrency and global uniqueness, such as microservice architectures, log tracking, and order generation.
SULIDs have a unique structure comprising the following parts, adding up to a 128-bit identifier:
Here is a visual breakdown of the SULID format:
| 42-bit Timestamp | 76-bit Random Number | 10-bit Worker ID (5-bit Data Center ID | 5-bit Machine ID) |
Here is a visual breakdown of the SULID format:
| 52-bit Timestamp | 66-bit Random Number | 10-bit Worker ID (5-bit Data Center ID | 5-bit Machine ID) |
To use SULID, add the following dependencies to your Cargo.toml
file:
[dependencies]
sulid = "0.7"
Here's how you can use the SulidGenerator
in your project:
use sulid::{SulidGenerator, TimestampType};
fn main() {
#[cfg(feature = "std")]
{
let generator = SulidGenerator::new1(1, 1, TimestampType::MS);
for _ in 0..3 {
let id = generator.generate();
println!("SULID-MS: {}", id);
}
let generator = SulidGenerator::new2(1, TimestampType::US);
for _ in 0..3 {
let id = generator.generate();
println!("SULID-US: {}", id);
}
}
#[cfg(not(feature = "std"))]
{
let generator = SulidGenerator::new1(1, 1, TimestampType::MS);
for i in 0..3 {
let id = generator.generate(1736611200000, i);
println!("SULID-MS: {}", id);
}
let generator = SulidGenerator::new2(1, TimestampType::US);
for i in 0..3 {
let id = generator.generate(1736611200000, i);
println!("SULID-US: {}", id);
}
}
}
Running the example code generates SULIDs such as:
SULID-V1: 01J75Y6K09Q0VD4D7HCF496K11
SULID-V1: 01J75Y6K09ZGD0ATFGNJ7TWZ11
SULID-V1: 01J75Y6K09DWG2RPNEXNYSRP11
SULID-V2: 01J75Y6K09FVT8QTQVYGSDJ401
SULID-V2: 01J75Y6K0A16WE3XQN92QPP701
SULID-V2: 01J75Y6K0AN12FBKX56YGZF301
SULID leverages both ULID and Snowflake's strengths:
ULID
: Provides lexicographically sortable identifiers based on a timestamp and randomness.Snowflake
: Adds data center and machine IDs to ensure distributed uniqueness.By combining these two approaches, SULID generates IDs that are globally unique, time-ordered, and suitable for high-concurrency distributed environments.
This project is licensed under the MIT License.