AmasiaLabs.Toolkit.FlowflakeId 1.4.21

dotnet add package AmasiaLabs.Toolkit.FlowflakeId --version 1.4.21
                    
NuGet\Install-Package AmasiaLabs.Toolkit.FlowflakeId -Version 1.4.21
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="AmasiaLabs.Toolkit.FlowflakeId" Version="1.4.21" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="AmasiaLabs.Toolkit.FlowflakeId" Version="1.4.21" />
                    
Directory.Packages.props
<PackageReference Include="AmasiaLabs.Toolkit.FlowflakeId" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add AmasiaLabs.Toolkit.FlowflakeId --version 1.4.21
                    
#r "nuget: AmasiaLabs.Toolkit.FlowflakeId, 1.4.21"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package AmasiaLabs.Toolkit.FlowflakeId@1.4.21
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=AmasiaLabs.Toolkit.FlowflakeId&version=1.4.21
                    
Install as a Cake Addin
#tool nuget:?package=AmasiaLabs.Toolkit.FlowflakeId&version=1.4.21
                    
Install as a Cake Tool

AmasiaLabs.Toolkit.FlowflakeId

Snowflake-like unique ID generator with configurable epoch and time source, preserving compatibility with an existing seconds-based layout.

Install

dotnet add package AmasiaLabs.Toolkit.FlowflakeId

Configuration

Default section path: Amasia:Toolkit:FlowflakeId.

appsettings.json:

{
  "Amasia": {
    "Toolkit": {
      "FlowflakeId": {
        "InstanceId": 1,
        "UseUtcNow": true,
        "FlowflakeClock": {
          "Epoch": "2023-02-15T00:00:00Z",
          "TimeSemantics": "UtcNormalized"
        }
      }
    }
  }
}

You can also bind from any custom section path by passing it to AddFlowflakeId(configuration, sectionPath).

ID Layout (compat mode)

  • Bits 31..63: seconds since epoch (you must set Epoch explicitly)
  • Bits 22..30: instance id (1..511)
  • Bits 0..21: sequence (22 bits, max 2^22-1)

Defaults mirror modern, UTC-normalized behavior (safer for distributed systems). To preserve legacy behavior, set TimeSemantics = LegacyUnspecifiedEpoch.

Usage

using AmasiaLabs.Toolkit.FlowflakeId.Extensions;

var builder = Host.CreateApplicationBuilder(args);

// Simplest - uses IConfiguration from DI, reads from "Amasia:Toolkit:FlowflakeId"
builder.Services.AddFlowflakeId();

// Or with additional configuration
builder.Services.AddFlowflakeId(o => o.FailoverInstanceId = 999);

// Or bind from explicit configuration
builder.Services.AddFlowflakeId(builder.Configuration);

// Or bind from a custom path
builder.Services.AddFlowflakeId(builder.Configuration, sectionPath: "My:Custom:Section");

// Or configure in code only (no config binding)
builder.Services.AddFlowflakeId(o =>
{
    o.InstanceId = 1;
    o.UseUtcNow = true;
    o.FlowflakeClock = new FlowflakeClockOptions
    {
        Epoch = new DateTime(2023, 02, 15),
        TimeSemantics = FlowflakeTimeSemantics.UtcNormalized
    };
});

var app = builder.Build();

var ids = app.Services.GetRequiredService<IFlowflakeId>();
var id = await ids.GenerateAsync();

API

public interface IFlowflakeId
{
    ValueTask<long> GenerateAsync(CancellationToken cancellationToken = default);
    ValueTask<long> GenerateForDateAsync(DateTime date, CancellationToken cancellationToken = default);
    ValueTask<long[]> GenerateBatchAsync(int size, CancellationToken cancellationToken = default);
    ValueTask<long[]> GenerateBatchForDateAsync(DateTime date, int size, CancellationToken cancellationToken = default);

    int InstanceId { get; }  // Property to get the configured instance ID
}

Extracting Information from IDs

// Using extension methods from AmasiaLabs.Toolkit.FlowflakeId.Extensions
using AmasiaLabs.Toolkit.FlowflakeId.Extensions;
using AmasiaLabs.Toolkit.FlowflakeId.Abstractions;

var id = await generator.GenerateAsync();

// Extract components
int instanceId = id.GetInstanceIdFromFlowflakeId();
int sequenceNumber = id.GetSequenceNumberFromFlowflakeId();
long timestamp = id.GetTimestampFromFlowflakeId(); // seconds since epoch

// Extract DateTime using configured clock
var options = app.Services.GetRequiredService<IOptions<FlowflakeIdOptions>>();
DateTime dateTime = id.GetDateTimeFromFlowflakeId(options.Value.ToFlowflakeClock());

// Or if you have FlowflakeClockOptions directly (for decode-only scenarios)
var clockOptions = app.Services.GetRequiredService<IOptions<FlowflakeClockOptions>>();
DateTime dateTime = id.GetDateTimeFromFlowflakeId(clockOptions.Value.ToFlowflakeClock());

Testing time-dependent behavior

Inject a custom TimeProvider (e.g., FakeTimeProvider from Microsoft.Extensions.Time.Testing) to control time in tests:

var epoch = new DateTime(2023, 02, 15, 0, 0, 0, DateTimeKind.Utc);
var fake = new FakeTimeProvider(new DateTimeOffset(epoch).AddSeconds(10));
var options = Options.Create(new FlowflakeIdOptions
{
    InstanceId = 1,
    UseUtcNow = true,
    FlowflakeClock = new FlowflakeClockOptions
    {
        Epoch = epoch,
        TimeSemantics = FlowflakeTimeSemantics.UtcNormalized
    }
});
var gen = new FlowflakeId(options, fake);

var id1 = await gen.GenerateAsync();
fake.Advance(TimeSpan.FromSeconds(1));
var id2 = await gen.GenerateAsync();

Decode-Only Scenarios

For services that only need to decode DateTime from existing IDs without generating new ones:

// Simplest - uses IConfiguration from DI
builder.Services.AddFlowflakeClock();

// Or with explicit configuration
builder.Services.AddFlowflakeClock(builder.Configuration);

// Usage
var clockOptions = app.Services.GetRequiredService<IOptions<FlowflakeClockOptions>>();
DateTime dateTime = id.GetDateTimeFromFlowflakeId(clockOptions.Value.ToFlowflakeClock());

Configuration for decode-only:

{
  "Amasia": {
    "Toolkit": {
      "FlowflakeId": {
        "FlowflakeClock": {
          "Epoch": "2023-02-15T00:00:00Z",
          "TimeSemantics": "UtcNormalized"
        }
      }
    }
  }
}

Time Semantics

  • UtcNormalized (default):
    • Epoch and input times are normalized to UTC before computing seconds.
    • Recommended for services (e.g., gRPC) and multi-zone deployments.
  • LegacyUnspecifiedEpoch:
    • Seconds are computed as a raw DateTime difference without UTC normalization.
    • Preserves historical behavior when Epoch was Unspecified and Generate() used DateTime.Now.
    • Use only to maintain exact continuity of existing IDs; prefer UtcNormalized for new systems.

Formatting (Codecs)

Formatting/parsing of IDs is decoupled from IFlowflakeId.

  • Use IIdCodec to plug different formats (default: NumericBase62Codec).
  • Extension helpers are available: FormatFlowflakeId and ParseFlowflakeId.
var codec = new NumericBase62Codec();
var id = await gen.GenerateAsync();

// Using the codec directly
string text = codec.Encode(id);
long back = codec.Decode(text);

// Or via extensions
string text2 = id.FormatFlowflakeId(codec);
long back2 = text2.ParseFlowflakeId(codec);

Backlog

  • gRPC client SDK: segment/lease RPC to allow local ID generation between refreshes.
  • Optional async-first IFlowflakeId variant for non-blocking remote calls.
  • Full-local mode bootstrap (allocate free instance id from a coordinator). Not planned yet.
Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on AmasiaLabs.Toolkit.FlowflakeId:

Package Downloads
AmasiaLabs.Toolkit.FlowflakeId.Extensions

DI and formatting extensions for Flowflake ID generator

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.4.21 117 9/4/2026
1.4.20 138 7/15/2026
1.4.19 119 7/6/2026
1.4.18 129 7/1/2026
1.4.17 156 6/18/2026
1.4.16 132 5/1/2026
1.4.15 134 4/22/2026
1.4.14 132 4/21/2026
1.4.13 126 4/21/2026