#id-generator #distributed-id #snowflake-id #id #snowflake #generator #random

sonyflake

A distributed unique ID generator inspired by Twitter's Snowflake

5 unstable releases

0.3.0 Oct 9, 2024
0.2.0 Jun 22, 2023
0.1.2 Sep 11, 2021
0.1.1 Dec 29, 2020
0.1.0 Dec 29, 2020

#684 in Algorithms

Download history 1758/week @ 2024-11-25 1615/week @ 2024-12-02 1565/week @ 2024-12-09 2086/week @ 2024-12-16 1488/week @ 2024-12-23 1141/week @ 2024-12-30 1359/week @ 2025-01-06 1204/week @ 2025-01-13 1205/week @ 2025-01-20 726/week @ 2025-01-27 1149/week @ 2025-02-03 948/week @ 2025-02-10 1276/week @ 2025-02-17 1686/week @ 2025-02-24 1799/week @ 2025-03-03 1680/week @ 2025-03-10

6,476 downloads per month
Used in 5 crates (2 directly)

MIT/Apache

20KB
399 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.7–2.7MB
~48K SLoC