URL Shortner System Design (original) (raw)

Last Updated : 6 Apr, 2026

The need for efficient and concise URL management has become important in today’s digital world. URL shortening services help convert long and complex links into short, easy-to-share URLs. This improves usability, especially on social media and messaging platforms.

**Example: A long e-commerce product link can be shortened into something like bit.ly/xyz123, making it easier to share on platforms like WhatsApp or Twitter.

1. System Requirements

These requirements define what the system should do and how well it should perform.

1. Functional requirements

These describe the core features that the system must provide to users.

2. Non-Functional requirements

These define the performance, reliability, and security aspects of the system.

2. Capacity Estimation

Let's assume our service has 30M new URL shortenings per month. Let’s assume we store every URL shortening request (and associated shortened link) for 5 years. For this period the service will generate about 1.8 B records.

30 million * 5 years * 12 months = 1.8B

**Note: Let's consider we are using 7 characters to generate a short URL. These characters are a combination of 62 characters [A-Z, a-z, 0-9] something like https://zpzy.in/redirecting?code=abXdef2.

Data Capacity Modeling

Discuss the data capacity model to estimate the storage of the system. We need to understand how much data we might have to insert into our system. Think about the different columns or attributes that will be stored in our database and calculate the storage of data for five years. Let's make the assumption given below for different attributes.

The above calculation will give a total of 2.031KB per shortened URL entry in the database.
If we calculate the total storage then for 30 M active users
total size = 30000000 * 2.031 = 60780000 KB = 60.78 GB per month. In a Year of 0.7284 TB and in 5 years 3.642 TB of data.

Note: We need to think about the reads and writes that will happen on our system for this amount of data. This will decide what kind of database (RDBMS or NoSQL) we need to use.

3. Low-Level Design

In the context of a TinyURL system, Low-Level Design focuses on how individual components like URL generation, storage, and redirection are implemented. It defines classes, methods, and database structure to efficiently create, store, and retrieve shortened URLs.

base62_encoding

LLD

URL Encoding Techniques to create Shortened URL

To convert a long URL into a unique short URL we can use some hashing techniques like Base62 or MD5. We will discuss both approaches.

1. Base62 Encoding

If we use base62 making the assumption that the service is generating 1000 tiny URLs/sec then it will take 110 years to exhaust this 3500 billion combination.

C++ `

#include #include

std::string to_base_62(int deci) { std::string s = "012345689abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; std::string hash_str = ""; while (deci > 0) { hash_str = s[deci % 62] + hash_str; deci /= 62; } return hash_str; }

Java

public String toBase62(int deci) { String s = "012345689abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; String hashStr = ""; while (deci > 0) { hashStr = s.charAt(deci % 62) + hashStr; deci /= 62; } return hashStr; }

Python

def to_base_62(deci): s = '012345689abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' hash_str = '' while deci > 0: hash_str = s[deci % 62] + hash_str deci /= 62 return hash_str

print to_base_62(999)

JavaScript

function toBase62(deci) { const s = '012345689abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; let hashStr = ''; while (deci > 0) { hashStr = s[deci % 62] + hashStr; deci = Math.floor(deci / 62); } return hashStr; }

`

2. MD5 Encoding

MD5 also gives base62 output but the MD5 hash gives a lengthy output which is more than 7 characters.

Efficient Database Storage & Retrieval of TinyURL

Let's discuss the mapping of a long URL into a short URL in our database:

1. Using **Base62 Encoding

Assume we generate the Tiny URL using base62 encoding then we need to perform the steps given below:

This technique works with one server very well but if there will be multiple servers then this technique will create a race condition.

2. **Using MD5 Approach

This approach saves some space in the database

3. Using Counter Approach

Using a counter is a good decision for a scalable solution because counters always get incremented so we can get a new value for every new request.

Single server approach:

4. High-level Design

High-Level Design defines the overall architecture of a URL shortening system, including components like API servers, databases, and caching layers. It shows how these components interact to generate short URLs and handle redirection efficiently.

420851556

HLD

1. User Interface / Clients

Users enter a long URL via web form or API and receive a shortened link.

2. Application Server

Handles core logic like key generation, storage, and redirection.

3. Load Balancer

Distributes incoming traffic across multiple system components.

4. Database

Stores persistent URL mappings and ensures scalability.

5. Caching

Improves performance by storing frequently accessed data.

6. Cleanup Service

Maintains system efficiency by removing unused data.

7. Redirection

Handles user requests by mapping short URLs to original URLs.

8. Analytics

Tracks and analyzes user interactions.

9. Security

Protects the system from malicious activities.

5. Database Design

Explore some of the choices for System Design of Databases of URL Shortner:

You may have to use consistent hashing to balance the traffic and DB queries in the case of RDBMS and which is a complicated process. So to handle this amount of huge traffic on our system relational databases are not fit and also it won't be a good decision to scale the RDBMS.

**NoSQL Database:

6. Caching and Load Balancing in URL Shortening service

Caching and load balancing play a critical role in handling high traffic and ensuring fast response times in a URL shortening system. They help reduce database load and distribute user requests efficiently across servers.

1. Caching

Caching is used to store frequently accessed URL mappings so that redirection can happen quickly without hitting the database every time.

2. Load Balancing

Load balancing distributes incoming traffic across multiple servers to ensure high availability and reliability.