Ever struggled with sharing a link so long that takes up the entire message? URL shorteners like TinyURL come to the rescue, transforming lengthy web addresses into compact, readable link, making it cleaner and shareable — especially on platforms where every character counts.

But how does it work?
This article will demystify URL Shorteners, explaining their working and architecture. Whether you're simply curious or planning to build one, this guide will break it down for you.
{
"longUrl": "https://example.com/very-long-url",
"customAlias": "custom123", // Optional
"expiryDate": "2024-12-31T23:59:59Z" // Optional
}{
"shortUrl": "https://tiny.url/custom123"
}{
"shortUrl": "https://tiny.url/custom123",
"longUrl": "https://example.com/very-long-url",
"clickCount": 12345,
"createdAt": "2024-01-01T00:00:00Z"
}

The Core Components
How It All Comes Together
Let’s roll to the mechanics of a URL shortener.
1. Input Validation: The First Line of Defense
Before anything, validate the input to ensure it's a legitimate URL and ready for transformation. This involves:
2. The Core Logic : Exploring Techniques for Unique Short URL Generation
This technique directly hashes the long URL using a hashing algorithm which converts the input string into 32-char/64-char hexadecimal value.
Since the hash result won't be human-readable and too long for "short" URL, encoding comes to the picture (that too Base62 encoding : uses only 6 char of the hash and converts it to an alphanumeric string 0-9 digits, lower and uppercase eng alphabets)
eg.
Input: https://example.com/very/long/url/with/multiple/segments
Hashed Value: 33b6758ee9b0c5be8f4a9a123c9a3e7813cb5875ad24b3e438c98db8f7b3c4d7
Base62 Encode: 6Ahiy
Short URL: https://tiny.url/6AhiyDownsides
-- Risk of collisions if the hash is truncated too much.
-- Requires handling collision scenarios, adding complexity.

This technique generates a random alphanumeric string for each URL, ensuring uniqueness
How it works
eg.
Input: https://example.com/long/url
Random String: aB3xY2
Short URL: https://tiny.url/aB3xY2Downsides
This approach uses an auto-incremented ID for every new URL
Converts it into a Base62 string
eg.
Input: Three long URLs:
https://example.com/first
https://example.com/second
https://example.com/third
Process:
Assign IDs: 1, 2, 3.
Convert IDs to Base62:
ID 1 → Base62: 1
ID 2 → Base62: 2
ID 3 → Base62: 3
Construct short URLs:
https://tiny.url/1
https://tiny.url/2
https://tiny.url/3
Output:
Short URLs: https://tiny.url/1, https://tiny.url/2, https://tiny.url/3Downsides
-- IDs are predictable, which might let someone guess the next URL.
-- It’s not ideal for distributed systems where multiple servers handle requests.
eg.
Input: https://example.com/secure/url
Process
Assign Sequential ID: 123
Apply Hashing:
f1a2c3Encode in Base62:
6AhiyOutput: https://tiny.url/6Ahiy
Downsides
-- Slightly more computational overhead for hashing.
-- Salt must be carefully managed to maintain consistency.
Sharded Key Generation
When a system grows large and distributed (like handling millions of URLs across multiple servers), generating unique IDs becomes a challenge. A single database can't handle all the traffic efficiently.
Sharded key generation solves this by decentralizing the process : Each server or "shard" is responsible for generating unique keys independently
Popular techniques
eg.
Input: https://example.com/scalable/url
Process:
Timestamp: 20240101
Shard ID: 01
Sequence: 0001
// combine all three
Snowflake ID: 20240101010001
Base62(20240101010001): aB3xY2
Output:
Short URL: https://tiny.url/aB3xY2