#unique-id-generator #snowflake-id #generator #random #random-id

sonyflake

A distributed unique ID generator inspired by Twitter's Snowflake

6 releases (3 breaking)

Uses new Rust 2024

0.4.0 Jul 20, 2025
0.3.0 Oct 9, 2024
0.2.0 Jun 22, 2023
0.1.2 Sep 11, 2021
0.1.1 Dec 29, 2020

#2290 in Algorithms

Download history 2255/week @ 2026-01-23 2015/week @ 2026-01-30 1627/week @ 2026-02-06 1495/week @ 2026-02-13 1921/week @ 2026-02-20 4060/week @ 2026-02-27 3485/week @ 2026-03-06 2345/week @ 2026-03-13 1385/week @ 2026-03-20 1924/week @ 2026-03-27 2130/week @ 2026-04-03 1982/week @ 2026-04-10 1928/week @ 2026-04-17 1824/week @ 2026-04-24 1539/week @ 2026-05-01 2296/week @ 2026-05-08

7,919 downloads per month
Used in 9 crates (6 directly)

MIT/Apache

22KB
422 lines

sonyflake-rs

Build crates.io docs.rs License

A distributed unique ID generator inspired by Twitter's Snowflake.

This is a Rust implementation of the original sony/sonyflake, which is written in Go.

A Sonyflake ID is composed of

  • 39 bits for time in units of 10 msec
  • 8 bits for a sequence number
  • 16 bits for a machine id

Install

Add the following to your Cargo.toml:

[dependencies]
sonyflake = "0.3"

Quickstart

use sonyflake::Sonyflake;

let sf = Sonyflake::new().unwrap();
let next_id = sf.next_id().unwrap();
println!("{}", next_id);

Benchmarks

Benchmarks were run on a MacBook Pro (15-inch, 2017) with a 2,8GHz i7 and 16 GB memory. Run them yourself with cargo bench.

test bench_decompose ... bench:       1,066 ns/iter (+/- 132)
test bench_new       ... bench:     738,129 ns/iter (+/- 318,192)
test bench_next_id   ... bench:      37,390 ns/iter (+/- 499)

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~1–2MB
~32K SLoC