0
0
HLDsystem_design~7 mins

First design walkthrough (URL shortener) in HLD - System Design Guide

Choose your learning style9 modes available
Problem Statement
Users want to share long URLs easily, but long links are hard to remember, prone to errors when typed, and visually cluttered. Without a system to shorten URLs, sharing links in messages, social media, or print becomes inconvenient and error-prone.
Solution
A URL shortener creates a unique short code for each long URL and redirects users from the short URL to the original long URL. It stores the mapping in a database and uses a simple lookup to forward requests quickly, making links easier to share and remember.
Architecture
User Client
(Browser/App)
URL Shortener

This diagram shows a user sending a request to the URL shortener service, which stores or retrieves the long URL from a database and redirects the user to the original URL.

Trade-offs
✓ Pros
Simplifies sharing by converting long URLs into short, easy-to-remember links.
Reduces errors when typing or copying URLs.
Enables tracking and analytics on link usage.
✗ Cons
Requires storage and management of URL mappings, which can grow large.
Potential for collisions if short codes are not generated carefully.
Adds a dependency on the shortener service's availability for redirects.
Use when your application needs to share or distribute long URLs frequently, especially if user experience benefits from shorter links and you expect at least hundreds of requests per day.
Avoid if your system has very low URL sharing needs (less than a few dozen per day) or if you cannot maintain the availability of the redirect service reliably.
Real World Examples
Bitly
Bitly shortens URLs for marketing campaigns, enabling easy sharing and click tracking across social media and email.
Twitter
Twitter uses its own URL shortener to fit links within the character limit and track link clicks.
Google
Google's former URL shortener helped users share links easily and provided analytics on link usage.
Alternatives
Hash-based URL shortening
Generates short codes by hashing the original URL instead of random or sequential IDs.
Use when: Choose when you want deterministic short codes that always map the same long URL to the same short code.
Custom alias URL shortening
Allows users to specify their own short code instead of auto-generated ones.
Use when: Choose when user-friendly or branded short URLs are required.
Distributed URL shortening
Uses multiple servers and databases to scale URL shortening for very high traffic.
Use when: Choose when expecting millions of URL shortening requests and redirects per day.
Summary
URL shorteners solve the problem of sharing long, unwieldy URLs by creating short, easy-to-share links.
They work by storing a mapping from a short code to the original URL and redirecting users accordingly.
This pattern improves user experience but requires careful design to handle scale, collisions, and availability.