Core Desgin Decisions:
Back of the Envelope Estimation:
Create Short URL: post/shorten
req = json
{
"longurl' : "......",
"customAlias":"...."(optional),
"expiry":"...."
}
Redirect URL: Get/{shortcode}
Analytics URL: Get/stats/{shortcode}
URL Shorten Service - generates short code
Redirect Service - Handles lookups and redirects
Analytics Service - async processing
DATABASE choise: key-value(DynamoDB), if we choose SQL, scaling writes is hard, sharding complexity.
Schema: table: URLMapping
shortcode (pk)
longurl
createdAt
expiryAt
userId(optional)
Data flow:
URL Creation flow:
Redirect flow:
Short URL Generation:
Approach1: Random string
. Generate a random 6-8 chars
. problem: collisions -> retry loops
Approach2: Hashing(MD5)
. Hash long URL
. Take first N chars
. Problem: collision risk
Approach3: Counter + Base62
. Generate a unique ID eveytime
. Convert to Base62:
. Why is it best: No Collisions, Deterministic, Compact
. Problem: Distributed ID generation
. use pre allocated ID ranges per server.
Scalability & Reliability:
Bottlenecks