Designing a URL Shortener
Make Links Short & Sweet βοΈπ

The Long Story (Literally)
Your friend sends you a link to their portfolio:
https://www.thisisaverylongurl.com/users/portfolio?userId=9xk34&ref=linkedin
Looks like a digital snake π. You squint at it, sigh, and say:
βWhy not shorten that?β
Boom π₯ β now it's:
bit.ly/myfriend
Looks clean. Feels good. And today, you're going to learn how to build something just like that.
Letβs design our own URL shortener β a mini Bit.ly β from scratch, system design style.
π― What Is a URL Shortener?
A URL shortener is a service that takes a long, hard-to-remember link and turns it into a short version.
Think:
π Long URL: https://amazon.com/product/deal/123xyz456
π Short URL: amzn.to/deal
When someone clicks the short link, the system redirects them to the original long URL.
Services like Bit.ly, TinyURL, and Twitterβs t.co do exactly this.
π§© Step 1: Define the Requirements
Before jumping into code or architecture, ask:
π‘ βWhat does this system need to do?β
β Functional Requirements
- Take a long URL, return a short one
- Redirect short URLs to the original ones
- Bonus features:
- Custom aliases (short.ly/myportfolio)
- Expiration dates
- Click analytics
π§± Non-Functional Requirements
- High availability β links should never break
- Low latency β redirections must be fast
- Scalability β must handle billions of links
- Reliability β data should never be lost
π’ Step 2: Generating Short URLs
This is the core of the system. Here are two popular methods:
β¨ Method 1: Hash the URL
Use hashing (like MD5 or SHA-256), and take the first 6β8 characters.
- β Easy to implement
- β Risk of collisions
- β Not ideal for versioning or expiration
π Method 2: Counter + Base62 (Recommended)
- Maintain a global auto-incrementing counter
- Convert the counter to Base62 (0β9, aβz, AβZ)
- Use that as the short code!
π‘ Base62 gives you 62βΆ = 56.8 billion combinations with just 6 characters!
Example:
Counter = 123456 β Base62 = abc123
ποΈ Step 3: Database Design
We need a place to store the mappings between long and short URLs.
Table: url_mappings

βοΈ Step 4: Workflow Breakdown
βοΈ a. Shortening a URL
- User sends a POST request with a long URL
- Backend increments counter β converts to Base62
- Stores mapping in database
- Returns: https://short.ly/abc123
π b. Redirecting a URL
- User visits https://short.ly/abc123
- Backend looks up abc123 in DB
- If found (and not expired), redirect to long URL
β‘ Step 5: Make It Fast with Caching
Redirection is read-heavy. You donβt want to hit the DB every time.
Use Redis or Memcached:
- Cache hot short codes like abc123 β original URL
- On miss: fetch from DB and populate cache
- On expiration: remove from both cache and DB
π Step 6: Make It Scale
When traffic grows, your system must grow too.
Techniques:
- Sharding: Split DB into smaller parts
- Replication: Use read replicas for redirects, master DB for writes
- Load Balancing: Spread requests across multiple servers
- CDN: Use edge locations for faster redirect responses
- Rate Limiting: Stop bots from spamming millions of links
π Bonus Features
1. Expiration Dates
- Add TTL to each short URL
- Delete or mark invalid after expiration
2. Custom Aliases
- Let users choose:
- short.ly/sreya-portfolio
- Check if alias already exists before saving.
3. Analytics
Track:
- Number of clicks
- Geo-location
- Browser/device type
Use tools like Kafka, Spark, or BigQuery for scalable analytics pipelines.
π Wrapping It All Up
And thatβs a wrap on URL shorteners! Hereβs what youβve learned:
- Generate Short Links Efficiently with Base62 encoding and a counter for fast, scalable links.
- Ensure Speed and Reliability by caching popular links and reducing database hits for quicker redirection.
- Scale for Growth through sharding, replication, and load balancing to handle millions of requests.
- Add Extra Features like custom aliases, expiration dates, and analytics to make your service stand out.
About the Creator
Sreya Satheesh
Senior Software Engineer | Student
https://github.com/sreya-satheesh
https://leetcode.com/u/sreya_satheesh/


Comments