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:
- Availability: The System should be highly available
- Scalable: System should be highly Scalable for 100M users and be able to tolerate request spikes.
- Latency: The Read and write request should have very low latency.
- Consistency:
- Two uses trying to access the long URL for a given short URL should return the same long URL.
- Two uses trying to create a short URL for a same long URL should get the same URL.
- A User created a short URL for a long URL and again trying to create a short URL again for a the same long URL should get the same short URL.
- Durability
- Once short URL created, needs to ensure that the data never lost
- Reliability
- The System should behave correctly and deliver the functional requirement even in case of failure, request spikes and other outage.
API Design
- Create Short URL
- HTTP Method: POST
- Endpoint: /url-shortener
- Request: accept the Long URL as a request body
- Response: returns the short URL
- Function: get(user_id, longUrl)
- Get the Long URL
- HTTP Method: GET
- Endpoint: /url-extractor
- Request: get the short URL as a request parameter
- Response: returns the corresponding long URL.
- Function: String getLongUrl(String shortUrl)
High-Level Design
As the application is read and write heavy. create a separate applications to read/get the long URL for a short URL and separate application servers to generate and write the Short URL for a given long URL.
the URL will be persisted in the database and at the same it it will be added in the cache.
Once the read request receives from the user first lookup into cache if found then return to the user else lookp to database and write into cache before retuning to the user.
cache evict policy will be lease recently used. the URLs which used less number of times will be removed from the cache.
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.