Distributed Unique Id genrator
Applications
In most system where Requirements is to get unique id in a distributed env like Url Shortener, Bank, MySQL or any other DB.
Requirements
Functional
- each time a service is called it should return a unique ID across all node in the system
- Id should be fixed size, let's say 64 bit
- Id should contain a creation timestamp
Non-Functional
- Highly consistent, means no duplicate key generation
- Able to handle hight throughput
Solution
Single Machine(node)
For single machine we can use multiple approach like
- Auto increment id
- Auto increment id with creation timestamp of the system
Random Numbers(Multi node)
This appraoch can work whenw we have to select ids from long range.
Universally Unique Identifier(UUID)
550e8400-e29b-11d4-a716-446655440000
- This a uniqueid generated from a set of 2^128 ids available, It is 128 bits.
- Based on the version of it the generation of id is diff
- V1
- use current timestamp and device's MAC address
- V3
- use MD5 hash logic
- V4
- use total random numbers
- most used
- V5
- uses SHA-1
- V1
- Collision Probability
- Due to v4 version ensure 122 bits reserved for unique numbers so prob of collision is very low
- After generating trillions of urls the prob is still less than 0.5
- Advantages
- collision is very rare
- non predictable as use random numbers (specially used in sensitive transactions)
- Disadv
- as uses 128-bit so take extra storage space
- Can not use sorting
Twitter snowflake

-
It is one of the most popular algorithms to generate unique ids in a distributed system.
-
It is in half of the size of the UUID.
-
Some properties of the Snowflake id is
- Ids are unique and sortable based on timestamp
- Ids include timestamp
- is a 64-bit
- only numerical values
-
Structure
Bits Field Description 1 Sign bit Always 0(ensures positive number).41 Timestamp Milliseconds since a custom epoch (ensures order). 10 Machine ID Identifies the machine or data center (prevents conflict). 12 Sequence Increments per millisecond to prevent collisions (0-4095). -
Breakdown
- Timestamp (41 bits): Allows for 69 years of usage (2^41 milliseconds).
- Machine ID (10 bits): Supports up to 1024 machines generating IDs.
- Sequence (12 bits): Can generate 4096 unique IDs per millisecond per machine.