Design a high-scale URL shortener
MicrosoftSoftware Engineer · OnsiteLast updated
This problem tests your ability to design a distributed, highly available URL shortening service that manages massive read-heavy traffic and ensures low-latency redirection.
Problem Statement
Design a URL shortening service similar to bit.ly or tinyurl.com. The system needs to support millions of requests per day with a high read-to-write ratio.
Functional Requirements
- URL Shortening: Given a long URL, the service should generate a unique, shorter, and random-looking alias (e.g.,
bit.ly/3xYz). - URL Redirection: When a user visits the shortened URL, the service should redirect them to the original long URL.
- Custom Aliases: Allow users to pick a custom alias if it is not already taken.
- Expiration: Links should expire after a certain duration (optional, but requested by the client).
Non-Functional Requirements
- Low Latency: Redirection must be extremely fast, ideally under 100ms.
- High Availability: The service must be highly available; if one component fails, the rest of the system should continue to operate.
- Scale: The system should support 100 million new URLs per month with a 100:1 read-to-write ratio.
Deliverables
Provide a high-level architecture diagram or description covering:
- API design (request/response format).
- Data storage strategy (NoSQL vs. SQL, schema design, and partitioning).
- Mechanism for URL shortening (e.g., base62 conversion, hashing, or ID generation).
- Strategy for handling high read volume (caching, load balancing).
- Handling of concurrency and collisions.
Hints
Hint 1
Consider how to map a unique integer ID to a short URL string using Base62 encoding (A-Z, a-z, 0-9).
Hint 2
Evaluate the trade-offs between using a SQL database for relational consistency versus a NoSQL Key-Value store like Cassandra or DynamoDB for horizontal scale.
Hint 3
Think about using a cache layer like Redis to store the most frequently accessed URLs to minimize database read pressure.
Follow-up questions
- How would you handle the requirement to generate analytics, such as the number of clicks per link or the geographic location of the users?
- If the database is partitioned by hash, how do you handle hotkeys (e.g., a viral link that receives millions of hits in a short window)?
- What changes would you make if you needed to ensure that the shortened URLs are impossible to guess or brute-force?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.