Optional:
Shorten:
POST /shorten
{
"long_url": url,
"alias": optional str,
"time_to_expire": optional time
}
-> short_code: str
Redirect:
GET /{short_code}
-> 302 temporary redirection to long url
-> 404 not found
In the database we store the short/long url.
Each entry would be:
We have a client, a server, and a database in our design. The client send the requests to the servers, the server processes the requests, query the database and update the entries.
Shortening:
Redirect:
Cache can be a Redis, with LRU eviction policy.
Database can be any database.
Server can be horizontally scaled, we need a load balancer
For the alias generation, we can use base62 encoding with a counter. Counter can be a Redis counter, so no race conditions. Every time a new alias is requested, we INCR counter, and encode it into base62. We need log_62(10^8)~=7chars. Quite short. Note that we may need to contact the database to see if it is already in the database given that we support custom alias.
Handling invalid URLs: we can use is-url to check, or a simple regex based solution.
Handling invalid custom alias: too long, collide with existing
Explain any trade offs you have made and why you made certain tech choices...
What are some future improvements you would make? How would you mitigate the failure scenario(s) you described above?