TinyURL

Difficulty: MEDIUMCategory: HLDFrequency: 1Asked On 2025-05-27

Problem Statement

Design a system that takes a long URL and generates a shorter, unique URL that redirects to the original long URL. The system should support high read and write throughput, ensure uniqueness, and handle large scale.


Functional Requirements (FR)


Short URL Generation


•Accept a long URL and generate a unique short URL.
•The short URL should be concise (e.g., 6-8 alphanumeric characters).


Redirection


•Accessing the short URL should redirect the user to the original long URL.


Custom Aliases (Optional)


•Users can specify a custom short URL instead of a system-generated one.


Analytics (Optional)


•Track usage metrics: number of clicks, unique visitors, geographic info, and referrer.


Expiration (Optional)


•Support URLs that expire after a certain period or number of uses.


Non-Functional Requirements (NFR)


•High Availability: System should be operational 24/7.
•Low Latency: Redirection must be extremely fast (<100ms).
•Scalability: Support billions of URLs and millions of requests per day.
•Consistency: Ensure no collisions in short URL generation.
•Durability: Stored mappings must not be lost.


Core Components


API Layer

•Endpoints:
•POST /shorten → Generate short URL.
•GET /{shortURL} → Redirect to long URL.


Short URL Generator

Methods:

•Random Hash: Generate random 6-8 character strings.
•Base62 Encoding: Encode sequential IDs to alphanumeric strings.
•Ensure uniqueness of generated short URLs.


Database / Storage Layer


•Stores mapping: shortURL → longURL.
•Can be SQL (e.g., MySQL) or NoSQL (e.g., DynamoDB, Cassandra) depending on scale.
•Optional TTL for expiring URLs.


Cache Layer


•Frequently accessed URLs stored in in-memory cache (e.g., Redis) for fast redirection.


Analytics / Logging (Optional)


•Store click events and metadata for reporting.


Load Balancer

•Distributes requests across multiple application servers for scalability.


High-Level Data Flow


Shortening Flow


•User submits a long URL → API server → Short URL generator → Store mapping in DB → Return short URL.


Redirection Flow


•User accesses short URL → Check cache → If not in cache, fetch from DB → Redirect to long URL.
•Optional: Increment click counter in analytics module.


Design Considerations


•Use collision-resistant short URL generation.
•For large scale:
•Use sharding/partitioning to distribute DB storage.
•Use consistent hashing for cache nodes.
•Consider rate limiting to prevent abuse.
•Ensure high throughput reads since redirection is read-heavy.
•Implement monitoring and alerting for system health.


✅ This is a complete HLD Problem Statement for a TinyURL-like service covering core modules, data flow, scalability, and optional features.

Solution

Comments