By given long URL, if already has shortening URL, return it, or else generate the unique shorten URL, and store in the db.
By given shorten URL, return the original long URL.
Optional: User-defined shorten URL, expiration time setting.
1000 QPS, 300 TPS
High availability.
Low latency. P95 1s
One pair = 200Bytes
200 * 300 * 60 * 60 *24 = 5.2GB / day * 365 = 2TB / year ~= 20TB / 10 years
POST createShorturl {origin_url(String) } : return short_url(String) HTTP_STATUS 200
GET getOriginurl {short_url(String)} : return origin_url(String)
HTTP_STATUS 302 (redirect)
GET getOriginurl {short_url(String)} : return error
HTTP_STATUS 404 (not found)
{
short_url: String (search index)
origin_url: String (search index)
}
Use NoSQL Database
Please see the high level diagram
When client initiates a request, it is first directed to the load balancer, load balancer will assign the request to different servers by round-robin to balance the load between URL servers.
If it's a createShorturl request:
If it's a getOriginurl request:
Use SHA 256 to hash the (given URL + fixed buffer string), and get the first 7 digits. If hashed value already exists, using the (hashed result + fixed buffer string) to hash again untill we get a unique value.
We can use both uppercase, lowercase and digits for the hash function, so we can generate (26+26+10)^7 = 3500B unique URLs.
Database SQL vs NoSQL - I choose NoSQL based on following reasons:
High scalability, can do horizontal scaling.
The data is just a pair of URLs, no stroing relation with other data.
Supports fast read and write.
More affordable for large volumn data.
Cache Stratergy:
Based on the pros and cons, I'd like to use cache aside combine with a suitable invalidation mechanism in this system.
Cache invalidation mechanism:
It kind of depends on the user habit, if the analytics shows that some of the short_urls are very often to request, we might want to apply Least-frequently use. If the analytics shows that user prefer to request recently generated urls, then we might want to apply LRU.
Single load balancer could be the single point of failure
To robust the system we could introduce more load balancers.