πŸ” Design a TinyURL System (Like Bit.ly) β€” From Scratch

 Ever wondered how Bit.ly shrinks huge URLs into tiny ones like https://bit.ly/abc123?
In this article, you’ll build your own TinyURL system step-by-step β€” and learn some system design + algorithmic magic along the way.

βœ… Problem Statement

Design a URL shortening service. It should:

Convert a long URL into a short one

Retrieve the original long URL from the short one

Be scalable and efficient

🧠 The Idea

We’ll map long URLs to short codes like this:

https://www.example.com/very/long/url  
β†’ https://tinyurl.com/abc123

Behind the scenes:

Use a base62 encoding (a-zA-Z0-9) for compact URLs

Store mappings in a HashMap

Optionally store data in a database for persistence

πŸ”’ Base62 Encoding

To make URLs short, we encode integers in base62:

Base62 characters = [a-z, A-Z, 0-9] = 26 + 26 + 10 = 62 chars

We can assign each long URL a unique ID (like 123456), and convert that number into a base62 string.

πŸ’» Code (Python Example)

Step 1: Base62 Encoder

import string

class TinyURL:
    def __init__(self):
        self.url_to_code = {}
        self.code_to_url = {}
        self.counter = 1
        self.alphabet = string.ascii_letters + string.digits

    def encode_base62(self, num):
        base = 62
        chars = []
        while num > 0:
            num, rem = divmod(num, base)
            chars.append(self.alphabet[rem])
        return ''.join(reversed(chars)).rjust(6, '0')  # pad to 6 characters

    def encode(self, long_url):
        if long_url in self.url_to_code:
            code = self.url_to_code[long_url]
        else:
            code = self.encode_base62(self.counter)
            self.url_to_code[long_url] = code
            self.code_to_url[code] = long_url
            self.counter += 1
        return f"https://tinyurl.com/{code}"

    def decode(self, short_url):
        code = short_url.split("/")[-1]
        return self.code_to_url.get(code, "URL not found")

πŸ”„ Usage Example

tiny = TinyURL()

short = tiny.encode("https://www.example.com/article/how-to-make-a-url-shortener")
print("Short URL:", short)

original = tiny.decode(short)
print("Original URL:", original)

βš™οΈ Behind the Scenes

The counter ensures every new URL gets a unique number.

encode_base62 makes that number small & URL-safe.

Two hash maps store mappings for quick lookup (O(1)).

πŸ” Optional Add-ons

Add expiration time to short URLs.

Limit number of requests per user (rate limiting).

Use a database like Redis/PostgreSQL for real storage.

Generate random codes instead of using counters.

πŸš€ Real-World Scenarios

This technique is used in:

Bit.ly

Twitter link shortener (t.co)
WhatsApp preview links
Firebase Dynamic Links

πŸ‘¨β€πŸ’» Final Thoughts

“You just built your own Bit.ly system from scratch!
It’s a blend of system design, data structures, and algorithmic creativity.”

Let me know if you want the JavaScript / Java / C++ version too!

πŸ–ŠοΈ Written by Raj Guru Yadav
πŸ“Œ Teen developer & builder of 700+ projects

Similar Posts