SnowflakeGenerator 2.0.0

dotnet add package SnowflakeGenerator --version 2.0.0                
NuGet\Install-Package SnowflakeGenerator -Version 2.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="SnowflakeGenerator" Version="2.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SnowflakeGenerator --version 2.0.0                
#r "nuget: SnowflakeGenerator, 2.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.
// Install SnowflakeGenerator as a Cake Addin
#addin nuget:?package=SnowflakeGenerator&version=2.0.0

// Install SnowflakeGenerator as a Cake Tool
#tool nuget:?package=SnowflakeGenerator&version=2.0.0                

SnowflakeGenerator

SnowflakeGenerator is a unique ID generator based on Twitter's Snowflake. It generates 64-bit, time-ordered, unique IDs based on the Snowflake algorithm. It is written in C# and is compatible with .NET Standard 2.0.

Available as a NuGet Package.

The default bit assignment for this Snowflake implementation is:

41 bits for the TimeStamp value
10 bits for the MachineID value
12 bits for Sequence value

This provides by default:

  • A time range of approximately 69 years (2^41 milliseconds).
  • Use of 1024 (2^10) unique MachineIDs across a distributed deployment.
  • Generation for a maximum of 4096 (2^12) IDs per ms from a single Snowflake instance.

If you require a higher generation rate or large range of MachineID's these values can be customised by the Settings used to initialize a Snowflake instance.

NOTE: 41 bits is always reserved for the TimeStamp value. Therefore, the sum of the MachineIDBitLength and SequenceBitLength cannot exceed 22. With the SequenceBitLength being at least equal to 1.

Features

  • Generates 64-bit unique IDs, which are time-ordered.
  • Customizable machine ID and custom epoch settings.
  • Thread-safe ID generation.
  • High-performance and low-latency.

Installation

To install the SnowflakeGenerator library, you can use the following command in the Package Manager Console:

Install-Package SnowflakeGenerator

Alternatively, you can use the .NET CLI:

dotnet add package SnowflakeGenerator

Usage

First, import the SnowflakeGenerator namespace:

using SnowflakeGenerator;

Create a new instance of the Snowflake class with optional settings:

Settings settings = new Settings 
{ 
    MachineID = 1,
    CustomEpoch = new DateTimeOffset(2020, 1, 1, 0, 0, 0, TimeSpan.Zero) 
};
    
Snowflake snowflake = new Snowflake(settings);

Generate a new unique ID:

long id = snowflake.NextID();

Decode ID content:

var (timeStamp, machineId, sequence) = sonyflake.DecodeID(uniqueId);

NextID() will throw a TimestampOverflowException once it reaches the limit of the TimeStamp.

Settings

The Settings class allows you to customize the Snowflake instance:

  • MachineID: A unique identifier for the machine or instance generating the IDs.
    • Defaults to: 0
  • CustomEpoch: A custom epoch or reference point for the timestamp portion of the generated ID.
    • Defaults to: 1970-01-01T00:00:00.000Z
  • MachineIDBitLength: Sets the number of bits allocated to the Machine ID part of the Snowflake ID.
    • Defaults to: 10
  • SequenceBitLength: Sets the number of bits allocated to the Sequence part of the Snowflake ID.
    • Defaults to: 12

License

This project is licensed under the MIT License.

Contributing

Contributions are welcome. Please submit a pull request or create an issue to discuss your proposed changes.

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. 
.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.
  • .NETStandard 2.0

    • No dependencies.

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
2.0.0 4,651 8/8/2023
1.1.1 165 4/23/2023
1.1.0 162 4/23/2023
1.0.0 173 4/23/2023