Mubai.Snowflake 1.0.0

dotnet add package Mubai.Snowflake --version 1.0.0
                    
NuGet\Install-Package Mubai.Snowflake -Version 1.0.0
                    
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="Mubai.Snowflake" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Mubai.Snowflake" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Mubai.Snowflake" />
                    
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 Mubai.Snowflake --version 1.0.0
                    
#r "nuget: Mubai.Snowflake, 1.0.0"
                    
#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 Mubai.Snowflake@1.0.0
                    
#: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=Mubai.Snowflake&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Mubai.Snowflake&version=1.0.0
                    
Install as a Cake Tool

Mubai.Snowflake

English | 简体中文

Vision

Provide a Snowflake ID component for .NET that is:

  • Simple to adopt: one DI registration, business code only cares about IIdGenerator.NewId().
  • Safe in production: clear defensive behavior for misconfiguration, clock skew/rollback, and extreme bit layouts.
  • Able to evolve: covers everything from single-instance deployment to multi-node, multi-region, and cluster-level worker allocation.

In short, a Snowflake ID library suitable as ID infrastructure for distributed systems, with a smooth path toward more complex architectures.


Version Overview

Version Description
v1.0.0 (current) Flexible bit layout config, clock safety, thread safety, extreme-config fallback, decoding, DI support
v1.1.0 Strongly-typed ID wrapper, EF Core integration, sample projects
v1.2.0 Fix JS Number precision issues and unify ID representation between backend and frontend
v1.3.0 Optional high-performance implementation and observability, keeping the default simple and reliable
v2.0.0 Cluster-aware & multi-region solution

Current Version: v1.0.0 – Base Implementation (Shipped)

Core Capabilities

  • Flexible bit layout configuration

    • Default layout: 0 | 41-bit timestamp | 10-bit workerId | 12-bit sequence
    • Custom bit widths for each field, as long as the total is ≤ 63 bits
  • Clock safety

    • Configurable epoch (default: 2025-01-01)
    • Built-in detection for “future timestamps” to prevent misconfigured clocks
    • Clear exceptions when runtime clock anomalies are detected (rollback / moving too far ahead)
  • Thread safety

    • Stable generation under multi-threaded workloads
    • Proper handling of sequence overflow and clock rollback edge cases
  • Extreme-configuration fallback

    • When the timestamp field overflows, a composite counter kicks in so IDs remain usable in stress / extreme test scenarios
  • Decoding

    • Decode timestamp, workerId, and sequence from a Snowflake ID
    • Consistent configuration between encoding and decoding
  • Dependency injection support

    • Single-line registration in DI:

      services.AddSnowflakeIdGenerator(options =>
      {
          options.WorkerId = 1;
      });
      
    • Business code injects and uses IIdGenerator directly

Performance

  • Generation: single thread ~244 ns per ID, throughput ~4.1M IDs/s
  • Decoding: 1–4 ns per ID, throughput in the hundreds of millions to billions of IDs/s
  • Configuration & initialization: ~240 ns, effectively zero overhead
  • High-concurrency stability: 20 / 50 threads under high contention, stable for a 25-second run, no abnormal behavior
  • End-to-end pipeline: generate + decode still stays around ~244 ns per ID

For details, see the v1 benchmark results.

Explicit Non-Goals / Not Yet Implemented

  • No EF Core integration yet (no ValueConverter / strongly-typed ID mapping).
  • No JSON converters yet (no unified solution for string IDs / JS precision issues).
  • No lock-free “high performance” implementation yet; the default focuses on simplicity and safety, using lock.
  • No cluster-level worker ID allocation (e.g., Redis / DB lease).

All of these are planned in later versions.


v1.1 – Application Integration: EF Core & Strongly-Typed IDs

Goal: make application code more type-safe and avoid “raw long IDs everywhere”.

Planned work:

  • Strongly-typed ID wrapper

    • Define a SnowflakeId struct / record struct that holds an internal long value.
    • Provide explicit / implicit conversions to and from long / string.
  • EF Core integration

    • Provide a ValueConverter for SnowflakeId.
    • Offer extension methods to simplify configuration.
  • Sample project

    • A simple WebAPI / Minimal API sample that demonstrates:
      • Using SnowflakeId as a primary key.
      • Combining SnowflakeIdGenerator with EF Core.

v1.2 – JSON & Frontend Compatibility

Goal: solve JS Number precision issues and unify how IDs are represented end to end.

Planned work:

  • System.Text.Json converter

    • By default, serialize Snowflake IDs as string values in JSON.
  • Optional Newtonsoft.Json support

    • For projects still using Newtonsoft.Json, provide a matching JsonConverter.
  • Frontend docs & examples

    • Show how to handle IDs as string in TypeScript / frontend code to avoid casting to number.
    • Examples: displaying IDs on the UI, passing IDs in URLs, table pagination, etc.

v1.3 – Performance Optimization & Diagnostics

Goal: provide an optional high-performance implementation and better observability, while keeping the default implementation simple and robust.

Planned work:

  • Optional high-performance implementation

    • Introduce a lock-free version, e.g. HighPerfSnowflakeIdGenerator:
      • Use Interlocked / CAS / SpinWait.
      • Eliminate the global lock overhead as much as possible.
    • Choose implementation via configuration at registration time.
  • Benchmark project improvements

    • Use BenchmarkDotNet to compare:
      • v1.0 generator vs. the high-performance implementation.
      • Snowflake vs. Guid.NewGuid().
    • Present typical environment results in tables within the README.
  • Diagnostics & observability

    • Provide logging hooks or counters for:
      • Clock rollback events.
      • WaitUntilNextMillis invocations.
      • Usage of composite counter logic in extreme configurations.

v2.0 – Cluster Awareness & Multi-Region Strategy

Goal: support large-scale multi-region / multi-datacenter systems with worker ID allocation and high availability strategies.

Planned work:

  • Worker ID leases & centralized allocation

    • Obtain worker IDs from Redis / a database with time-bound leases.
    • Register worker IDs with a central service at startup; automatically reclaim leases when a process crashes.
  • Multi-region bit splitting

    • Split WorkerIdBits into:
      • High bits: RegionId
      • Low bits: MachineId
    • Provide configuration support for this split.
  • Availability strategies

    • Define clear behavior for:
      • Worker ID acquisition failures.
      • Lease conflicts.
      • Central service downtime.
    • Potential strategies:
      • Fail-fast with clear diagnostics.
      • Optional fallback to single-node mode.
      • Emit warning logs.
  • Docs & guidance

    • Provide deployment guidelines for multi-region setups:
      • How to plan epochs, bit allocation, and worker ID distribution.

Backlog (Unscheduled Ideas)

Ideas that have come up but are not yet scheduled for any of the versions above.

  • Monitoring dashboard example

    • Provide a sample based on Prometheus / OpenTelemetry showing:
      • IDs generated per second.
      • Clock rollback counts.
      • Composite counter usage under extreme configurations.
  • Security & obfuscation

    • Provide an optional obfuscation scheme for IDs exposed externally.
  • Language interoperability

    • In documentation, recommend compatible Snowflake implementations in other languages
      to make it easier to integrate in polyglot stacks.
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 239 11/10/2025

Initial release of Mubai.Snowflake, a high-performance Snowflake ID generator for .NET.