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.
Key Characteristics
- 64-bit integer: Compact and efficient storage
- Time-ordered: IDs are naturally sorted by creation time
- Distributed: Generated across multiple servers without coordination
- Unique: Guaranteed no collisions across all servers
- High performance: 4+ million IDs per second per worker
Snowflake ID Structure
Who Uses Snowflake IDs?
Tweet IDs, user IDs, DM IDs. Epoch: Nov 4, 2010
Discord
User IDs, message IDs, server IDs. Epoch: Jan 1, 2015
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
// 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.