All Projects → RobThree → Idgen

RobThree / Idgen

Licence: mit
Twitter Snowflake-alike ID generator for .Net

Projects that are alternatives of or similar to Idgen

snowworker
Website snow! It'll settle on anything that has a .rooftop class.
Stars: ✭ 25 (-95.7%)
Mutual labels:  snowflake
IdGenerator
💎迄今为止最全面的分布式主键ID生成器。 💎优化的雪花算法(SnowFlake)——雪花漂移算法,在缩短ID长度的同时,具备极高瞬时并发处理能力(50W/0.1s)。 💎原生支持 C#/Java/Go/Rust/C/SQL 等多语言,且提供 PHP 扩展及 Python、Node.js、Ruby 多线程安全调用动态库(FFI)。💎支持容器环境自动扩容(自动注册 WorkerId ),单机或分布式唯一IdGenerator。💎顶尖优化,超强效能。
Stars: ✭ 548 (-5.68%)
Mutual labels:  snowflake
Snowflake
Twitter的分布式自增ID雪花算法snowflake (Java版)
Stars: ✭ 336 (-42.17%)
Mutual labels:  snowflake
carto-spatial-extension
A set of UDFs and Procedures to extend BigQuery, Snowflake, Redshift and Postgres with Spatial Analytics capabilities
Stars: ✭ 131 (-77.45%)
Mutual labels:  snowflake
pre-commit-dbt
🎣 List of `pre-commit` hooks to ensure the quality of your `dbt` projects.
Stars: ✭ 149 (-74.35%)
Mutual labels:  snowflake
dbd
dbd is a database prototyping tool that enables data analysts and engineers to quickly load and transform data in SQL databases.
Stars: ✭ 30 (-94.84%)
Mutual labels:  snowflake
sfquickstarts
Follow along with our tutorials to get you up and running with the Snowflake Data Cloud.
Stars: ✭ 83 (-85.71%)
Mutual labels:  snowflake
Neural
高并发、高可用的微服务架构中的分布式治理利刃,提供了分布式限流、服务降级、熔断器、重试器等容错特性,并提供了SPI、过滤器和JWT等功能。此外还提供了很多小的黑科技(如:IP黑白名单、UUID加强版、Snowflake和大并发时间戳获取等)。
Stars: ✭ 373 (-35.8%)
Mutual labels:  snowflake
monoton
Highly scalable, single/multi node, sortable, predictable and incremental unique id generator with zero allocation magic on the sequential generation
Stars: ✭ 21 (-96.39%)
Mutual labels:  snowflake
Distributedid
基于twitter的雪花算法(SnowFlake)来产生分布式ID,支持SDK、HTTP方式接入
Stars: ✭ 273 (-53.01%)
Mutual labels:  snowflake
snowflake-starter
A _simple_ starter template for Snowflake Cloud Data Platform
Stars: ✭ 31 (-94.66%)
Mutual labels:  snowflake
tsid-creator
A Java library for generating Time Sortable Identifiers (TSID).
Stars: ✭ 16 (-97.25%)
Mutual labels:  snowflake
tellery
Tellery lets you build metrics using SQL and bring them to your team. As easy as using a document. As powerful as a data modeling tool.
Stars: ✭ 219 (-62.31%)
Mutual labels:  snowflake
Butterfly
分布式ID生成器框架:超高性能的发号器框架。通过引入多种新的方案,彻底解决雪花算法的时间回拨等问题,并将雪花算法原生QPS提高最少十几~二十倍
Stars: ✭ 111 (-80.9%)
Mutual labels:  snowflake
Mindsdb
Predictive AI layer for existing databases.
Stars: ✭ 4,199 (+622.72%)
Mutual labels:  snowflake
astro
Astro allows rapid and clean development of {Extract, Load, Transform} workflows using Python and SQL, powered by Apache Airflow.
Stars: ✭ 79 (-86.4%)
Mutual labels:  snowflake
growthbook
Open Source Feature Flagging and A/B Testing Platform
Stars: ✭ 2,342 (+303.1%)
Mutual labels:  snowflake
Snowflake
❄️ A React-Native Android iOS Starter App/ BoilerPlate / Example with Redux, RN Router, & Jest with the Snowflake Hapi Server running locally or on RedHat OpenShift for the backend, or a Parse Server running locally or remotely on Heroku
Stars: ✭ 4,576 (+687.61%)
Mutual labels:  snowflake
Sqlpad
Web-based SQL editor run in your own private cloud. Supports MySQL, Postgres, SQL Server, Vertica, Crate, ClickHouse, Trino, Presto, SAP HANA, Cassandra, Snowflake, BigQuery, SQLite, and more with ODBC
Stars: ✭ 4,113 (+607.92%)
Mutual labels:  snowflake
Naza
🍀 Go basic library. || Go语言基础库
Stars: ✭ 253 (-56.45%)
Mutual labels:  snowflake

Logo IdGen

Build status NuGet version

Twitter Snowflake-alike ID generator for .Net. Available as Nuget package

Why

In certain situations you need a low-latency, distributed, uncoordinated, (roughly) time ordered, compact and highly available Id generation system. This project was inspired by Twitter's Snowflake project which has been retired. Note that this project was inspired by Snowflake but is not an exact implementation. This library provides a basis for Id generation; it does not provide a service for handing out these Id's nor does it provide generator-id ('worker-id') coordination.

How it works

IdGen generates, like Snowflake, 64 bit Id's. The Sign Bit is unused since this can cause incorrect ordering on some systems that cannot use unsigned types and/or make it hard to get correct ordering. So, in effect, IdGen generates 63 bit Id's. An Id consists of 3 parts:

  • Timestamp
  • Generator-id
  • Sequence

An Id generated with a Default IdStructure is structured as follows:

Id structure

However, using the IdStructure class you can tune the structure of the created Id's to your own needs; you can use 45 bits for the timestamp, 2 bits for the generator-id and 16 bits for the sequence if you prefer. As long as all 3 parts (timestamp, generator and sequence) add up to 63 bits you're good to go!

The timestamp-part of the Id should speak for itself; by default this is incremented every millisecond and represents the number of milliseconds since a certain epoch. However, IdGen relies on an ITimeSource which uses a 'tick' that can be defined to be anything; be it a millisecond (default), a second or even a day or nanosecond (hardware support etc. permitting). By default IdGen uses 2015-01-01 0:00:00Z as epoch, but you can specify a custom epoch too.

The generator-id-part of the Id is the part that you 'configure'; it could correspond to a host, thread, datacenter or continent: it's up to you. However, the generator-id should be unique in the system: if you have several hosts or threads generating Id's, each host or thread should have it's own generator-id. This could be based on the hostname, a config-file value or even be retrieved from an coordinating service. Remember: a generator-id should be unique within the entire system to avoid collisions!

The sequence-part is simply a value that is incremented each time a new Id is generated within the same tick (again, by default, a millisecond but can be anything); it is reset every time the tick changes.

System Clock Dependency

We recommend you use NTP to keep your system clock accurate. IdGen protects from non-monotonic clocks, i.e. clocks that run backwards. The DefaultTimeSource relies on a 64bit monotonic, increasing only, system counter. However, we still recommend you use NTP to keep your system clock accurate; this will prevent duplicate Id's between system restarts for example.

The DefaultTimeSource relies on a Stopwatch for calculating the 'ticks' but you can implement your own time source by simply implementing the ITimeSource interface.

Getting started

Install the Nuget package and write the following code:

using IdGen;
using System.Linq;

class Program
{
    static void Main(string[] args)
    {
        var generator = new IdGenerator(0);
        var id = generator.CreateId();
    }
}

Voila. You have created your first Id! Want to create 100 Id's? Instead of:

var id = generator.CreateId();

write:

var id = generator.Take(100);

This is because the IdGenerator() implements IEnumerable providing you with a never-ending stream of Id's (so you might want to be careful doing a .Select(...) or Count() on it!).

The above example creates a default IdGenerator with the GeneratorId (or: 'Worker Id') set to 0 and using a DefaultTimeSource. If you're using multiple generators (across machines or in separate threads or...) you'll want to make sure each generator is assigned it's own unique Id. One way of doing this is by simply storing a value in your configuration file for example, another way may involve a service handing out GeneratorId's to machines/threads. IdGen does not provide a solution for this since each project or setup may have different requirements or infrastructure to provide these generator-id's.

The below sample is a bit more complicated; we set a custom epoch, define our own id-structure for generated Id's and then display some information about the setup:

using IdGen;
using System;

class Program
{
    static void Main(string[] args)
    {
        // Let's say we take april 1st 2020 as our epoch
        var epoch = new DateTime(2020, 4, 1, 0, 0, 0, DateTimeKind.Utc);
            
        // Create an ID with 45 bits for timestamp, 2 for generator-id 
        // and 16 for sequence
        var structure = new IdStructure(45, 2, 16);
            
        // Prepare options
        var options = new IdGeneratorOptions(structure, new DefaultTimeSource(epoch));
            
        // Create an IdGenerator with it's generator-id set to 0, our custom epoch 
        // and id-structure
        var generator = new IdGenerator(0, options);

        // Let's ask the id-structure how many generators we could instantiate 
        // in this setup (2 bits)
        Console.WriteLine("Max. generators       : {0}", structure.MaxGenerators);

        // Let's ask the id-structure how many sequential Id's we could generate 
        // in a single ms in this setup (16 bits)
        Console.WriteLine("Id's/ms per generator : {0}", structure.MaxSequenceIds);

        // Let's calculate the number of Id's we could generate, per ms, should we use
        // the maximum number of generators
        Console.WriteLine("Id's/ms total         : {0}", structure.MaxGenerators * structure.MaxSequenceIds);


        // Let's ask the id-structure configuration for how long we could generate Id's before
        // we experience a 'wraparound' of the timestamp
        Console.WriteLine("Wraparound interval   : {0}", structure.WraparoundInterval(generator.Options.TimeSource));

        // And finally: let's ask the id-structure when this wraparound will happen
        // (we'll have to tell it the generator's epoch)
        Console.WriteLine("Wraparound date       : {0}", structure.WraparoundDate(generator.Options.TimeSource.Epoch, generator.Options.TimeSource).ToString("O"));
    }
}

Output:

Max. generators       : 4
Id's/ms per generator : 65536
Id's/ms total         : 262144
Wraparound interval   : 407226.12:41:28.8320000 (about 1114 years)
Wraparound date       : 3135-03-14T12:41:28.8320000+00:00

IdGen also provides an ITimeSouce interface; this can be handy for unittesting purposes or if you want to provide a time-source for the timestamp part of your Id's that is not based on the system time. For unittesting we use our own MockTimeSource.

<configuration>
  <configSections>
    <section name="idGenSection" type="IdGen.Configuration.IdGeneratorsSection, IdGen.Configuration" />
  </configSections>

  <idGenSection>
    <idGenerators>
      <idGenerator name="foo" id="123"  epoch="2016-01-02T12:34:56" timestampBits="39" generatorIdBits="11" sequenceBits="13" tickDuration="0:00:00.001" />
      <idGenerator name="bar" id="987"  epoch="2016-02-01 01:23:45" timestampBits="20" generatorIdBits="21" sequenceBits="22" />
      <idGenerator name="baz" id="2047" epoch="2016-02-29"          timestampBits="21" generatorIdBits="21" sequenceBits="21" sequenceOverflowStrategy="SpinWait" />
    </idGenerators>
  </idGenSection>

</configuration>

The attributes (name, id, epoch, timestampBits, generatorIdBits and sequenceBits) are required. The tickDuration is optional and defaults to the default tickduration from a DefaultTimeSource. The sequenceOverflowStrategy is optional too and defaults to Throw. Valid DateTime notations for the epoch are:

  • yyyy-MM-ddTHH:mm:ss
  • yyyy-MM-dd HH:mm:ss
  • yyyy-MM-dd

You can get the IdGenerator from the config using the following code:

var generator = AppConfigFactory.GetFromConfig("foo");

Upgrading from 2.x to 3.x

Upgrading from 2.x to 3.x should be pretty straightforward. The following things have changed:

  • Most of the constructor overloads for the IdGenerator have been replaced with a single constructor which accepts IdGeneratorOptions that contains the ITimeSource, IdStructure and SequenceOverflowStrategy
  • The MaskConfig class is now more appropriately named IdStructure since it describes the structure of the generated ID's.
  • The UseSpinWait property has moved to the IdGeneratorOptions and is now an enum of type SequenceOverflowStrategy instead of a boolean value. Note that this property has also been renamed in the config file (from useSpinWait to sequenceOverflowStrategy) and is no longer a boolean but requires one of the values from SequenceOverflowStrategy.
  • ID is now Id (only used as return value by the FromId() method)

The generated 2.x ID's are still compatible with 3.x ID's. This release is mostly better and more consistent naming of objects.


Icon made by Freepik from www.flaticon.com is licensed by CC 3.0.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].