List functional requirements for the system (Ask the chat bot for hints if stuck.)...
List non-functional requirements for the system...
200 requests per second (17 million in a day) in creating short urls
20,000 (1.7 billion) requests per second to access shortened URLs.
Estimate the scale of the system you are going to design...
Throughput Requirements
Average writes per second (WPS): 200
Peak writes per second : 2000
Average redirects per second (WPS): 20000
Peak redirects second : 200000
Storage Estimation
original URL: 100 chars
short URL: 8 chars
creation date: 8 bytes (timestamp)
expiration date: 8 bytes (timestamp)
click count: 4 bytes (integer)
total storage per URL: 128 bytes
total urls per year = 17,000,000 X 365 = 6,205,000,000
total storage per year = 128 * 6,205,000,000 = 794,000,000,000 bytes ~ 795GB
Bandwidth Estimation
One request is 500 bytes, so 500 * 17,000,000 = 8.5GB per day
peak bandwidth = 500 * 200000 = 100 MB/s
Caching Estimation
Read heavy system requires caching to reduce the latency of read requests
Consider the 80-20 rule where 20% of the hot URLs generate 80% of the traffic
so, 17,000,000 * 0.2 * 128bytes = 435MB
20000 * 0.10 = 2000 rps
Infrastructure Sizing
API Servers - 7-8 instances behind a load balancer that can handle 200 -300 RPS
Database - Distributed database with 10 to 20 nodes to handle storage, high read/write throughput
Cache Layer - distributed cache with 3 - 4 nodesdepending on the load cache
Define what APIs are expected from the system...
URL Shortening
POST REQUEST /api/v1/shorten
Sample Request
{
"long_url": "https://example.com/needs/shortening" ,
"user_id": "user1",
"custom_option": "https://shortened",
"expiry_date": "12/4/2025"
}
Sample Response
{
"long_url": "https://example.com/needs/shortening" ,
"short_url": "https://shortened/12",
"date_created": "12/4/2024"
"expiry_date": "12/4/2025"
}
URL Redirection API
GET REQUEST /{short_url_key}
response: status 301
location: https://example.com/needs/shortening
Defining the system data model early on will clarify how data will flow among different components of the system. Also you could draw an ER diagram using the diagramming tool to enhance your design...
DB Schema
Two tables, one for User Information, another one for URL Mappings
url_mapping
long_url: varchar
short_url: varchar
date_created: Timestamp
expiry_date: Timestamp
user_id: int
click_count: int
user
user_id: int
name: varchar
email: varchar
password: varchar
You should identify enough components that are needed to solve the actual problem from end to end. Also remember to draw a block diagram using the diagramming tool to augment your design. If you are unfamiliar with the tool, you can simply describe your design to the chat bot and ask it to generate a starter diagram for you to modify...
Explain how the request flows from end to end in your high level design. Also you could draw a sequence diagram using the diagramming tool to enhance your explanation...
Client - send a request to the load balance
load balance - distributes traffic to the available application servers
application servers - handle the incoming requests for shortening and redirecting
URL Generation service - shortens the original urls, stores them and manages link expiration
redirection service - redirects the user to original url
database - stores mapping for short and long urls
analytics service - fetches the data for number of clicks of the short url
cache - stores frequently accessed urls mappings for faster retrieval
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...
URL Generation Service
Approach 1 : Hashing and encoding
Work flow
Issues that can be encountered with this approach
Resolution
Re-Hashing - If a collision is detected, the service should rehash the original url with a different seed or additional bits from the original hash to generate a unique short url
increamental suffix - append an incremental suffix e.g. -1 or -2 to the short url until a unique key is found
Approach 2: Unique ID Generation
Two considerations
Custom short urls
Link Expiration
Redirection Service
Analytics Service
Explain any trade offs you have made and why you made certain tech choices...
Try to discuss as many failure scenarios/bottlenecks as possible.
SCALABILITY
API layer - Deploy the API Layer across multiple instances behind a load balancer to distribute incoming requests evenly.
Sharding - if using the auto-increment ID as shard key, first shard can be 1 to I million while second shard can be 1m to 2 million. Only issues is that it can create a hotspot due to uneven load distribution when one shard is bigger than another.
Hash-based sharding - apply hash function to the shard key to determine which shard the data should go to. e.g. hash the short url then modulo with the number of shards to determine the shard it should go to.
The issue with this is when scaling out, redistributing data can be challenging and can be resolved with consistent hashing techniques.
AVAILABILITY
Replication - use database replication to ensure data is available even if some nodes fail.
Failover - use failover mechanisms for the API and data store layers to switch to a backup incase of server failures.
geo-distributed deployment - deploy the servers across multiple geographical locations to reduce latency and improve availability.
Handling edge cases
Expired URLS - Return meaningful response e.g. HTTP410 instead of redirecting.
Non-existent URLS - Return 404
urls conflicts - implement collision detection during URL creation to prevent conflicts.
SECURITY
Rate limiting -implement rate limiting at the API Layer to prevent abuse.
Input validation - Ensure there is no malicious content.
https - communication between clients and services should be encrypted.
monitoring and alerts - check for unusual activity patterns and trigger attacks for potential DDOS attacks or misuse
What are some future improvements you would make? How would you mitigate the failure scenario(s) you described above?