TypedFlake
Snowflake-style IDs for Rust, where every kind of ID is its own type.
- One type per ID.
UserIdandOrderIdcannot be mixed up. - Sortable by time. Newer IDs are larger.
- Fits your database. IDs are
i64and never negative, ready for aBIGINTcolumn. - Works across servers. Each process generates its own IDs, with no coordination.
- Custom formats. Choose the epoch and the bit layout, checked at compile time.
- Text encoding. Base 62 for URLs, or any alphabet you define.
- Optional integrations. Serde, SQLx (PostgreSQL), and
postgres-types.
Quick start
[]
= "0.2"
use typedflake;
;
;
17 is the node: a number that identifies this process. Give each running instance of your application a different one (a replica index, a configured value), so two instances never produce the same ID.
Declaring IDs
Put #[typedflake(...)] on a tuple struct with one private i64 or u64 field, above any #[derive]. The type already comes with Debug, Display, FromStr, Clone, Copy, comparison, and hashing, so do not derive those again.
An ID packs three fields into one integer:
[ timestamp ][ node ][ sequence ]
- timestamp: milliseconds since your epoch.
- node: which process generated the ID.
- sequence: a counter within one millisecond.
Epoch
The epoch is the date your timestamps start counting from. Pick a date shortly before your first ID and never change it, because existing IDs depend on it.
It is written as a YYYY-MM-DD date, at midnight UTC.
Bits
By default an ID has 10 node bits (1024 nodes), 12 sequence bits (4096 IDs per millisecond per node), and the rest for the timestamp:
| Integer | Timestamp bits | Lasts |
|---|---|---|
i64 |
41 | about 69 years |
u64 |
42 | about 139 years |
Prefer i64 unless you need the extra bit: most databases only have signed 64-bit integers.
To choose the widths yourself, give all three:
use typedflake;
;
The total may be less than the integer holds, which gives shorter numbers in exchange for capacity.
Shared formats
Several ID types can share one format:
use ;
pub const APP_IDS: Format = Format ;
;
;
A format that does not fit, such as 64 bits in an i64, is a compile error.
Nodes
A node can be a plain number, as in the quick start, or a struct that splits the node bits into named fields:
use ;
;
Fields are u8, u16, or u32, and their bits must add up to the format's node bits. A value too large for its bits is an error.
Generating IDs
With a default node
Call typedflake::init(node) once at startup, then use generate() anywhere:
use typedflake;
;
If some IDs use a plain number and others a TypedNode struct, call init once for each kind of node.
Calling init twice with the same kind of node is an error. Tests that share a process can ignore it: let _ = typedflake::init(0);.
With an explicit generator
A generator carries its own node and does not need init:
use typedflake;
;
Generators are cheap to clone and safe to share between threads.
When generation fails
generate() returns immediately and never waits. It returns an error when:
- all the IDs for the current millisecond are used (
SequenceExhausted); - the system clock went backwards (
ClockRollback); it works again once the clock catches up; - the clock is before the epoch, or the timestamp bits have run out.
To wait for the next millisecond instead of handling SequenceExhausted yourself:
use typedflake;
;
async
Working with IDs
use typedflake;
;
Converting from an integer or a string checks that the value is a valid ID for that type, so it returns a Result.
To write code that is generic over ID types, use the typedflake::Id trait.
Encoding IDs as text
An ID is a number, and to_string() writes it in decimal: up to 19 digits. For URLs, share links, or codes people type, give the type an alphabet and it gains encode() and decode():
use ;
;
- Fixed length. Every ID of a type encodes to the same number of characters.
- One text per ID.
decode()only accepts exactly whatencode()writes. - Only for text.
to_string()and the database keep using the number. So does Serde, unless you deriveSerdeEncoded.
Built-in alphabets
| Alphabet | Characters | Length of an i64 ID |
|---|---|---|
Alphabet::BASE36 |
0-9 a-z |
13 |
Alphabet::BASE58 |
Bitcoin's: no 0, O, I, or l |
11 |
Alphabet::BASE62 |
0-9 A-Z a-z |
11 |
Alphabet::BASE64_URL |
- 0-9 A-Z _ a-z |
11 |
With these, encoded IDs sort the same way the IDs do. BASE64_URL has the characters of base64url in a different order: it is not base64, and its IDs can start with -.
Your own alphabet
Any 2 to 94 different ASCII characters work. The first one is the digit zero:
use ;
// Digits and lowercase letters, without the easily confused `0`, `1`, `i`, `l`, and `o`.
pub const FRIENDLY: Alphabet = new;
;
A repeated or unsupported character is a compile error. Fewer characters make longer IDs, and encoded IDs sort like the IDs only if the characters are listed in ASCII order.
To write code that is generic over encoded IDs, use the typedflake::EncodedId trait.
Integrations
Each integration is a Cargo feature plus a derive on the IDs that need it:
| Derive | Feature | For |
|---|---|---|
typedflake::Serde |
serde |
Serde, as the decimal number |
typedflake::SerdeEncoded |
serde |
Serde, as the encoded text |
typedflake::SqlxPostgres |
sqlx-postgres |
SQLx 0.8 with PostgreSQL |
typedflake::Postgres |
postgres |
postgres-types 0.2 (tokio-postgres, postgres) |
The tokio feature adds generate_async.
Use these derives instead of serde::Deserialize, sqlx::Type, and similar: they check every value they read.
Serde
use ;
use typedflake;
;
IDs are written as strings because JavaScript and many JSON parsers lose precision on large numbers.
To write the encoded text instead, give the type an alphabet and derive typedflake::SerdeEncoded:
use ;
;
A type uses one derive or the other. SerdeEncoded reads only the encoded text, not integers or decimal strings, so switching an existing type to it changes what your clients must send.
SQLx
use typedflake;
;
async
IDs are stored as BIGINT. With the query! macros, use SQLx's usual type override: id AS "id: UserId".
postgres-types
use typedflake;
;
async
Both database derives work with i64 IDs only, since PostgreSQL has no unsigned 64-bit integer.
examples/axum-sqlx is a small HTTP service that uses Serde and SQLx together.
Good to know
- Each process needs its own node. Two processes generating the same ID type with the same node can produce the same ID.
- Keep the system clock in sync. A clock that jumps backwards while the process is stopped can lead to repeated IDs after it restarts.
- IDs are unique per type. A
UserIdand anOrderIdcan have the same number. - IDs sort roughly by creation time. Exactly within one node, and to the millisecond across nodes.
Migrating from 0.1
See MIGRATION.md for the API changes and for how to keep reading existing 0.1 IDs.
Performance
Measured with cargo bench -p typedflake --bench performance on an AMD Ryzen 7 5700G:
| Operation | Time |
|---|---|
generate() |
35 ns |
generate(), 4 threads sharing one node |
18 ns |
try_from(i64) |
under 1 ns |
to_string() |
25 ns |
parse() |
12 ns |
encode(), base 62 |
8 ns |
decode(), base 62 |
14 ns |
License
Acknowledgments
Algorithm inspirations:
- Twitter Snowflake (original algorithm)
- Discord's Snowflake implementation
Design philosophy: