DEV Community

Kashaf Abdullah
Kashaf Abdullah

Posted on

How URL Shorteners Generate Unique Links Instantly

How URL Shorteners Generate Unique Links


Every day, millions of people share links using services like TinyURL, Bitly, and Rebrandly. Instead of sharing a long and messy URL, these platforms convert it into a short, clean link that is easier to remember and share.

But have you ever wondered how these services generate unique short links almost instantly?

Let's break it down.


What Is a URL Shortener?

A URL shortener is a service that converts a long URL into a much shorter one.

For example:

Original URL: https://example.com/blog/how-url-shorteners-generate-unique-links-instantly

Short URL: https://short.ly/aB3xYz

When someone visits the short URL, they are automatically redirected to the original destination.

The real challenge is ensuring that every short link is unique and generated in milliseconds.


Step 1: Store the Original URL

When a user submits a URL, the system first saves it in a database.

Example:

At this point, the database has assigned a unique ID to the URL.


Step 2: Convert the ID Into a Short Code

Most URL shorteners don't expose database IDs directly.

Instead, they convert the numeric ID into a shorter representation using Base62 encoding.

Base62 uses:

  • Numbers: 0–9
  • Lowercase letters: a–z
  • Uppercase letters: A–Z

This gives a total of 62 possible characters.

Examples:

ID Base62 Code
1001 g9
500000 28CG
10000000 FXsk

The final shortened URL becomes: https://short.ly/g9

Because the database ID is already unique, the generated short code is automatically unique as well.


Why Base62?

Imagine storing URLs as plain numbers: https://short.ly/1001

This works, but it becomes longer as IDs grow.

Base62 compresses large numbers into fewer characters, making links shorter and more user-friendly.

Number Base62
1001 g9
500,000 28CG
10,000,000 FXsk

This is why most URL shortening services use some form of Base62 encoding.


Alternative Approach: Random Code Generation

Some systems generate random strings instead of converting IDs.

Example: aB3xYz

The system checks whether the code already exists:

  • If it doesn't exist → code is saved
  • If it already exists → another code is generated

Pseudo-code:
do {
code = generateRandomString(6);
} while (codeExists(code));

save(code);

This approach works well for smaller systems but may require additional collision checks at scale.


How Redirection Works

When a user visits: https://short.ly/g9

The server looks up the short code in the database:

SELECT long_url
FROM urls
WHERE short_code = 'g9';
Enter fullscreen mode Exit fullscreen mode

Result: https://example.com/blog/how-url-shorteners-generate-unique-links-instantly

The server then responds with an HTTP redirect:
HTTP/1.1 301 Moved Permanently
Location: https://example.com/blog/how-url-shorteners-generate-unique-links-instantly

The browser automatically opens the original URL.

How Large Platforms Handle Millions of Links

Services like Bitly process enormous volumes of traffic every day. To maintain speed and uniqueness, they often:

Distribute ID ranges across multiple servers

Cache frequently accessed URLs

Use load balancers

Store mappings in highly optimized databases

Generate IDs using distributed ID systems

This ensures that links remain unique even when millions of URLs are created every day.

Final Thoughts

URL shorteners appear simple from the outside, but they solve an interesting engineering challenge: generating unique, compact links instantly while supporting massive scale.

The most common approach is surprisingly straightforward:

Store the original URL

Generate a unique database ID

Convert the ID into a Base62 string

Return the shortened URL

Redirect users back to the original URL when accessed

This combination of simplicity, speed, and scalability is what makes modern URL shortening services so effective.

Top comments (0)