Companies/Twitter/

Snowflake — Unique ID Generator

Lesson overview

Snowflake — Unique ID Generator

How Twitter's Snowflake generates unique, sortable 64-bit IDs at scale — breaking down the epoch, datacenter, machine, and sequence bits.

How to Make Unique Id in a Distributed System

transacrtion

2026183863091020

2026183863091111

2026183863091134

2026183863091172

Kartik - Lorel

David - philips

EMma - jason

Charlie - ABC

(Indian - UPI 9000 TPS/sec)

Functional Requirements:

1. generate unique in distributed system 2. id should fit in -> 64 bits 3. should be sortable --> [new Id > old id] -- help in sorting

Non-Functional Requirements

1. Low latency 2. High Availability 3. Security 3. 1 lakh id/sec

Mindset: 1. Unique doesnot mean random 2. Unique does not mean consecutive

| Solution | Basic idea | Main advantage | Main problem | | ----------------------- | -------------------------------------- | --------------------------------- | --------------------------------------------------- | | Database auto-increment | One database increments a counter | Very simple | Central bottleneck and difficult with shards | | Per-shard counters | Each database has its own counter | Scales better | Can produce duplicate IDs without a shard prefix | |Rangeallocation+zookeeper| Give each server a block of IDs | Few central calls | Range management and weak time ordering | | Central ticket service | One service returns the next ID | Easy global uniqueness | Network dependency and central bottleneck | | Timestamp only | Use current time | Simple and time ordered | Many objects can be created in the same millisecond | | UUID v4 | Generate a random 128-bit value | No central service | Larger than 64 bits and not naturally time ordered | | Snowflake | Combine time, machine and sequence | Fast, compact and roughly ordered | Requires good clocks and unique worker IDs |

General Table

Solution - 1: Database Auto-Increment

roll_number table

CREATE TABLE roll_number ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100) );

Advantages Easy to understand Small IDs Naturally ordered Works well for small systems Problems Every write depends on one database or counter. It can become a bottleneck. It can become a single point of failure. It becomes difficult when data is split across many databases.

Solution - 2: Database Auto-Increment Per Shard

Final ID: 1-1 1-2 1-3 1-4 . . . . 1-999 1-1099 . . . . . . 2-1001 . . . . 2-2002

Loading Snowflake — Unique ID Generator