Design A URL Shortener
UberSoftware Engineer · OnsiteLast updated
This question tests your ability to design a high-scale, distributed URL shortening service that handles billions of requests while ensuring low latency and data durability.
System Design: URL Shortener
Design a URL shortening service similar to bit.ly or tinyurl.com. The system should allow users to submit a long URL and receive a unique, shorter alias. When a user navigates to the short URL, they should be redirected to the original long URL.
Requirements
Functional Requirements:
- URL Shortening: Given a long URL, generate a unique short alias.
- URL Redirection: Given a short alias, redirect the user to the original long URL.
- Analytics (Optional/Bonus): Track basic click counts for shortened URLs.
- Custom Aliases: Allow users to pick a custom short alias.
Non-Functional Requirements:
- Scalability: The system must handle high read/write traffic (e.g., 100 million new URLs per day, 1 billion redirects per day).
- Latency: Redirection must be extremely fast (under 100ms).
- Durability: Shortened URLs must not be lost.
- Availability: The service should remain highly available under heavy load.
Deliverables
Your response should include:
- API Design: Define the endpoints for creating and resolving URLs.
- Data Model: Choose between SQL or NoSQL and explain why.
- Architecture: Provide a high-level design including load balancers, application servers, storage, and caching layers.
- Generation Strategy: Explain how you will generate unique short codes and handle collisions.
- Trade-offs: Discuss how you handle high read volume versus write volume and how you maintain consistency.
Hints
Hint 1
Consider using a distributed key generation service or hashing approach to create unique identifiers without needing to check for collisions constantly.
Hint 2
Think about where a caching layer would be most effective to reduce database load for the most popular URLs.
Hint 3
Address the choice of database by considering the specific read/write patterns and the need for horizontal scaling.
Follow-up questions
- How would you handle the deletion or expiration of shortened URLs after a certain period?
- How would you protect the system from malicious use, such as massive scraping or shortening URLs that point to malware?
- If you were tasked with tracking geolocation data for each click, how would you design the analytics pipeline to ensure it doesn't impact redirection latency?
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.