"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Implementing Snowflake Id generator

Implementing Snowflake Id generator

Published on 2024-11-07
Browse:282

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
Release Statement This article is reproduced at: https://dev.to/alquama/implementing-snowflake-id-generator-3f8k?1 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3