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

sonyflake

A distributed unique ID generator inspired by Twitter's Snowflake

4 releases

0.2.0 Jun 22, 2023
0.1.2 Sep 11, 2021
0.1.1 Dec 29, 2020
0.1.0 Dec 29, 2020

#2081 in Algorithms

Download history 525/week @ 2024-03-13 555/week @ 2024-03-20 714/week @ 2024-03-27 578/week @ 2024-04-03 666/week @ 2024-04-10 703/week @ 2024-04-17 751/week @ 2024-04-24 796/week @ 2024-05-01 811/week @ 2024-05-08 1096/week @ 2024-05-15 862/week @ 2024-05-22 1284/week @ 2024-05-29 785/week @ 2024-06-05 1476/week @ 2024-06-12 1744/week @ 2024-06-19 1311/week @ 2024-06-26

5,504 downloads per month
Used in helyim

MIT/Apache

20KB
400 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.2"

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

~4–5.5MB
~97K SLoC