Design a URL shortener
J.P. MorganSoftware Engineer · Technical ScreenLast updated
This question assesses the ability to design a distributed URL shortening service, focusing on database schema design, collision resolution, and caching strategies for high-read throughput.
System Design: URL Shortener
Design a service similar to Bitly that takes a long URL and returns a shorter, unique alias (e.g., bit.ly/xyz123). The system needs to support high read traffic (redirects) and moderate write traffic (creating new links).
Functional Requirements
- Shorten: Given a long URL, generate a unique, shorter URL.
- Redirect: Given a short URL, redirect the user to the original long URL.
- Analytics (Optional): Basic tracking of total redirect count.
Non-Functional Requirements
- High Availability: The redirect service must be fast and reliable.
- Scalability: The system should support billions of URLs.
- Latency: Redirects should happen with minimal latency (ideally < 100ms).
Expectations
- API Design: Define the request/response structure for the shortening and redirect endpoints.
- Data Model: Propose a database schema (SQL vs. NoSQL) and explain why.
- Algorithm: Explain how you will generate the short key (e.g., hashing, base conversion, or centralized counter).
- High-level Architecture: Describe the components (Load Balancer, Web Servers, Database, Caching layer) and how they interact.
- Scalability: How do you handle a scenario where a specific URL becomes viral?
Hints
Hint 1
Consider using Base62 encoding to map a numeric ID to a short string to minimize key length.
Hint 2
Decide whether you want to use a distributed database like Cassandra or a relational one like MySQL, keeping in mind the need for high-speed primary key lookups.
Hint 3
Think about where to place a cache (like Redis) to reduce the load on the database for frequently accessed URLs.
Follow-up questions
- How would you handle the deletion or expiration of links?
- If two users provide the same long URL, should they get the same short URL, and how would you manage that?
- How would you evolve this system to support custom aliases (e.g., bit.ly/my-cool-link)?
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.