GoogleSystem DesignHard
Design a URL Shortener (like bit.ly)
System DesignURL ShortenerDatabaseScalability
Question
How would you design a URL shortener service like bit.ly? Discuss the system architecture, database design, and scalability considerations.
Answer
System Requirements:
- Shorten long URLs
- Redirect short URLs to original URLs
- Handle high traffic (millions of requests per day)
- Short URLs should be unique and short (6-8 characters)
System Design:
1. API Design:
- POST /api/v1/shorten - Create short URL
- GET /{shortCode} - Redirect to original URL
2. Database Schema:
ShortURLs:
- id (primary key)
- short_code (unique, indexed)
- original_url (indexed)
- created_at
- expires_at (optional)
- click_count
3. Encoding Algorithm:
- Base62 encoding (0-9, a-z, A-Z)
- Generate unique 6-8 character codes
- Use counter or hash-based approach
4. Caching:
- Use Redis to cache frequently accessed URLs
- Cache original_url by short_code
5. Scalability:
- Load balancers for traffic distribution
- Database sharding by short_code
- CDN for static assets
- Rate limiting to prevent abuse
Explanation
This is a classic system design problem that tests your understanding of:
- Database design and indexing
- Caching strategies
- Scalability and performance optimization
- API design
- Distributed systems concepts