Building TinyURL: A Deep Dive into URL Shortener System Design ππ #SystemDesign #URLShortener #TechInsights
π¨ Problem Statement
Assuming we would like to design a URL shortener service, various key considerations must be addressed. To start with, we need to decide on our serviceβs functionality.
π― Key Considerations
1οΈβ£ Customization π οΈ
- Will users be able to customize their shortened URLs?
- Alternatively, will the service generate a random string of characters for each URL?
2οΈβ£ Scalability π & Storage πΎ
- How will we store the data associated with each shortened URL?
- Should we use a database? If so, which type would be best suited for our needs?
- How often will the application access data?
- Do we need to support real-time lookups?
3οΈβ£ User Interaction π₯οΈπ±
- How will users interact with the service?
- Web interface π
- API β‘
Depending on our choice, we need to design our system accordingly.
π Summary
Designing a URL shortener service requires careful planning π and consideration π€ of various factors. By taking the time to define our requirements upfront, we can create a system that is both scalable βοΈ and secure π.
π₯ Check out this video below for a detailed discussion on how you should approach this system design interview question:
Introduction
Implementing a tiny URL π using the minimalistic approach β¨ shortens a long original URL into a shorter and manageable URL.
π What is a URL Shortener?
This project concerns the system design ποΈ of a URL shortener service like bit.ly, goo.gl, or tinyurl.com. Weβll examine the various design aspects π and trade-offs βοΈ in creating such a service.
A URL shortener service is a web service π that provides shortened URLs for long URLs. The shortened URL is:
β
Easier to remember π§
β
More convenient to share π’
β
Capable of tracking π clicks and analytics
π Existing URL Shortener Services
Many popular URL shortener services exist, such as:
πΉ bit.ly
πΉ goo.gl
πΉ tinyurl.com
π In this project, we will design our own URL shortener service! ποΈπ
Letβs dive in! π
π Clarifications
To ensure a well-structured URL shortener service, we need to clarify certain requirements:
π Privacy & Access Control
- Can users create private URLs π that only specific users can access?
π Analytics & Tracking
- How many times has a short URL been used? π
- What were the user locations π, devices π±, and referring websites π?
- How will we store these statistics in our system? πΎ
π Customization Options
- Should users be able to choose their own custom URL instead of a randomly generated one? βοΈ
π Tracking Features
- The service should be able to track the number of clicks on each shortened URL π.
π Storage & Access
- We will use a database to store all URL mappings and metadata πΎ.
- Data must be accessed in real-time β³ to ensure fast redirection.
π‘ Security Measures
- Rate limiting β³ to prevent abuse and excessive API requests π«.
- Implement filtering mechanisms to prevent malicious URL submissions β οΈ.
βοΈ User Interaction & API Support
- Users will interact with the service primarily via an API β‘.
- Consider providing a web interface π for easier usability.
This ensures our service is scalable, secure, and user-friendly π.
π User Stories / Functional Requirements
π Minimum Viable Product (MVP)
1οΈβ£ Basic URL Shortening π
As a user, I can enter a URL to shorten in the system and receive a unique shortened URL.
Details: The system should ensure that no two URLs map to the same shortened URL.
2οΈβ£ Redirection to Original URL π
As a user, I can enter a shortened URL into my browser and be redirected to the original URL instantly.
π Optional Features
3οΈβ£ Custom Shortened URLs βοΈ
As a user, I can enter a URL along with a custom string so that the system returns a shortened URL with my chosen suffix (if available).
π‘ Other Functional User Stories
πΉ Easy Sharing π’
As a user, I want to shorten a URL quickly so that I can share it easily.
πΉ Click Tracking & Analytics π
As a user, I want to view statistics about how many people clicked my shortened URL to track its effectiveness.
πΉ Custom Shortcodes π
As a user, I want to choose my own shortcode for a URL so that I can remember it more easily.
πΉ Security & Protection π‘
As a user, I want the service to be secure so that my data is not compromised.
πΉ High Speed & Performance β‘
As a user, I want the service to be fast so that I can shorten and share my URLs instantly.
πΉ Free Service π°
As a user, I want the service to be free to use so that I donβt have to pay anything.
βοΈ Non-Functional Requirements (NFRs)
For our URL Shortener Service π, we must ensure it is fast, reliable, and resilient πͺ. Below are the key non-functional requirements (NFRs):
β‘ 1. Low Latency
- URL redirection π should happen in real-time β³ with minimal latency.
- Users should experience instantaneous redirects when accessing a shortened URL.
π 2. High Availability & Fault Tolerance
- The system should be highly available (99.99% uptime) β²οΈ.
- It should remain operational even if a server crashes π¨ due to:
- Power outages β‘
- Hard disk failures πΎ
- Network issues π
- A load balancer ποΈ should distribute traffic across multiple instances for seamless failover.
π 3. Strong Data Consistency
- Once a URL is shortened, it must always resolve consistently β .
- Given a shortened URL, the system should always return the same original URL, regardless of when it is requested.
π‘ 4. No Downtime / Resilient Architecture
- The service should never go down. π«π
- Even if some machines fail, the system should continue functioning without interruption.
- The only failure scenario would be if all servers go down at once, which is highly unlikely.
Ensuring these NFRs will make the URL Shortener Service scalable, reliable, and efficient ππ.
π Capacity Estimation & Constraints
To ensure our URL Shortener Service π is scalable and efficient, we need to estimate CPU, storage, bandwidth, and caching requirements.
π₯οΈ CPU (Compute) Requirements
- The system is read-heavy π, with a 100:1 ratio between reads and writes.
- Retention period: URLs will be stored for 5 years.
- Traffic estimates:
- 500 million new URLs π shortened per month π .
- Redirections per month = 100 * 500M = 50 billion π.
- Queries per second (QPS) =
π 500 million / (30 days * 24 hours * 3600 sec) = 20,000 requests/sec
πΎ Storage Requirements
- Total URLs stored =
500M * 5 years * 12 months
= 30 billion URLs π¦. - Storage per URL (including metadata) = 1 KB.
- Total storage required =
30 billion * 1 KB
= 30 TB.
π Network Bandwidth Estimates
Write Requests (Input):
200 * 1 KB = **200 KB/s**
Read Requests (Output):
20,000 * 1 KB/sec = **20 MB/s**
β‘ Cache Estimation (80β20 Rule)
π 80% of traffic is generated by 20% of the URLs.
- We use LRU (Least Recently Used) caching ποΈ with a Linked HashMap to store frequently accessed URLs.
Total cacheable requests per day:
20,000 * 3600 sec * 24 hours = 1.7 billion
π
Cache storage required:
1.7B * 20% * 1 KB = **340 GB**
π‘ Since many requests may be duplicates, actual cache usage might be lower.
π API Design for URL Shortener
To enable efficient URL shortening and redirection, we provide a simple RESTful API π οΈ.
π 1. Create a Short URL
π Endpoint:
POST /api/link HTTP/1.1
Host: urlshrtnr.taruntelang.repl.co
Content-Type: application/x-www-form-urlencoded
π Request Body:
{
"link": "https://www.linkedin.com/in/taruntelang/"
}
π Response:
{
"shortURL": "6U8Ahx9"
}
π‘ Example:
π 2. Retrieve the Original URL
π Endpoint:
GET /6U8Ahx9 HTTP/1.1
Host: urlshrtnr.taruntelang.repl.co
π Response:
{
"originalURL": "https://www.linkedin.com/in/taruntelang/"
}
π‘ Behavior:
- The user enters
https://urlshrtnr.taruntelang.repl.co/6U8Ahx9
in the browser. - The system redirects to the original URL instantly.
π― Why Short Links?
β
Easy to share via messages, tweets, or print media ππ’.
β
Reduces clutter in social media posts π.
β
Tracks analytics for marketing performance π.
β
Hides affiliate links for better engagement π.
This API-first approach ensures a simple yet powerful URL shortener service ππ.
βοΈ System Components
1οΈβ£ Application Server π₯οΈ
- Handles URL shortening, redirections, analytics tracking, and API requests.
- Should be stateless, allowing easy horizontal scaling.
2οΈβ£ Cache (e.g., Redis / Memcached) β‘
- Stores frequently accessed URLs to reduce database load.
- Uses LRU (Least Recently Used) eviction policy.
3οΈβ£ Database (NoSQL β e.g., Cassandra, DynamoDB, MongoDB) πΎ
- Scalable and optimized for high read traffic.
- Stores URL mappings and user-related data.
- Replication & Sharding for improved performance.
π Telemetry & Analytics
Tracking user interactions helps improve performance and understand user behavior. The system should capture:
π User Tracking Data:
- π Country of the visitor
- π Date & Time of access
- π Referrer URL (where the user clicked from)
- π Browser type (Chrome, Firefox, etc.)
- π± Platform (Mobile, Desktop, Tablet, etc.)
π Use Case:
- π Marketers can analyze link engagement to optimize campaigns.
- π‘ Helps detect bot traffic or suspicious activity.
- π Provides insights for scaling infrastructure based on traffic patterns.
π₯ Health Check System
πΉ Ensures that the service remains available and performs optimally π‘.
β Periodically checks system health by monitoring:
- Database connectivity π
- Cache performance β‘
- API response times β³
- Server resource utilization (CPU, RAM) π
π Actionable Insights:
- Auto-restart failing services π
- Alert engineers if an anomaly is detected π¨
- Log failures for debugging π
β Load Balancer
A Load Balancer π is critical for distributing traffic across multiple servers to:
β
Prevent server overload π¦
β
Ensure high availability (If one server fails, traffic is routed to another)
β
Optimize response times β‘
π Common Load Balancer Options:
- Software-based: Nginx, HAProxy π₯οΈ
- Cloud-based: AWS ELB, Google Cloud Load Balancer βοΈ
πΉ How It Works:
- User requests a shortened URL π
- The load balancer directs the request to an available server π―
- The server fetches the original URL from the database or cache ποΈ
- The system redirects the user to the original website π
π Final Thoughts
By integrating Telemetry, Health Checks, and Load Balancing, our URL Shortener becomes:
β Reliable (Auto-failover, Monitoring)
β Scalable (Handles millions of requests per second)
β Optimized (Efficient resource utilization)
This modular approach ensures high performance while keeping the system resilient and user-friendly π₯π.
π Database Schema
1οΈβ£ URL Table π
UrlId: NUMBER <PRIMARY KEY>
OriginalURL: VARCHAR
CreationDate: DATETIME
ExpiryDate: DATETIME
UserId: NUMBER <FOREIGN KEY>
2οΈβ£ User Table π€
UserId: NUMBER <PRIMARY KEY>
Name: VARCHAR
Email: VARCHAR
CreationDate: DATETIME
LastLoginDate: DATETIME
π‘ Why NoSQL?
- We need to store billions of rows π.
- Easy horizontal scaling βοΈ.
- Minimal relationships between entities.
π’ Short URL Generation
- Shortened URL format: 7-character string using Base64 encoding π‘.
- Character set:
[a-z A-Z 0β9 - _]
(Base64 URL variant). - Ensures uniqueness by checking for collisions before storing.
π Key Takeaways
β NoSQL for scalability π.
β Cache for performance optimization β‘.
β Stateless architecture for easy scaling π.
β 7-character Base64 encoding ensures compact short URLs π.
This setup ensures a fast, scalable, and reliable URL shortening system π₯.