January 2nd 2022


Whilst creating a multitude of projects I have always noticed that I had to generate IDs for database entries. Wether it be users, blogposts, orders, or anything else, IDs are always important.

Now with my recent switch to using Scyllo (and thereby Scylla), which, to add to the conversation, does not generate IDs itself, I have started to look into solutions.

After stumbling upon the Snowflake ID, which is currently what is utalized by platforms such as Discord, Twitter, Instagram and Blizzard just to name a few, I realized we might have a winner.

After further testing and using it in a few projects (using flakeid) it appeared to work nicely.

Why Sunflake?

Whilst (ironically) on Discord (which uses Snowflakes thats why its funny), RobiotRobiot and I further discussed Snowflakes, Gen-Z Snowflakes, and how to get better typescript support. This is when Sunflake came into the picture.

RobiotRobiot and I then proceeded to re-implement Snowflake ID natively in typescript.


Installation of sunflake is relatively easy. Simply instruct your prefered package manager npm or yarn to add it and you should be good to go.

npm install sunflake

Or with yarn

yarn add sunflake

How to use

Using Sunflakes requires one setup function to be ran. In this function we can specify the MachineID aswell as the preferred Epoch timestamp to use.

Option Description
Machine ID Number used to differentiate one machine/process from another.
Epoch Custom timestamp at which the universe was created
or whatever steve decided, upto u.
import { generateSunflake } from 'sunflake';

const snowflake = generateSunflake({
    machineID: 1, // Machine id
    epoch: 1640995200000 // First of January 2022

Now when you are ready to generate a snowflake, run it as follows.

const randomId = await snowflake();

And voila! In addition if you wanted to generate it for a specific timestamp, you could do the following

const time = new Date('2022-01-02').getTime();
const randomId = await snowflake(time);

Why Snowflake ID

Snowflakes are awesome, they incorporate the current timestamp when generated (in the first 42 bits), then the next 10 bits are used to differentiate between instances, and the last 12 bits for a millisecond sequence counter.

This essentially mittigates that, if a multitude of processes tried spawning a lot of snowflakes at the same time, would ever produce overlapping IDs.

In addition to the values being time-based, this also allows IDs to be in cronological order if sorted/queried. This time-based ID spec allows for (in Discord’s case) easy fetching messages that were generated between two specific timestamps.


I hope that the above has peaked your interest for ID generation as much as it has for me. And next time you are working on IDs think about what you are using and choosing to go for.

If you would like to try out sunflake, check out these links:

Sunflake on Github

Sunflake on NPM