Designing a URL shortener is a classic system design problem that helps you understand how real-world scalable systems are built. It involves concepts like unique ID generation, database design, caching, and handling large traffic efficiently.
1. Basic Explanation
A URL shortener converts a long URL into a shorter, manageable link.
Example:
Long URL
https://www.example.com/blog/how-to-learn-java-step-by-step
Short URL
https://short.ly/abc123
When a user clicks the short URL, the system redirects them to the original long URL.
The core idea is simple:
Generate a unique short code for every long URL
Store the mapping between short code and long URL
Redirect users when the short URL is accessed
2. How It Works (Step-by-Step Flow)
Step 1: User submits a long URL
Step 2: System generates a unique identifier (ID or hash)
Step 3: Convert that ID into a short code (using Base62 encoding)
Step 4: Store mapping in database (shortCode → longURL)
Step 5: Return short URL to user
When user accesses the short URL:
Extract short code
Look up database
Redirect to original URL using HTTP 301/302
3. Detailed Design Components
3.1 API Design
POST /shorten
Input: Long URL
Output: Short URL
GET /{shortCode}
Output: Redirect to original URL
3.2 Database Design
A simple table structure:
Table: URL_MAPPING
id (Primary Key)
short_code (Unique)
long_url
created_at
expiry_date (optional)
Indexes:
Index on short_code for fast lookup
3.3 Short Code Generation Strategies
Option 1: Auto-increment ID + Base62 Encoding
Generate ID (1, 2, 3...)
Convert to Base62 (a-z, A-Z, 0-9)
Example:
1 → a
2 → b
61 → Z
62 → ba
Advantages:
Simple
Predictable
Disadvantages:
Sequential (not secure)
Option 2: Hashing (MD5/SHA)
Hash the long URL
Take first few characters
Problem:
Collisions possible
Option 3: Random String
Generate random 6–8 character string
Problem:
Need collision handling
3.4 Redirect Mechanism
When user hits short URL:
Extract short code
Query database
If found → return HTTP redirect
If not → return 404
3.5 Caching (Important for Performance)
Use caching (like Redis):
Store frequently accessed URLs
Reduce database load
Flow:
Check cache first
If not found → query DB → update cache
3.6 Scalability Considerations
To handle millions of users:
Use load balancers
Use distributed databases
Use caching layers
Use CDN for faster access
3.7 High Availability
Replicate database
Use failover systems
Avoid single point of failure
4. Real-Time Example
User input:
https://ashokitech.com/core-java-online-training/
System process:
ID generated: 125
Base62 encoded: cb
Short URL: short.ly/cb
Database:
cb → original URL
5. Java Implementation (Simple Version)
5.1 Base62 Encoder
class Base62 {
private static final String CHARSET = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
public String encode(int num) {
StringBuilder sb = new StringBuilder();
while (num > 0) {
sb.append(CHARSET.charAt(num % 62));
num /= 62;
}
return sb.reverse().toString();
}
}
5.2 URL Service
import java.util.HashMap;
import java.util.Map;
class URLShortenerService {
private Map<String, String> db = new HashMap<>();
private int counter = 1;
private Base62 encoder = new Base62();
public String shortenURL(String longURL) {
String shortCode = encoder.encode(counter++);
db.put(shortCode, longURL);
return "http://short.ly/" + shortCode;
}
public String getOriginalURL(String shortCode) {
return db.get(shortCode);
}
}
5.3 Main Class
public class Main {
public static void main(String[] args) {
URLShortenerService service = new URLShortenerService();
String shortUrl = service.shortenURL("https://ashokitech.com");
System.out.println("Short URL: " + shortUrl);
String code = shortUrl.substring(shortUrl.lastIndexOf("/") + 1);
System.out.println("Original URL: " + service.getOriginalURL(code));
}
}
6. Advanced Real-World Improvements
In production systems like bit.ly:
Use distributed ID generators (Snowflake)
Store data in NoSQL databases
Add analytics (click tracking)
Support custom aliases
Add expiration for links
Implement rate limiting
7. Java Learning Roadmap
To build systems like this, follow a structured path:
8. Learn Core Java
If you want to master concepts like system design, backend development, and real-time projects.
Final Thoughts
A URL shortener may look simple, but it involves many important backend concepts:
Database design
Scalability
Caching
System reliability
.png)
No comments:
Post a Comment