README.md

erlflake
=====

Erlflake is an erlang unique distributed ID generator inspired
by [Twitter's Snowflake](https://blog.twitter.com/2010/announcing-snowflake)

```
 39 bits for the timestamp in 10 milliseconds unit
 13 bits for the woker node (zookeeper assigned by default)
 12 bits for the sequence number
```

Design - (64/96)bits global ID generation
------
All time are measure with 00:00:00 00 January 2020 as epoch. So the actual time in erlflake is the current unix time
minus erlflake defined epoch. That's if unix time is **T** ms, then erlflake time is **(T - E)** ms where **E** is
erlflake's epoch. As defined above, we'll run out of time when all the **40 bits** are **1 ones**. In other words, the
maximum time is **2^39 - 1 = 549755813887**. Let **M** represents the maximum unix time erlflake can handle before it
overflows.

In order to slow time exhaustion, we store the time bits in **1ms** unit. This means, for every **1ms** the sequence
number is reset. If a generation caused the sequence is to overflow, the erlflake generator process will sleep to make
sure the clock moves by 1ms then reset the sequence and eventually generate the id.

With all the above the information, the **M**, **E**  and the **time unit** are related mathematically as:

```
=> M - E = 2^39 - 1 
=> M - E  = 549755813887
=> M  = 549755813887 + E
=> M = 549755813887 + 1577836800000
=> M = 2127592613887 =  Tue Jun 02 2037 21:56:53
```

This means, erlflake will start regenerating the same id in almost after 16 years!!!. For every 1ms, erlflake can
serially generate **2^12 = 4096** unique IDs within the same worker.

Build
-----

    $ rebar3 shell