ferroid
ferroid is a Rust crate for
generating and parsing Snowflake-style unique IDs, compatible with public
formats used by platforms like Twitter, Discord, Instagram, and Mastodon. These
64-bit identifiers encode timestamps, machine/shard IDs, and sequence
numbersβmaking them lexicographically sortable, scalable, and ideal for
distributed systems.
This crate provides:
- π Bit-level layout compatibility with major Snowflake formats
- π§© Pluggable time sources via the
TimeSourcetrait - π§΅ Lock & Lock-free and thread-safe ID generation
- π Customizable layouts via the
Snowflaketrait - π’ Lexicographically sortable string output
π¦ Supported Layouts
| Platform | Timestamp Bits | Machine ID Bits | Sequence Bits | Epoch |
|---|---|---|---|---|
| 41 | 10 | 12 | 2010-11-04 01:42:54.657 | |
| Discord | 42 | 10 | 12 | 2015-01-01 00:00:00.000 |
| 41 | 13 | 10 | 2011-01-01 00:00:00.000 | |
| Mastodon | 48 | 0 | 16 | 1970-01-01 00:00:00.000 |
π§ Generator Comparison
| Generator | Thread-Safe | Lock-Free | Throughput | Use Case |
|---|---|---|---|---|
BasicSnowflakeGenerator |
β | β | Highest | Single-threaded, one per thread |
LockSnowflakeGenerator |
β | β | Medium | Multi-threaded, high contention |
AtomicSnowflakeGenerator |
β | β | Medium | Multi-threaded, low-to-medium contention |
All generators produce monotonically increasing, time-ordered, and unique IDs.
π Usage
Generate an ID
use ;
let clock = with_epoch;
let mut generator = new;
let id = loop ;
println!;
Behavior
- If the clock advances: reset sequence to 0 β
IdGenStatus::Ready - If the clock is unchanged: increment sequence β
IdGenStatus::Ready - If the clock goes backward: return
IdGenStatus::Pending - If the sequence overflows: return
IdGenStatus::Pending
Serialize as padded string
use ;
let id = from;
println!;
// > id: 517811998762
println!;
// > id padded: 00000000517811998762
// Crockford base32
let encoded = id.encode;
println!;
// > encoded: 00000Y4G0082M
// Decode from Base32
let decoded = decode.expect;
assert_eq!;
π Benchmarks
ferroid ships with Criterion benchmarks to measure ID generation
performance:
BasicSnowflakeGenerator: single-threaded generatorLockSnowflakeGenerator: mutex-based, thread-safe generatorAtomicSnowflakeGenerator: lock-free, thread-safe generator
Benchmark scenarios include:
- Generating IDs from a single thread with a mock clock
- Generating IDs from a single thread with a real clock
- Generating IDs from multiple threads with a mock clock
- Generating IDs from multiple threads with a real clock
NOTE: Generators may perform worse under multithreaded contention due to locking or atomic compare-and-swap (CAS) overhead. For maximum throughput, assign a separate generator to each thread and avoid contention entirely.
To run:
π§ͺ Testing
Run with:
π License
Licensed under either of:
- [Apache License, Version 2.0](LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0)
- [MIT License](LICENSE-MIT or https://opensource.org/licenses/MIT)
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.