#flake #unique-id-generator #distributed-database #id-generator #flake-id #flake-id-generator

tiltflake

Tiltflake is a distributed database that uses the flake algorithm to generate unique IDs

1 unstable release

new 0.1.0 Apr 8, 2025

#560 in Database interfaces

MIT license

44KB
323 lines

Preview

A strict, deterministic, and time-traveling Snowflake ID generator for Rust — supports timewalk generation from SystemTime, DateTime, epoch milliseconds, or RFC3339 strings.


GitHub License Tests Forks Contributors Stars Open issues

Powered by Rust


🧩 Installation

Add the following to your Cargo.toml file:

[dependencies]
tiltflake = {version = "*", features = ["serde"]}

📖 Usage

Generate a Snowflake ID with a UNIX epoch

use tiltflake::flake::Tiltflake;
use std::error::Error;

fn main() -> Result<(), Box<dyn Error>> {
	let generator = Tiltflake::builder().build(); // Default machine_id is 1 and epoch is Unix
	let id = generator.generate_from_rfc3339("2025-04-08T12:00:00Z", 0)?;
	println!("Snowflake ID: {}", id);

	let (dt, machine_id, seq) = generator.parse(id);
	println!(
		"Parsed: {}, machine_id={}, sequence={}",
		dt, machine_id, seq
	);

	Ok(())
}

Generate a Snowflake ID with a custom epoch

use chrono::{TimeZone, Utc};
use tiltflake::{epoch::EpochType, flake::Tiltflake};

fn main() -> Result<(), Box<dyn std::error::Error>> {
	let epoch = EpochType::Custom(
		Utc.with_ymd_and_hms(2020, 1, 1, 0, 0, 0).single().unwrap(),
	);
	let generator = Tiltflake::builder()
		.with_epoch(epoch)
		.with_machine_id(1)
		.build();
	let id = generator.generate_from_rfc3339("2025-04-08T12:00:00Z", 0)?;
	println!("Snowflake ID: {}", id);

	let (dt, machine_id, seq) = generator.parse(id);
	println!(
		"Parsed: {}, machine_id={}, sequence={}",
		dt, machine_id, seq
	);

	Ok(())
}

Generate a Snowflake ID with a custom Discord epoch

use tiltflake::{epoch::EpochType, flake::Tiltflake};
use std::error::Error;

fn main() -> Result<(), Box<dyn Error>> {
	let generator = Tiltflake::builder()
		.with_machine_id(1)
		.with_epoch(EpochType::Discord)
		.build();
	let id = generator.generate_from_rfc3339("2025-04-08T12:00:00Z", 0)?;
	println!("Snowflake ID: {}", id);

	let (dt, machine_id, seq) = generator.parse(id);
	println!(
		"Parsed: {}, machine_id={}, sequence={}",
		dt, machine_id, seq
	);

	Ok(())
}

See all examples in the examples folder.


🤝 Contributing

Contributions are what make the open source community an amazing place to learn, be inspired, and create. Any contributions you make are greatly appreciated.

  1. Fork the repository
  2. Clone your fork git clone https://github.com/t1ltxz-gxd/tiltflake.git
  3. Create your feature branch git checkout -b feat-smth-amazing
  4. Stage changes git add .
  5. Commit your changes git commit -m 'feat: add some amazing feature'
    • Use Conventional Commits for commit messages.
    • Use fix, feat, docs, style, refactor, perf, test, chore prefixes.
    • Use the present tense ("add feature" not "added feature").
    • Use the imperative mood ("move cursor to..." not "moves cursor to...").
    • Limit the first line to 72 characters or less.
    • Reference issues and pull requests liberally after the first line.
  6. Push to the branch git push origin feat-smth-amazing
  7. Submit a pull request

❤️ Credits

Released with ❤️ by Tilt.

Dependencies

~1.2–2MB
~35K SLoC