Every day, billions of links are shared through services like Bitly, TinyURL, and many custom URL shorteners.
But have you ever wondered:
How can they generate unique short URLs instantly, even when millions of users are creating links at the same time?
Let's understand the complete system step by step-
What is a URL Shortener?
A URL shortener converts a long URL into a shorter and easier-to-share URL.
Example
Long URL:
https://www.example.com/blog/how-url-shorteners-work-in-distributed-systems
Short URL:
https://short.ly/g8
When someone opens short.ly/g8, the service redirects them to the original long URL.
The Main Challenge
Imagine:
Millions of users creating links
Thousands of requests every second
No duplicate short URLs allowed
The system must:
✅ Generate links quickly
✅ Never create duplicates
✅ Scale to millions of users
✅ Keep URLs short
Approach 1: Using URL Prefix
One simple method is:
Take the first few characters of the original URL.
Example:
https://linkedin.com/jobs
→ link
Problem:
Many URLs start with the same letters.
https://linkedin.com/jobs
https://linkedin.com/feed
https://linkedin.com/profile
All become:
link
This creates collisions.
Therefore, real-world systems don't use this approach.
Approach 2: Unique Counter + Base62 Encoding
This is one of the most common approaches used in large systems.
The process looks like:
User submits URL
↓
Generate unique ID
↓
Convert ID to Base62
↓
Store mapping
↓
Return short URL
Step 1: User Sends Long URL
Example:
https://www.linkedin.com/posts/example-post
The request reaches the backend server.
Step 2: Generate a Unique Number
Instead of generating random strings, the system first generates a unique number.
Example:
1000
1001
1002
1003
Every new URL gets the next number.
Why Not Use Database Auto Increment?
Databases can generate IDs, but under very high traffic:
- Database becomes a bottleneck
- Slower performance
- Increased load Therefore, many systems use Redis.
What is Redis?
Redis is an extremely fast in-memory database.
It stores data in RAM instead of disk.
Because RAM is much faster than disk:
Database → milliseconds
Redis → microseconds
This makes Redis perfect for counters.
Redis INCR Command
Redis provides a command called:
INCR
It increases a number by 1.
Example:
Counter = 1000
Request 1:
INCR
→ 1001
Request 2:
INCR
→ 1002
Request 3:
INCR
→ 1003
Why Redis INCR Is Special
Redis operations are atomic.
Atomic means:
Only one operation happens at a time.
Even if:
1 million requests arrive simultaneously
Redis guarantees:
1001
1002
1003
1004
...
No duplicates.
No race conditions.
No conflicts.
This is why Redis is commonly used in URL shortening systems.
What Is a Race Condition?
Imagine two users create a URL at exactly the same moment.
Without atomic operations:
User A gets ID 1001
User B gets ID 1001
Now both URLs have the same short code.
System breaks.
Redis prevents this problem.
Step 3: Convert Number to Base62
Now we have:
1001
Using this directly would make URLs long.
So we convert it into Base62.
What Is Base62?
Base62 uses:
a-z → 26 characters
A-Z → 26 characters
0-9 → 10 characters
Total:
26 + 26 + 10 = 62 characters
Character set:
abcdefghijklmnopqrstuvwxyz
ABCDEFGHIJKLMNOPQRSTUVWXYZ
0123456789
Why Base62?
Because it is:
✅ URL friendly
✅ Compact
✅ Easy to encode
✅ No special characters
Example Base62 Conversion
Suppose:
1000 → g8
1001 → g9
1002 → ga
Now:
https://short.ly/g8
Is much shorter than:
https://short.ly/1000
How Base62 Conversion Works
Think of it like converting:
Decimal → Binary
But instead:
Decimal → Base62
Example:
1000 ÷ 62
Repeated division generates characters from the Base62 character set.
The resulting string becomes the short code.
How Many URLs Can Base62 Create?
Formula:
62ⁿ
Where:
62 = available characters
n = length of short code
1 Character
62 combinations
2 Characters
62² = 3,844
3 Characters
62³ = 238,328
6 Characters
626ⁿ
Result:
56,800,235,584
Over 56 billion URLs.
Step 4: Store Mapping
The system saves:
Short Code
| Short Code | Original URL |
| ---------- | ---------------------------------------------------------------------- |
| g8 | [https://example.com/very-long-url](https://example.com/very-long-url) |
| g9 | [https://linkedin.com/post](https://linkedin.com/post) |
| ga | [https://youtube.com/watch?v=abc](https://youtube.com/watch?v=abc) |
Database record:
{
"id": 1000,
"shortCode": "g8",
"originalUrl": "https://example.com/very-long-url",
"createdAt": "2026-06-08",
"expiryDate": null
}
Step 5: Return Short URL
Backend returns:
https://short.ly/g8
The user can now share this URL anywhere.
Step 6: Redirection Process
When someone visits:
https://short.ly/g8
The backend:
- Reads g8
- Finds corresponding URL
- Returns HTTP redirect
Example:
g8
↓
Database lookup
↓
Original URL found
↓
302 Redirect
↓
User reaches destination
Why Not Use Random Strings?
Example:
a8KzP1
Random generation causes problems:
- Collision checks required
- More database queries
- Slower performance
Example:
Generated:
abc123
Already exists?
Need extra lookup.
Counter + Base62 avoids this completely.
System Design Used by Large Companies
A simplified architecture:
Client
↓
Load Balancer
↓
Application Servers
↓
Redis (INCR Counter)
↓
Database
Redis
Generates unique IDs.
Application Server
Converts IDs to Base62.
Database
Stores URL mappings.
Load Balancer
Distributes traffic among servers.
Interview Question: Why Base62 Instead of Base64?
Many people confuse Base62 and Base64.
Base64 Characters
A-Z
a-z
0-9
+
/
Problem:
+
/
=
Special characters are not ideal inside URLs.
Example:
ab+c/
Needs encoding.
Base62 Characters
a-z
A-Z
0-9
No special characters.
Completely URL-friendly.
Therefore URL shorteners usually use Base62, not Base64.
Real-World Optimizations
Large URL shorteners also add:
Caching
Frequently accessed URLs stay in Redis.
Analytics
Track:
Click count
Device type
Browser
Country
Expiration
Links can expire automatically.
Custom Aliases
short.ly/jatin
instead of:
short.ly/g8
Final Summary
A modern URL shortener works like this:
User submits URL
↓
Redis INCR generates unique ID
↓
ID converted to Base62
↓
Mapping stored in database
↓
Short URL returned
↓
User opens short URL
↓
Database lookup
↓
Redirect to original URL
Key Takeaways
Redis INCR generates unique IDs atomically.
Atomic operations prevent duplicate URLs.
Base62 makes IDs short and URL-friendly.
6 Base62 characters can generate over 56 billion unique combinations.
This approach is fast, scalable, and widely used in production URL shortening systems.
This is why services like Bitly can create unique short URLs almost instantly, even when handling millions of requests every second. 🚀

Top comments (0)