Requirements


Functional Requirements:


  • User should get a short URL corresponding to the requested long URL
  • User should be able to redirect to original URL upon hitting short URL on browser search bar



Non-Functional Requirements:


  • System latency should be as low as possible like less than or equal to 200 ms
  • System should be highly available and partition tolerant as consistency is already maintained. One short URL is mapped to one long URL so no duplicates exist
  • Every short URL must be unique


API Design


Endpoint-1

POST /api/v1/urls

Request Body: {

"long_url": "https://domain/endpoint",

"expires_at": expire_time

}

status code: 201 Created

Response: {

"long_url": "https://domain/endpoint",

"short_url": "https://shorturl/",

"expires_at": expire_time

}



Endpoint-2

POST /api/v1/endpoints

Request Body: {

"short_url": "https://shorturl/"

}

status code: 302 Found

Response: {

"long_url": "https://domain/endpoint"

}


High-Level Design


Client: Regular device - mobile, website, webapp


Load Balancers: To redirect user request to highest available server when multiple app servers are present. This helps in managing large traffic


Application Servers: For generating and mapping a short URL corresponding to a long URL, storing the mapping on database, returning short URL as response during creation and redirecting to original long URL when the short URL request comes


Caching layer: As read requests will be more than write requests (writes on DB are only done once during creation phase of short URL), we need to store frequently queried URLs in memory stores like redis, avoiding DB access when same request comes again. This is necessary because database operations are heavy and we should avoid latency where ever possible



Database layer: Contains mapping of short URL to long URL along with user_id, expiry time, time of creation etc. Will have short_url column as primary key to ensure unique short URLs for each long URL. Anytime a read operation happens store the result in cache to avoid re querying DB for same URL




Detailed Component Design


The system should have low latency equal to or less than like 200ms for faster response times


The system should be able to scale to 100M DAU at least


As the system is read heavy so high availability should be ensured


The system should scale well with horizontal scaling where multiple server nodes should be present for growing DAU along with partition tolerance