Implementing Snowflake Id generator
Alquama Salim
Posted on September 21, 2024
What is a Snowflake ID?
Snowflake IDs are used in distributed environments to generate collision-free, short, unique IDs. Unlike traditional methods, such as relying on a database for ID generation or using a long 128-bit UUID, Snowflake IDs use time and simple bitwise operations. This clever technique allows each microservice to generate unique IDs independently, without needing a central system to avoid collisions.
How to Generate One
Generating a Snowflake ID is like building a puzzle with three key pieces. Let’s break it down:
Take an n-bit long bit string:
First, we start with a bit string of lengthn
. This will hold all the necessary information to generate a unique ID.Divide it into three sections: i, j, and k:
The bit string is divided into three parts, such thati + j + k = n
.
i
- The Time Component:
The first part,i
, represents the current time. Choose a fixed start time (also known as the epoch), and the bits ofi
will be calculated by taking the current time in nanoseconds and subtracting the start time. This ensures that newer IDs are always larger than older ones.j
- The Machine ID:
The second part,j
, is the machine identifier. When your microservice starts, it is assigned a unique ID (machine ID), which becomes thej
part. This ensures that IDs generated by different machines won’t collide, even if they are created at the exact same moment.k
- The Sequence Number:
The last part,k
, is the sequence number. It acts like a counter that increments whenever multiple IDs are generated within the same time unit. This keeps IDs unique, even if they are generated in rapid succession.
-
Combine the pieces:
Once you have your
i
,j
, andk
values, concatenate them to form a single bit string. Then, convert this bit string to base 10 to get your final Snowflake ID.
A Quick Analogy
Think of a Snowflake ID as a special dish tag in a busy kitchen:
-
Time (
i
): This is like the clock ticking in the kitchen, ensuring dishes prepared later get larger numbers than those made earlier. -
Machine ID (
j
): Each chef (or microservice) has their own signature, making sure their dish tags don’t clash with anyone else’s. -
Sequence number (
k
): If a chef makes multiple dishes in one instant, they add a tiny increment to their tags, so each dish has a unique label.
Snowflake implemented in Go
Check this GitHub repo for a Go implementation of Snowflake ID generation
Sources
Posted on September 21, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.