Core Desgin Decisions:
Back of the Envelope Estimation:
Create Short URL: Post/shorten
Redirect URL: Get/{shortcode}
Analytics URL: Get/stats/{shortcode}
Cleanup URL: Post/cleanup
ID generation URL: Get/id
URL Shorten Service - generates short code
Redirect Service - Handles lookups and redirects
Analytics Service - async processing
ID generator Service - URL Shorten Service uses this to generate the unique id
Cleanup Service - cleansup the old expired urls from DB
DATABASE choice: key-value DB(DynamoDB), if we choose SQL, scaling writes is hard, sharding is complex.
Schema: table: URLMapping
shortcode (pk)
longurl
createdAt
expiryAt
userId(optional)
Redis : key-shortCode value-longURL. TTL can be defined and LRU eviction policy.
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 [timestamp | machineId | sequence]
. Convert to Base62:
. Why is it best: No Collisions, Deterministic, Compact
. Problem: Distributed ID generation
. use pre allocated ID ranges per server.
. use zookeeper for coordination of id generator services
Scalability & Reliability:
Horizontal scaling:
Bottlenecks and solutions:
Security Measures:
Backup & Disaster Recovery:
Logging & Monitoring: