Design a URL Shortener
HackerRankSoftware Engineer · Technical ScreenLast updated
This question assesses the ability to design a URL shortening service (like Bitly) focusing on scalability, distributed ID generation, and efficient data retrieval.
Design a URL Shortener
Design a service that takes a long URL and returns a shorter, alias URL (e.g., bit.ly/xyz123). When a user visits the shortened URL, the service should redirect them to the original long URL.
Functional Requirements
- URL Shortening: Users can provide a long URL and receive a unique short link.
- Redirection: Visiting the short link must redirect to the original long URL.
- Custom Aliases: Users should optionally be able to define their own custom short URL.
- Analytics (Optional): Basic tracking of click counts.
Non-Functional Requirements
- Scalability: The system must support millions of requests per day.
- High Availability: Redirection must be extremely fast (latency < 100ms).
- Persistence: Shortened URLs should be stored reliably.
Constraints & Assumptions
- Assume 100 million new URLs created per month.
- Assume a 100:1 read-to-write ratio (10 billion redirects per month).
- Links can expire after a certain period, or persist indefinitely.
Expectations
Provide a high-level design that includes:
- API Endpoints: Define the request and response structures.
- Data Model: Choose an appropriate database schema (SQL vs. NoSQL) and explain why.
- ID Generation: Propose a mechanism for creating unique, collision-resistant short keys.
- Caching Strategy: Explain how to optimize read-heavy traffic patterns.
- System Diagram: Describe the components involved (Load Balancer, Web Server, Database, Cache).
Hints
Hint 1
Consider the trade-offs between using a random hash function versus a base-62 encoding of an auto-incrementing database ID.
Hint 2
Think about how a Distributed Cache like Redis can be placed in front of your database to handle the high volume of read requests.
Hint 3
Address how you would partition (shard) your data across multiple database instances if the total URL count grows into the billions.
Follow-up questions
- How would you handle the 'collision' problem if two different users try to claim the same custom alias simultaneously?
- How would you implement URL expiration or garbage collection for links that haven't been accessed in a year?
- What strategies would you implement to prevent malicious users from spamming the system or creating short links to harmful content?
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.