❄️ What is a Snowflake ID?
A Snowflake ID is a unique, time-ordered identifier (usually a 64-bit integer) generated in a distributed system without collisions.
- First designed by Twitter (Snowflake service) to generate unique IDs for tweets at scale.
- Later adopted by many companies (e.g., Discord IDs, database keys, order IDs in e-commerce, etc.).
🧩 Structure of a Snowflake ID (64 bits)
A typical Twitter Snowflake looks like this:
| 1 bit (unused) | 41 bits timestamp | 10 bits machine ID | 12 bits sequence number |
- 1 bit (sign bit) → always 0 (positive number).
-
41 bits timestamp → milliseconds since a custom epoch (e.g., Nov 2010 for Twitter).
- Can last ~69 years.
-
10 bits machine ID → identifies the datacenter + machine.
- Allows up to 1024 machines.
-
12 bits sequence number → per-machine counter.
- Allows 4096 unique IDs per millisecond, per machine.
👉 This guarantees uniqueness across the whole distributed system.
🎯 Why Snowflake IDs are useful
- Globally unique across data centers.
- Time-ordered (newer IDs are larger numbers → useful for sorting).
- No central bottleneck like database auto-increment.
- Scalable → can generate millions of IDs per second.
🔍 Example
Let’s say a ride-hailing system wants to generate ride IDs without depending on a central DB:
- Every server (in different regions) can generate Snowflake IDs.
- The IDs will be unique (thanks to machine ID + sequence).
- They’ll also be roughly ordered by creation time → useful for analytics.
Example (Discord Snowflake):
419430400000000000
From this, you can decode → timestamp, server ID, sequence.
🚀 Alternatives
- UUID (Universally Unique ID) → Random-based, but not time-ordered, longer (128-bit).
- Database auto-increment → Simple, but not distributed (central bottleneck).
- Snowflake → Best for distributed, high-scale, time-ordered IDs.
👉 So in short: A Snowflake ID is a distributed, time-ordered unique identifier (64-bit integer), invented by Twitter, widely used in modern systems for scalable ID generation.