@Universe Tech
Increase by k
Hard to scale with multiple data centers
IDs do not go up with time across multiple servers
It does not scale well when a server is added or removed
Centralized auto_increment
64 bit long
Datacenter IDs and machine IDs are chosen at the startup time
A machine can support a maximum of 4096 new IDs per ms
Time clock synchronization is crucial
Cost on getting time from system is higher than random generation
UIDGenerator by BaiDu
Leaf-Segment by 美团
Leaf-Snowflake by 美团
Seqsvr by Tencent
URL Shortening
URL Redirecting
High Availability
Scalability
Fault Tolerance
Write operation per second: 100 million / 24 /3600 = 1160
Read operation: Assuming ratio of read operation to write operation is 10:1, read operation per second: 1160 * 10 = 11,600
Assuming the URL shortener service will run for 10 years, this means we must support 100 million * 365 * 10 = 365 billion records
Assume average URL length is 100.
Storage requirement over 10 years: 365 billion * 100 bytes * 10 years = 365 TB
URL shortening. To create a new short URL, a client sends a POST request, which contains one parameter: the original long URL. The API looks like this:
POST api/v1/data/shorten
request parameter: {longUrl: longURLString}
return shortURL
URL redirecting. To redirect a short URL to the corresponding long URL, a client sends a GET request. The API looks like this:
GET api/v1/shortUrl
Return longURL for HTTP redirection
Each hashValue can be mapped back to the longURL
Hash function
hash + collision resolution
base 62 conversion
Hash value length
[0-9, a-z, A-Z], containing 10 + 26 + 26 = 62 possible characters
62^n ≥ 365 billion
When n = 7, 62 ^ n = ~3.5 trillion
https://tinyurl.com/2TX
Unique ID generator returns ID: 2009215674938
Convert the ID to shortURL using the base 62 conversion. ID (2009215674938) is converted to "zn9edcu"