Requirements


Functional Requirements:


  • Create a short URL for a given long URL.
  • Return the long URL associated with a given short URL.



Non-Functional Requirements:


  • Low latency
  • High consistency
  • Scalable to 1 lakh req / s


API Design

GET /urls/?url=

Status code: 301 redirect

{

"redirected_url":

}


POST

/urls/

{

"url": "long format url",

"short_code": ,

"expiry":

}


return {

"url":

}



High-Level Design

URL can be shotened using base 62 encoding

We can have bloom filter to check if the url exists or not for the given short code while creation


For expiry of urls, we can ave postgresql extension installed to mark field as inactive when its ttl is crossed



For availability perspective, load balancer or service deployed on ec2 instances are horizontally scalable, hence can be scaled


Now for unique constraint on short_code ,

we can have bloom filter or can use snowflake mechanism to create short codes across distributed system



POST can easily be handled viathat

and for GET, since there will be millions of data,we can have a redis layer to keep recently x hrs created data,

this would be highly accessible and fast ..


For POST, we can have bloom filter thing to easily check and create


Though we would be using postgresql extension to expire rows, we can set a CRON as well to expire


Also this shoudl be highly consistent instead of available to avoid conflict of short codes


Detailed Component Design

Deep dive into 2-3 key components. Explain how they work, how they scale, discuss tradeoffs, capacity, and any relevant algorithms or data structures.