Implementing Snowflake Id generator

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:

  1. Take an n-bit long bit string:

    First, we start with a bit string of length n. This will hold all the necessary information to generate a unique ID.

  2. Divide it into three sections: i, j, and k:

    The bit string is divided into three parts, such that i + 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 of i 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 the j 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.

  1. Combine the pieces:
    Once you have your i, j, and k 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

  1. https://blog.x.com/engineering/en_us/a/2010/announcing-snowflake
  2. https://en.wikipedia.org/wiki/Snowflake_ID

Discover more from Coursity

Subscribe to get the latest posts sent to your email.

Leave a Comment

Your email address will not be published. Required fields are marked *

Discover more from Coursity

Subscribe now to keep reading and get access to the full archive.

Continue reading

Scroll to Top
Frequently asked questions.