Create a short url for the input long url. Returns a short url that can redirect to this long url.
Return the mapped longUrl by giving a short url
cache database 1: used for storing a pool of available short url:
{
string shortUrl,
Int offSet
}
cache database 2: used for speeding up the read of frequently access shortUrl
{
string shortUrl, key
String longUrl, value
}
main database short to long mapping table: (noSQL)
{
string shortUrl, key
value {
string longUrl,
TimeStamp creatdAt,
TimeStamp updatedAt,
String userId
}
}
client -> load balancer -> cache -> database
server
client -> load balancer -> cache -> database
server
Client sent a request to create a short url based on long url.
Request reaches load balancer to go to the nearest and available cache db(Redis).
Cache db stored a bunch of available short urls ready to be used and they are generated by a shortUrl generator service with a cron job.
When a create request comes in, we will pick up a short url in the short url pool in cache db, write the mapping to another table (short to long mapping table )
And then we can have a cron job periodicolly flush the new url mapping to its main database
Note: since redis is single threaded, we resolve the potential collision naturally. Since when a write request comes in, the request will be queued to access redisc cache to retrieve the pre-generated short url, thus no collision will happen in this design.
Client sent a request to read a long url based on a short url.
Request reaches load balancer to go to the nearest and available cache db(Redis).
If cache hit, returns the long url.
If cache miss, query the main db and add to cache.
Dig deeper into 2-3 components and explain in detail how they work. For example, how well does each component scale? Any relevant algorithm or data structure you like to use for a component? Also you could draw a diagram using the diagramming tool to enhance your design...
Explain any trade offs you have made and why you made certain tech choices...
Try to discuss as many failure scenarios/bottlenecks as possible.
What are some future improvements you would make? How would you mitigate the failure scenario(s) you described above?