Skip to content

Snowflake.ts

Creates compact, sortable identifiers for cluster messages and runtime events.

A snowflake id is a branded bigint built from a millisecond timestamp, a machine id, and a sequence number for that machine. The parts make generated ids sortable by time while still being unique for a runner. This module includes schemas for bigint and string encodings, helpers for creating and reading ids, and a Clock-backed generator service for cluster runtime use.

Since v4.0.0



Context service for a stateful snowflake id generator.

Signature

declare class Generator

Source

Since v4.0.0

Layer that provides the default snowflake Generator service.

Signature

declare const layerGenerator: Layer.Layer<Generator, never, never>

Source

Since v4.0.0

Creates a stateful snowflake generator using Clock.

Details

The generator starts with a random machine id, never moves generated timestamps backward, resets the sequence each millisecond, and advances the timestamp when more than 4096 ids are requested in the same millisecond.

Signature

declare const makeGenerator: Effect.Effect<Snowflake.Generator, never, never>

Source

Since v4.0.0

Defines the custom snowflake epoch in Unix milliseconds.

Signature

declare const constEpochMillis: number

Source

Since v4.0.0

Constructs a branded cluster snowflake id from a bigint or bigint-compatible string.

Signature

declare const Snowflake: (input: string | bigint) => Snowflake

Source

Since v4.0.0

Creates a branded snowflake id from a timestamp, machine id, and sequence number, using the custom snowflake epoch and 10-bit machine id and 12-bit sequence fields.

When to use

Use to pack known timestamp, machine id, and sequence parts into a branded snowflake id.

Gotchas

Machine id values are encoded modulo 1024, and sequence values modulo 4096; values outside those ranges wrap instead of being rejected.

See

  • toParts for the inverse operation that decodes a snowflake id into timestamp, machine id, and sequence parts
  • makeGenerator for generating ids with Clock-backed timestamp and sequence management

Signature

declare const make: (options: {
readonly machineId: MachineId
readonly sequence: number
readonly timestamp: number
}) => Snowflake

Source

Since v4.0.0

Branded bigint identifier composed from a timestamp, machine id, and per-machine sequence number.

Signature

type Snowflake = Brand.Branded<bigint, TypeId>

Source

Since v4.0.0

Extracts the timestamp from a snowflake id as a DateTime.Utc.

Signature

declare const dateTime: (snowflake: Snowflake) => DateTime.Utc

Source

Since v4.0.0

Extracts the machine id component from a snowflake id.

Signature

declare const machineId: (snowflake: Snowflake) => MachineId

Source

Since v4.0.0

Extracts the per-machine sequence component from a snowflake id.

Signature

declare const sequence: (snowflake: Snowflake) => number

Source

Since v4.0.0

Extracts the Unix timestamp in milliseconds from a snowflake id.

Signature

declare const timestamp: (snowflake: Snowflake) => number

Source

Since v4.0.0

Decomposes a snowflake id into its timestamp, machine id, and sequence parts.

Signature

declare const toParts: (snowflake: Snowflake) => Snowflake.Parts

Source

Since v4.0.0

Schema for snowflake ids represented as branded bigints.

Signature

declare const SnowflakeFromBigInt: SnowflakeFromBigInt

Source

Since v4.0.0

Schema type for snowflake ids represented as branded bigints.

Signature

export interface SnowflakeFromBigInt extends Schema.brand<Schema.BigInt, TypeId> {}

Source

Since v4.0.0

Schema that decodes snowflake ids from strings into branded bigints and encodes them back to strings.

Signature

declare const SnowflakeFromString: SnowflakeFromString

Source

Since v4.0.0

Schema type for snowflake ids decoded from strings into branded bigints.

Signature

export interface SnowflakeFromString extends Schema.decodeTo<SnowflakeFromBigInt, Schema.String> {}

Source

Since v4.0.0

Runtime brand identifier for cluster snowflake ids.

Signature

declare const TypeId: "~effect/cluster/Snowflake"

Source

Since v4.0.0

Type-level representation of the cluster snowflake brand identifier.

Signature

type TypeId = typeof TypeId

Source

Since v4.0.0

Namespace containing support types for snowflake parts and generators.

Source

Since v4.0.0

Decoded components of a snowflake id: Unix timestamp milliseconds, machine id, and sequence number.

Signature

export interface Parts {
readonly timestamp: number
readonly machineId: MachineId
readonly sequence: number
}

Source

Since v4.0.0

Stateful generator for runner-local snowflake ids, exposing an unsafe synchronous nextUnsafe operation and an effectful machine id setter.

Signature

export interface Generator {
readonly nextUnsafe: () => Snowflake
readonly setMachineId: (machineId: MachineId) => Effect.Effect<void>
}

Source

Since v4.0.0