Design a URL Shortener Service

Difficulty: MEDIUMCategory: HLDFrequency: 1Asked On 2022-06-18

Problem Statement

Design a URL Shortener Service that generates short, unique URLs for long URLs and efficiently redirects users when the short link is accessed. The system should be scalable, fault-tolerant, and performant, handling millions of requests daily.


Functional Requirements (FR)


Shorten URL

•Accept a long URL and return a short unique URL.
•Support both auto-generated keys and custom aliases.

Redirect URL

•When a user accesses the short URL, redirect to the original long URL.

Expiration & Deletion

•Support setting expiration dates for short URLs.
•Option to delete or disable a short URL.

Analytics (Optional/Advanced)

•Track click counts, geographic data, device/browser info.
•Provide stats to users.

APIs

•createShortUrl(longUrl, alias?, expiry?)
•getOriginalUrl(shortUrl)
•deleteUrl(shortUrl)
•getAnalytics(shortUrl)


Non-Functional Requirements (NFR)


•Scalability → Handle millions of URL shortenings and redirections per day.
•Low Latency → Redirection must be < 50ms.
•Availability → 99.99% uptime.
•Consistency → Ensure unique short codes and correct redirections.
•Fault Tolerance → Survive server or data node failures.
•Security → Prevent abuse (phishing, spam URLs).


Core Components


API Gateway / Web Service


•Accepts URL shortening and redirection requests.


Key Generation Service


•Generates unique short codes (Base62 encoding, hash functions, or ID generator).
•Must avoid collisions.


Database


•Store mapping: shortUrl → longUrl (+ metadata).
•Options:
•SQL for consistency.
•NoSQL (Cassandra/DynamoDB/Redis) for high scalability & fast lookups.


Cache Layer (Redis/Memcached)


•Store frequently accessed short URLs for fast redirection.


Redirection Service


•Looks up short URL and issues 301/302 redirect to long URL.


Analytics Service (Optional)


•Logs redirection events for reporting.


Monitoring & Abuse Detection


•Prevent malicious URL shortening (spam, phishing).


High-Level Flow


Shorten URL

•User calls createShortUrl(longUrl) → API Gateway → Key Generator → Database → returns short URL.

Redirect

•User clicks shortUrl.com/xyz123.
•Service looks up in cache → if miss, query DB.
•Redirects (HTTP 302) to long URL.

Analytics (Optional)

•Log request details asynchronously to analytics DB.


Scalability & Trade-offs


DB Choice:

•SQL (Postgres/MySQL) → strong consistency, but harder to scale.
•NoSQL (Cassandra, DynamoDB) → horizontal scalability, eventual consistency.


ID Generation:

•Centralized counter → simple, but bottleneck.
•Base62 encoded hash → distributed, but risk of collisions.
•Snowflake-style ID generator → scalable & unique.
•Caching → Reduces DB load for popular URLs.
•Replication & Sharding → Needed for global availability.


Design Goals


•Efficient short URL creation & redirection.
•Global scalability to handle billions of links.
•Reliability & fault tolerance with replication.
•Low latency lookups via cache.
•Optional analytics for business insights.


✅ This is a classic HLD interview problem, testing knowledge of ID generation, caching, distributed storage, scaling, and trade-offs.

Solution

Comments