Problem
You are tasked with designing the core architecture for a URL shortening service similar to bit.ly or TinyURL. The service should accept a long URL and return a shortened version that redirects users to the original URL when visited.
Requirements
- Shorten URL: Given a long URL, generate a unique short code (e.g.,
https://short.ly/abc123) and store the mapping.
- Redirect: When a user visits the short URL, redirect them to the original long URL with minimal latency.
- Analytics (basic): Track the number of times each short URL has been accessed.
- Expiration: Support optional expiration dates for short URLs.
Constraints
- The system should handle up to 1,000 new URLs per second.
- Redirects should complete in under 50ms (excluding network latency).
- Short codes should be URL-safe and reasonably short (6-8 characters).
- The same long URL submitted by different users may produce different short codes.
- Short codes must not be easily guessable or sequential.
What to Design
Describe the following in your solution:
- The overall system architecture (components, data flow)
- How you would generate unique short codes
- The database schema and choice of database
- How you would handle high read throughput for redirects
- How you would handle collisions in short code generation