snowflake-rs

Snowflake-Me

English Chinese

Crates.io Docs.rs Build License Miri Semver

A high-performance, highly concurrent, distributed Snowflake ID generator in Rust.

ID allocation uses a lock-free CAS loop. Calls can wait when the sequence is exhausted or the clock moves backward; an optional std wait timeout bounds this waiting.

Highlights

Snowflake ID Structure

The generated ID is a 64-bit unsigned integer (u64) with the following default structure:

┌─────────────────────────────────────────────────────────────────┐
│ 0 │ 41 bits: time  │ 5 bits: dc │ 5 bits: machine │ 12 bits: seq │
└─────────────────────────────────────────────────────────────────┘

Note: The bit lengths must sum to 63. Machine and data center sections each support 0–16 bits; a zero-width section requires a zero ID. Invalid layouts are rejected before constructing the generator.

Uniqueness and capacity

Share one generator, or its clones, for each (data_center_id, machine_id) pair. Separately constructed generators do not share state: two Snowflake::default() instances both use (0, 0) and can issue duplicate IDs. Production deployments must allocate distinct node pairs to independent generators using the same epoch and bit layout.

State is in memory. When reusing a node pair after a restart, ensure its clock is later than the previous generator’s last issued timestamp, or use external coordination that prevents overlapping use and timestamp reuse. IP fallback is only a configuration convenience: with default widths, 192.168.1.1 and 192.168.33.33 both map to (1, 1).

The default 12-bit sequence permits 4096 IDs per millisecond, approximately 4.096 million IDs per second per node pair under a normally advancing clock. Batching reduces atomic operations and allocation costs but does not increase that capacity. More sequence bits reduce the timestamp lifetime or node namespace; changing layouts requires a migration plan. See benchmark methodology.

Quick Start

1. Add Dependency

The library supports Rust 1.88 and newer. Development tests and benchmarks use the current stable toolchain. Source on main may include unreleased APIs; see CHANGELOG.md.

Add this library to your Cargo.toml:

[dependencies]
snowflake_me = "2.2.0"

To enable the IP address fallback feature:

[dependencies]
snowflake_me = { version = "2.2.0", features = ["ip-fallback"] }

To enable all optional features at once:

[dependencies]
snowflake_me = { version = "2.2.0", features = ["full"] }

Feature Flags

Feature Default Description
std Yes Standard library support (time via jiff). Disable for no_std environments.
ip-fallback No Auto-detect machine_id and data_center_id from local network interfaces (IPv4/IPv6). Requires std.
serde No Serde Serialize/Deserialize for SnowflakeId (u64) and SnowflakeIdString (string).
tracing No Structured logging at key points. Requires std.
metrics No Counters and gauges for observability. Requires std.
use-strong-cas No Use compare_exchange instead of compare_exchange_weak. Eliminates spurious CAS failures; performance depends on hardware and workload.
full No Enables std, serde, tracing, metrics, and ip-fallback; retains weak CAS.

2. Basic Usage

# #[cfg(feature = "std")] {
use snowflake_me::Snowflake;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Create a generator with explicit machine and data center IDs.
    // Alternatively, enable the `ip-fallback` feature and use `Snowflake::new()`
    // to auto-detect IDs from the local network interface.
    let sf = Snowflake::builder()
        .machine_id(&|| Ok(1))
        .data_center_id(&|| Ok(1))
        .finalize()?;

    // Generate a unique ID
    let id = sf.next_id()?;
    println!("Generated Snowflake ID: {id}");

    Ok(())
}
# }

3. Batch Generation

Generate multiple unique IDs in a single call:

# #[cfg(feature = "std")] {
use snowflake_me::Snowflake;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let sf = Snowflake::builder()
        .machine_id(&|| Ok(1))
        .data_center_id(&|| Ok(1))
        .finalize()?;

    let ids = sf.next_ids(100)?;
    println!("Generated {} IDs", ids.len());

    Ok(())
}
# }

For a reusable allocation, append into an existing vector. On error, successfully appended IDs remain valid and reserved; the length increase tells you how many were generated:

use snowflake_me::Snowflake;
let sf = Snowflake::builder()
    .start_time(0)
    .clock_source(|| 1_000)
    .machine_id(&|| Ok(1))
    .data_center_id(&|| Ok(1))
    .finalize()?;
let mut ids = Vec::with_capacity(100);
sf.next_ids_into(100, &mut ids)?;
ids.clear(); // Reuse the allocation for the next batch.
sf.next_ids_into(100, &mut ids)?;
# Ok::<(), snowflake_me::Error>(())

4. Multi-threaded Usage

Snowflake instances can be efficiently cloned and shared between threads.

# #[cfg(feature = "std")] {
use snowflake_me::Snowflake;
use std::thread;
use std::sync::Arc;
use std::collections::HashSet;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Manually configure machine_id and data_center_id using the Builder.
    // This is the recommended approach for production environments.
    let sf = Snowflake::builder()
        .machine_id(&|| Ok(10))
        .data_center_id(&|| Ok(5))
        .finalize()?;

    let sf_arc = Arc::new(sf);
    let mut handles = vec![];

    for _ in 0..10 {
        let sf_clone = Arc::clone(&sf_arc);
        let handle = thread::spawn(move || {
            let mut ids = Vec::new();
            for _ in 0..10000 {
                ids.push(sf_clone.next_id().unwrap());
            }
            ids
        });
        handles.push(handle);
    }

    let mut all_ids = HashSet::new();
    for handle in handles {
        let ids = handle.join().unwrap();
        for id in ids {
            assert!(all_ids.insert(id), "Found duplicate ID: {id}");
        }
    }

    println!("Successfully generated {} unique IDs across 10 threads.", all_ids.len());
    Ok(())
}
# }

5. Decomposing an ID

You can decompose a Snowflake ID back into its components for debugging or analysis.

# #[cfg(feature = "std")] {
use snowflake_me::Snowflake;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let sf = Snowflake::builder()
        .machine_id(&|| Ok(15))
        .data_center_id(&|| Ok(7))
        .finalize()?;

    let id = sf.next_id()?;
    let decomposed = sf.decompose(id);

    println!("ID: {}", decomposed.id);
    println!("Time: {}", decomposed.time);
    println!("Data Center ID: {}", decomposed.data_center_id);
    println!("Machine ID: {}", decomposed.machine_id);
    println!("Sequence: {}", decomposed.sequence);

    assert_eq!(decomposed.machine_id, 15);
    assert_eq!(decomposed.data_center_id, 7);

    Ok(())
}
# }

6. Clock Drift Protection

If the system clock moves backward (e.g., due to NTP adjustments), the generator handles it based on the configured strategy. By default, it waits until the clock catches up, using a short spin followed by scheduler yields in std builds. A stopped clock can block indefinitely unless a wait timeout is configured.

# #[cfg(feature = "std")] {
use snowflake_me::{Snowflake, ClockDriftStrategy};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let sf = Snowflake::builder()
        .machine_id(&|| Ok(1))
        .data_center_id(&|| Ok(1))
        .clock_drift_strategy(ClockDriftStrategy::Wait)
        .max_clock_drift_ms(5000)  // fail if drift exceeds 5 seconds
        .wait_timeout(std::time::Duration::from_millis(100))
        .finalize()?;

    let id = sf.next_id()?;
    println!("Generated ID: {id}");

    Ok(())
}
# }

Available strategies:

wait_timeout(Duration) is available with std and bounds the waiting period within one generation call using a monotonic clock. It is separate from the allowed wall-clock rollback magnitude. Calls can return Error::WaitTimeout; timestamps before the epoch are reported as Error::BeforeEpoch when they cannot be handled by the configured strategy.

For deterministic tests or a device clock, use Builder::clock_source(F) with a thread-safe closure returning Unix milliseconds. Clones share that clock; each independently built generator can use its own source. The default remains the system clock with std, or set_time_source without it.

7. no_std Usage

In no_std environments, disable default features and provide a time source. An allocator and native 64-bit atomics are required; CI checks aarch64-unknown-none, including the optional serde feature. The supplied clock must advance independently while a generation call waits (for example in an interrupt); updating it only after the call returns cannot release a sequence-exhaustion wait:

[dependencies]
snowflake_me = { version = "2.2.0", default-features = false }
use snowflake_me::{Snowflake, set_time_source};

// Call periodically (e.g., from a timer interrupt or RTC read)
set_time_source(get_current_millis());

let sf = Snowflake::builder()
    .start_time(1_640_995_200_000) // 2022-01-01 UTC in milliseconds
    .machine_id(&|| Ok(1))
    .data_center_id(&|| Ok(1))
    .finalize()
    .unwrap();

let id = sf.next_id().unwrap();

Upgrading to 2.2.0

The default ID layout and existing encoding alphabets are unchanged. Review these compatibility changes when upgrading from 2.1.x:

New APIs include Builder::clock_source, Builder::wait_timeout (with std), and Snowflake::next_ids_into for reusable buffers. See the 2.2.0 changelog for the complete changes and benchmark methodology for interpreting performance results.

Migration from v0.6.x

If you are upgrading from v0.6.x, note the following breaking changes:

Contributing

Issues and Pull Requests are welcome.

License

This project is dual-licensed under the MIT and Apache 2.0 licenses.