Snowflake ID Definition

A Snowflake ID is a 64-bit unique identifier that contains a timestamp, making it both unique and time-ordered. Originally developed by Twitter, Snowflake IDs are now used by Discord, Instagram, and many other platforms for generating distributed unique identifiers at scale.

Last updated

Key Characteristics

Snowflake ID Structure

41 bits

What Is a Snowflake ID?

A Snowflake ID is a 64-bit integer used as a unique identifier in distributed systems. Originally developed by Twitter in 2010, the Snowflake algorithm generates IDs that are unique across thousands of servers without any central coordination. The result is a single integer that encodes the creation time, the machine that generated it, and a sequence number — all packed into one compact value.

Anatomy of a Snowflake ID

Every Snowflake ID is a 64-bit signed integer broken into three fields:

  • Timestamp (41 bits) — milliseconds elapsed since a custom epoch
  • Machine ID (10 bits) — identifies the server or worker that created the ID (5 bits datacenter + 5 bits worker)
  • Sequence (12 bits) — counter that increments for each ID generated within the same millisecond on the same machine
| 1 bit (sign) | 41 bits (timestamp) | 10 bits (machine) | 12 bits (sequence) |

This layout allows up to 4,096 unique IDs per millisecond per machine, across 1,024 machines, for roughly 69 years before the timestamp overflows.

Example: Decoding a Twitter Snowflake ID

Take the Snowflake ID 1529877576591609861. Here is how to decode it manually:

Snowflake ID:  1529877576591609861
Binary:        0001010100111011001110001001001001001001001001001001001000000101

Step 1 — Extract timestamp (right-shift by 22 bits):
  1529877576591609861 >> 22 = 364,876,800,000 ms (approx)
  Add Twitter epoch (1288834974657 ms):
  364,876,800,000 + 1,288,834,974,657 = 1,653,711,774,657 ms since Unix epoch
  → 2022-05-28T00:02:54.657Z

Step 2 — Extract machine ID (bits 12–21):
  (1529877576591609861 >> 12) & 0x3FF = 0 (datacenter 0, worker 0)

Step 3 — Extract sequence (bits 0–11):
  1529877576591609861 & 0xFFF = 5

So this ID was created on May 28, 2022 at 00:02:54 UTC, by machine 0, and was the 5th ID generated that millisecond.

Example: Generating a Snowflake ID in Python

import time

EPOCH = 1288834974657  # Twitter epoch in ms

def generate_snowflake(datacenter_id=0, worker_id=0):
    ts = int(time.time() * 1000) - EPOCH
    sequence = 0  # simplified; real impl tracks per-ms counter
    snowflake = (ts << 22) | (datacenter_id << 17) | (worker_id << 12) | sequence
    return snowflake

print(generate_snowflake(datacenter_id=1, worker_id=3))
# Example output: 1653711774657536003

Example: Decoding a Snowflake ID in JavaScript

const TWITTER_EPOCH = 1288834974657n;

function decodeSnowflake(id) {
  const bigId = BigInt(id);
  const timestamp = (bigId >> 22n) + TWITTER_EPOCH;
  const machineId = (bigId >> 12n) & 0x3FFn;
  const sequence  = bigId & 0xFFFn;

  return {
    createdAt: new Date(Number(timestamp)).toISOString(),
    machineId: Number(machineId),
    sequence:  Number(sequence),
  };
}

console.log(decodeSnowflake("1529877576591609861"));
// { createdAt: '2022-05-28T00:02:54.657Z', machineId: 0, sequence: 5 }

Example: Discord Snowflake IDs

Discord uses the same Snowflake structure but with a different epoch: January 1, 2015 (1420070400000 ms).

const DISCORD_EPOCH = 1420070400000n;

function decodeDiscordSnowflake(id) {
  const bigId = BigInt(id);
  const timestamp = (bigId >> 22n) + DISCORD_EPOCH;
  return new Date(Number(timestamp)).toISOString();
}

// Discord message ID example
console.log(decodeDiscordSnowflake("175928847299117063"));
// → '2016-04-30T11:18:25.796Z'

Example: Instagram Snowflake IDs

Instagram uses a similar scheme with their own epoch and shard ID instead of machine ID. The timestamp occupies the upper bits, making IDs sortable by creation time just like Twitter's implementation.

-- Instagram-style decode in PostgreSQL
SELECT
  to_timestamp(((media_id::bigint >> 23) + 1314220021721) / 1000.0) AS created_at,
  (media_id::bigint >> 13) & 1023 AS shard_id,
  media_id::bigint & 8191 AS sequence
FROM media
LIMIT 5;

Snowflake ID vs UUID Comparison

  • Snowflake IDs are 64-bit integers; UUIDs are 128-bit values
  • Snowflake IDs sort chronologically; UUID v4 is random and does not sort by time
  • Snowflake IDs require distributed coordination for machine IDs; UUIDs are fully independent
  • Snowflake IDs are more compact and index-friendly in databases
  • UUIDs reveal no information about creation time; Snowflake IDs do

Example: Custom Epoch Calculation

If you are building your own Snowflake system, choose an epoch close to your launch date to maximize the 69-year lifespan of the timestamp field.

// Custom epoch: January 1, 2024
const MY_EPOCH = new Date("2024-01-01T00:00:00Z").getTime(); // 1704067200000

function makeSnowflake(workerId, sequence) {
  const ts = BigInt(Date.now() - MY_EPOCH);
  return (ts << 22n) | (BigInt(workerId) << 12n) | BigInt(sequence);
}

console.log(makeSnowflake(1, 0).toString());

Why Snowflake IDs Matter for Database Performance

Because Snowflake IDs increase monotonically over time, new rows always insert at the end of a B-tree index. This eliminates the page splits and index fragmentation that occur with random UUIDs, keeping insert performance consistent even as tables grow to billions of rows. For high-write workloads, this difference is significant.

Common Platforms Using Snowflake IDs

  • Twitter / X — tweets, users, direct messages
  • Discord — messages, servers, channels, users
  • Instagram — media posts and comments
  • LinkedIn — posts and connections
  • Mastodon — statuses and accounts
Timestamp
Milliseconds since epoch
10 bits
Worker/Datacenter
0-1023 machines
12 bits
Sequence
0-4095 per ms

Who Uses Snowflake IDs?

🐦

Twitter

Tweet IDs, user IDs, DM IDs. Epoch: Nov 4, 2010

💬

Discord

User IDs, message IDs, server IDs. Epoch: Jan 1, 2015

📸

Instagram

Media IDs (encoded as shortcodes). Epoch: Nov 4, 2010

How Snowflake IDs Work

Step 1: Get Current Timestamp

Calculate milliseconds since the custom epoch (e.g., Nov 4, 2010 for Twitter)

Step 2: Add Worker/Datacenter ID

Identify which server generated the ID (0-1023)

Step 3: Add Sequence Number

Increment counter for IDs generated in the same millisecond (0-4095)

Step 4: Combine into 64-bit Integer

Bit-shift and combine all parts into final ID

Code Example

JavaScript
// Decode Snowflake ID
function decodeSnowflake(id, epoch) {
  const snowflake = BigInt(id);
  const timestamp = Number((snowflake >> 22n) + BigInt(epoch));
  const workerId = Number((snowflake >> 17n) & 0x1Fn);
  const datacenterId = Number((snowflake >> 12n) & 0x1Fn);
  const sequence = Number(snowflake & 0xFFFn);
  
  return {
    timestamp: new Date(timestamp),
    workerId,
    datacenterId,
    sequence
  };
}

// Twitter example
const TWITTER_EPOCH = 1288834974657;
const result = decodeSnowflake('1382350606417817604', TWITTER_EPOCH);
console.log(result);
// {
//   timestamp: 2021-04-14T15:30:06.657Z,
//   workerId: 0,
//   datacenterId: 0,
//   sequence: 0
// }

Advantages of Snowflake IDs

  • No coordination needed: Each server generates IDs independently
  • Time-ordered: Sort by ID = sort by creation time
  • Compact: 64-bit vs 128-bit UUIDs saves storage
  • Embedded metadata: Timestamp included in the ID
  • High throughput: Millions of IDs per second
  • No database queries: Generate IDs without hitting DB

Disadvantages

  • Clock dependency: Requires synchronized clocks across servers
  • Limited lifespan: 41-bit timestamp lasts ~69 years from epoch
  • Not cryptographically secure: Predictable and sequential
  • Reveals timing: Anyone can extract creation timestamp

Common Use Cases

Social Media

Posts, comments, messages, user accounts

Databases

Primary keys for distributed databases

APIs

Request IDs, transaction IDs, event IDs

Microservices

Distributed tracing, correlation IDs

Frequently Asked Questions

Q: Are Snowflake IDs globally unique?

A: Yes, when properly configured with unique worker IDs across all servers.

Q: Can I use Snowflake IDs in my application?

A: Absolutely! The algorithm is open and widely used. Just choose your own epoch.

Q: What happens after 69 years?

A: The 41-bit timestamp overflows. You'd need to migrate to a new epoch or ID system.

Frequently Asked Questions

A Snowflake ID is a 64-bit unique identifier that contains a timestamp, worker ID, and sequence number. Created by Twitter, it's used for distributed ID generation across multiple servers without coordination. Examples: Twitter IDs, Discord IDs, Instagram media IDs.

It's called Snowflake because like snowflakes in nature, each ID is unique. The name also reflects that IDs are generated independently across distributed systems without central coordination, yet remain globally unique.

Twitter (tweets, users), Discord (messages, users, servers), Instagram (posts, reels), and many other platforms use Snowflake IDs for distributed unique ID generation.