Imagine your friend sends you a super long link to a cat video:
https://www.youtube.com/watch?v=dQw4w9WgXcQ&ab_channel=RickAstleyOfficial&list=PL1234567890ABCDEFGHIJK
That’s ugly, right? What if we could turn it into something like:
https://sho.rt/xyz123
That’s exactly what a URL shortener does — and in this article, you’ll learn how to design such a system, from scratch, even if you’ve never done any system design before.
🌐 What Is a URL Shortener?
A URL shortener is a web service that turns long URLs into short ones. When someone clicks the short link, it redirects them to the original long URL.
Popular examples:
bit.ly
tinyurl.com
-
t.co
(used by Twitter)
🎯 Goal of the System
Design a system that:
- Accepts a long URL like
https://www.example.com/articles/2025/07/shorten-this-link
. - Returns a short URL like
sho.rt/abc123
. - When someone visits
sho.rt/abc123
, they’re redirected to the original URL.
🔨 Core Features
Feature | Description |
---|---|
Shorten URL | Accept a long URL and generate a short one |
Redirect | Given a short URL, redirect to the original long URL |
Analytics (optional) | Count how many times a short URL was clicked |
Expiration (optional) | Make some short URLs valid only for a limited time |
🧠 Key Design Considerations
Before we build anything, we must think about:
1. Scalability
Can our system handle millions of links and billions of redirects?
2. Availability
Can people always access their links even if something crashes?
3. Speed
Can the system redirect people in milliseconds?
4. Uniqueness
How do we ensure no two links generate the same short code?
🧱 Basic Components
Let’s break down the system into pieces:
[Client] -> [Load Balancer] -> [Application Servers] -> [Database]
- Client: The user who wants to shorten or open a link.
- Load Balancer: Distributes incoming requests to servers.
- Application Server: Handles logic (generate/redirect).
- Database: Stores mapping between short and long URLs.
🔢 How to Generate Short URLs
Let’s say we want to shorten this:
https://example.com/my-very-long-article-about-space
We can generate a short URL code in several ways:
Option 1: Hashing
- Hash the original URL using a function like
SHA-256
. - Take the first 6–8 characters of the hash.
- Map that to a short code.
✅ Simple
❌ Risk of collision (two URLs might get same short code)
Option 2: Auto-Increment ID + Base62
- Store each new URL in the database with a numeric ID (like 123456).
- Convert the ID to Base62 (0-9 + a-z + A-Z):
123456 → "abc123"
✅ Unique, Compact
✅ Easy to reverse
❌ Not random (easy to guess next links)
Option 3: Random String
- Generate a random string of 6-8 characters.
- Check in DB if it's already taken. If not, save it.
✅ Harder to guess
❌ Can collide, so we need retries
📦 Data Model (Database Schema)
Let’s use a simple table:
Field | Type | Description |
---|---|---|
short_code | VARCHAR(10) | The generated code, like abc123
|
long_url | TEXT | The original URL |
created_at | DATETIME | When the short URL was created |
expiry_date | DATETIME | (Optional) Expiration date |
click_count | INT | (Optional) Number of times clicked |
🚦 Redirect Flow
When someone visits:
https://sho.rt/abc123
Here’s what happens:
- User’s browser sends a request to your server with code
abc123
. - Server looks up the code in the database.
- If found, it sends a 301 redirect to the original long URL.
- Browser takes the user to the long URL.
🏗️ Putting It Together (Step-by-Step)
1. URL Shortening API
POST /shorten
Request:
{
"long_url": "https://example.com/article/123"
}
Response:
{
"short_url": "https://sho.rt/abc123"
}
2. Redirection API
GET /abc123
Action:
- Find
abc123
in the database. - If found, return a 301 redirect to the long URL.
⚙️ Scaling the System
If the system grows big, here’s how we keep it fast:
1. Caching
Use Redis or Memcached to cache popular short codes in memory.
This avoids hitting the database on every redirect.
2. Sharding
Split the database into multiple machines to handle billions of entries.
3. Rate Limiting
Prevent abuse by limiting how many links a user can create per minute.
4. Load Balancing
Distribute traffic across multiple servers using a load balancer like Nginx or AWS ELB.
🛡️ Security and Edge Cases
Problem | Solution |
---|---|
Malicious URLs (phishing, malware) | Use a URL scanner like Google Safe Browsing |
Abuse (spamming) | Rate limit users, add CAPTCHA |
Expired links | Add TTL (Time-To-Live) logic |
Editable links | Let users log in and manage their links |
🏭 Diagram
📊 Optional Features
- Analytics Dashboard: Show users how many clicks, locations, devices, etc.
-
Custom Short Codes: Let users choose their code (e.g.,
sho.rt/mybrand
) - Link Previews: Show metadata when sharing links on social media
- QR Codes: Auto-generate QR for each short link
✅ Summary
Concept | Description |
---|---|
Goal | Shorten and redirect URLs |
Short Code | Can be generated using hashing, Base62, or random strings |
Storage | Store code-to-URL mapping in a database |
Redirect | Lookup short code and return 301 redirect |
Scaling | Use cache, sharding, and load balancing |
Security | Scan for malicious URLs, rate limiting, CAPTCHA |
Extra Features | Analytics, custom codes, link expiration |
🧠 Final Thought
Even though a URL shortener seems simple on the surface, designing it at scale teaches you many real-world software engineering concepts: databases, caching, hashing, APIs, distributed systems, and security.
Whether you're building for a side project or preparing for a system design interview — mastering the design of a URL shortener is a perfect starting point.
Top comments (0)