DEV Community

Jatin Gupta
Jatin Gupta

Posted on

How URL Shorteners Generate Unique Links Instantly

URL shortener system design workflow

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
Enter fullscreen mode Exit fullscreen mode

Short URL:

https://short.ly/g8
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Approach 1: Using URL Prefix
One simple method is:
Take the first few characters of the original URL.

Example:

https://linkedin.com/jobs
→ link
Enter fullscreen mode Exit fullscreen mode

Problem:
Many URLs start with the same letters.

https://linkedin.com/jobs
https://linkedin.com/feed
https://linkedin.com/profile
Enter fullscreen mode Exit fullscreen mode

All become:

link
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Step 1: User Sends Long URL
Example:

https://www.linkedin.com/posts/example-post

Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Every new URL gets the next number.

Why Not Use Database Auto Increment?
Databases can generate IDs, but under very high traffic:

  1. Database becomes a bottleneck
  2. Slower performance
  3. 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
Enter fullscreen mode Exit fullscreen mode

This makes Redis perfect for counters.

Redis INCR Command
Redis provides a command called:

INCR
Enter fullscreen mode Exit fullscreen mode

It increases a number by 1.

Example:

Counter = 1000

Request 1:
INCR
→ 1001

Request 2:
INCR
→ 1002

Request 3:
INCR
→ 1003
Enter fullscreen mode Exit fullscreen mode

Why Redis INCR Is Special
Redis operations are atomic.
Atomic means:

Only one operation happens at a time.
Enter fullscreen mode Exit fullscreen mode

Even if:

1 million requests arrive simultaneously
Enter fullscreen mode Exit fullscreen mode

Redis guarantees:

1001
1002
1003
1004
...
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Now both URLs have the same short code.
System breaks.
Redis prevents this problem.

Step 3: Convert Number to Base62
Now we have:

1001
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Total:

26 + 26 + 10 = 62 characters
Enter fullscreen mode Exit fullscreen mode

Character set:

abcdefghijklmnopqrstuvwxyz
ABCDEFGHIJKLMNOPQRSTUVWXYZ
0123456789
Enter fullscreen mode Exit fullscreen mode

Why Base62?
Because it is:

✅ URL friendly

✅ Compact

✅ Easy to encode

✅ No special characters
Enter fullscreen mode Exit fullscreen mode

Example Base62 Conversion
Suppose:

1000 → g8
1001 → g9
1002 → ga
Enter fullscreen mode Exit fullscreen mode

Now:

https://short.ly/g8
Enter fullscreen mode Exit fullscreen mode

Is much shorter than:

https://short.ly/1000
Enter fullscreen mode Exit fullscreen mode

How Base62 Conversion Works
Think of it like converting:

Decimal → Binary
Enter fullscreen mode Exit fullscreen mode

But instead:

Decimal → Base62
Enter fullscreen mode Exit fullscreen mode

Example:

1000 ÷ 62
Enter fullscreen mode Exit fullscreen mode

Repeated division generates characters from the Base62 character set.
The resulting string becomes the short code.

How Many URLs Can Base62 Create?

Formula:

62ⁿ
Enter fullscreen mode Exit fullscreen mode

Where:

62 = available characters
n = length of short code
Enter fullscreen mode Exit fullscreen mode

1 Character

62 combinations
Enter fullscreen mode Exit fullscreen mode

2 Characters

62² = 3,844
Enter fullscreen mode Exit fullscreen mode

3 Characters

62³ = 238,328
Enter fullscreen mode Exit fullscreen mode

6 Characters

626ⁿ
Enter fullscreen mode Exit fullscreen mode

Result:

56,800,235,584
Enter fullscreen mode Exit fullscreen mode

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)     |
Enter fullscreen mode Exit fullscreen mode

Database record:

{
  "id": 1000,
  "shortCode": "g8",
  "originalUrl": "https://example.com/very-long-url",
  "createdAt": "2026-06-08",
  "expiryDate": null
}
Enter fullscreen mode Exit fullscreen mode

Step 5: Return Short URL
Backend returns:

https://short.ly/g8
Enter fullscreen mode Exit fullscreen mode

The user can now share this URL anywhere.

Step 6: Redirection Process
When someone visits:

https://short.ly/g8
Enter fullscreen mode Exit fullscreen mode

The backend:

  1. Reads g8
  2. Finds corresponding URL
  3. Returns HTTP redirect

Example:

g8
↓
Database lookup
↓
Original URL found
↓
302 Redirect
↓
User reaches destination
Enter fullscreen mode Exit fullscreen mode

Why Not Use Random Strings?

Example:

a8KzP1
Enter fullscreen mode Exit fullscreen mode

Random generation causes problems:

  1. Collision checks required
  2. More database queries
  3. Slower performance

Example:

Generated:
abc123

Already exists?
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
+
/
Enter fullscreen mode Exit fullscreen mode

Problem:

+
/
=
Enter fullscreen mode Exit fullscreen mode

Special characters are not ideal inside URLs.

Example:

ab+c/
Enter fullscreen mode Exit fullscreen mode

Needs encoding.

Base62 Characters

a-z
A-Z
0-9
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

instead of:

short.ly/g8
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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)