BitlyDifficulty: Beginner
How to Design a URL Shortener (Bitly)
A classic system design interview question. The goal is to take a long URL, generate a unique short alias, and redirect users quickly when they visit the short alias.
High-Level Architecture
[User] -> (POST /create) -> [API] -> (Base62 Encode) -> [DB]
[User] -> (GET /alias) -> [API] -> [Cache] -> (Redirect 301)Database Design
A simple relational database (PostgreSQL) or NoSQL (MongoDB) mapping `short_hash` -> `original_url`. Include expiration dates and user IDs.
Caching Strategy
Cache heavily using Redis. If a short link goes viral, the DB would crash without caching.
Scaling & APIs
Scaling: Use a pre-generated ID system (like Snowflake or Zookeeper) to ensure multiple servers don't generate the same short alias concurrently. Convert the Base-10 ID to Base-62 to get the short string.
API Design: REST API with rate limiting to prevent malicious users from creating millions of links.
Real-World Challenges
- •Preventing hash collisions.
- •Generating unpredictable hashes for security.
- •Handling 301 (Permanent) vs 302 (Temporary) redirects for analytics.