Design a URL Shortener Service
What is the URL Shortener Problem?
Section titled “What is the URL Shortener Problem?”Design a URL Shortener Service that converts long URLs into shorter, unique URLs. The system should handle URL shortening, redirection, expiration management, and analytics tracking. The design should support different ID generation strategies, flexible storage mechanisms, and decoupled analytics tracking using appropriate design patterns.
In this problem, you’ll design a service that maps long, cumbersome URLs to short, easy-to-share strings, while tracking usage analytics and enforcing expiration dates.
Problem Overview
Section titled “Problem Overview”Design a service that creates unique aliases for long URLs and redirects users to the original destination with minimal latency.
Core Requirements
Section titled “Core Requirements”Functional Requirements:
- URL Shortening: Convert long URLs into unique, shortened codes.
- Redirection: Redirect from a short code back to the original long URL.
- Custom Aliases: Support user-provided short codes while ensuring global uniqueness.
- Expiration: Set optional expiration times; expired codes should no longer redirect.
- Analytics: Track metrics like click counts and access timestamps for each URL.
- Analytics Querying: Allow retrieving analytics data for specific shortened URLs.
- Swappable Components: Switch ID generators and storage strategies at runtime.
Non-Functional Requirements:
- Thread Safety: Handle concurrent shortening and redirection requests without data corruption.
- Scalability: Design for high availability and low redirection latency.
- Extensibility: Use OOD principles to add new generators or storage sinks easily.
- Uniqueness: ID generation must strictly minimize collisions and ensure unique codes.
- Reliability: Background tasks like expiration cleanup shouldn’t block core operations.
What’s Expected?
Section titled “What’s Expected?”1. System Architecture
Section titled “1. System Architecture”The service coordinates between an IDGenerator, a StorageEngine, and an AnalyticsQueue.
2. Key Classes to Design
Section titled “2. Key Classes to Design”classDiagram
class ShortenerService {
-IDGenerator idGen
-StorageStrategy storage
+shorten(longUrl) String
+resolve(shortCode) String
}
class IDGenerator {
<<interface>>
+generate() String
}
class Base62Generator {
-long counter
+generate()
}
class URLMapping {
-String shortCode
-String longUrl
-DateTime expiry
-long clickCount
}
ShortenerService --> IDGenerator
ShortenerService --> URLMapping
ShortenerService o-- StorageStrategy
System Flow
Section titled “System Flow”URL Shortening Flow
Section titled “URL Shortening Flow”Key Design Challenges
Section titled “Key Design Challenges”1. Generating Unique IDs
Section titled “1. Generating Unique IDs”How do you ensure every ID is unique without checking the database every time?
Solution: Use Base62 Encoding on a global counter. Base62 (a-z, A-Z, 0-9) allows for $62^7$ (over 3.5 trillion) unique combinations with just 7 characters. Using a counter ensures $100%$ uniqueness and $O(1)$ ID generation.
2. Handling URL Expiration
Section titled “2. Handling URL Expiration”Scanning millions of URLs to find expired ones can kill database performance.
Solution: Lazy Deletion + Background Cleanup. When a user accesses a URL, check the expiry timestamp. If it’s passed, delete it and return a 404. Additionally, run a low-priority background job once a day to prune old entries.
3. High-Speed Redirection
Section titled “3. High-Speed Redirection”Database lookups for every click are slow.
Solution: Use an LRU Cache. Since 20% of URLs typically generate 80% of traffic, keeping the most popular mappings in memory (Redis or local Cache) dramatically reduces latency.
What You’ll Learn
Section titled “What You’ll Learn”By solving this problem, you’ll master:
- ✅ Encoding & Hashing - Shrinking data while maintaining uniqueness.
- ✅ Scalable ID Generation - Building distributed counter systems.
- ✅ Cache Strategies - Optimizing read-heavy workloads.
- ✅ Persistence Logic - Swapping between SQL and NoSQL storage.
View Complete Solution & Practice
Section titled “View Complete Solution & Practice”Ready to see the full implementation? Open the interactive playground to access:
- 🎯 Step-by-step guidance through the 8-step LLD approach
- 📊 Interactive UML builder to visualize your design
- 💻 Complete Code Solutions in Python, Java, C++, TypeScript, JavaScript, C#
- 🤖 AI-powered review of your design and code
Related Problems
Section titled “Related Problems”After mastering URL Shortener, try these similar problems:
- Hash Map - Designing the underlying storage mechanics.
- Rate Limiter - Protecting your shortener from abuse.
- Cache Manager - Advanced eviction policies for high-traffic URLs.