Design TinyURL

Difficulty: MEDIUMCategory: HLDFrequency: 1Asked On 2021-10-12

Problem Statement

Design a URL shortening service similar to TinyURL that generates short, unique aliases for long URLs. The system should allow users to create, access, and optionally track shortened URLs with high availability, scalability, and low latency.


Functional Requirements (FR)


URL Shortening


•Accept a long URL and return a short, unique URL.
•Ensure uniqueness of shortened URLs.
•Optional: allow custom short URL aliases.


Redirection


•Redirect users from the short URL to the original long URL.
•Ensure minimal latency in redirection.


Analytics (Optional)


•Track number of clicks per short URL.
•Track geographical location or device type of users.


Expiry & Deletion (Optional)


•Allow setting expiration date for short URLs.
•Allow users to delete their shortened URLs.


APIs


•POST /shorten → Shorten a URL.
•GET /<short_url> → Redirect to original URL.
•GET /analytics/<short_url> → Retrieve click statistics (optional).


Non-Functional Requirements (NFR)


•Scalability → Handle millions of URLs and high request rates.
•High Availability → 24/7 uptime with automatic failover.
•Low Latency → Redirection should be near-instant.
•Consistency → Ensure mapping between short URL and long URL is correct.
•Fault Tolerance → System should recover from node failures without losing URL mappings.


Core Components


API Gateway / Web Server


•Receives requests to shorten or access URLs.
•Handles routing to application servers.



Application Server


•Processes URL shortening requests.
•Generates unique short keys (base62 encoding or hash).
•Stores mapping in database or cache.


Database / Storage


•Stores mapping: short_url_key → long_url.
•Supports high read/write throughput.
•Optional: use NoSQL DB for horizontal scalability.


Cache Layer (Optional)


•Store frequently accessed short URL mappings for faster redirection.


Analytics Service (Optional)


•Logs and tracks clicks per short URL.


Key Design Considerations


Short URL Generation


•Base62 encoding of an auto-increment ID or
•Hashing of the original URL with collision handling.


Database Partitioning / Sharding


•Horizontal scaling by hashing short URL keys to different nodes.


High Availability & Replication


•Replicate DB or cache nodes to avoid single point of failure.


Redirection Performance


•Minimize latency via in-memory caching or CDN.


Collision Handling


•Ensure unique mapping for every short URL.


High-Level Flow


1.User sends POST /shorten with long URL → Application Server.
2.Server generates unique short key → Stores mapping in DB.
3.Returns short URL to user.
4.User accesses GET /<short_url> → Lookup mapping in cache/DB → Redirect to long URL.
5.(Optional) Analytics logged asynchronously.


Evaluation Criteria


•Scalability to handle millions of URLs and requests.
•Low-latency redirection.
•Correctness and uniqueness of URL mappings.
•Fault tolerance and replication strategies.
•Extensibility for analytics, custom aliases, and URL expiry.


Solution

Comments