Building TinyURL: A Deep Dive into URL Shortener System Design πŸš€πŸ”— #SystemDesign #URLShortener #TechInsights

Tarun Telang
8 min readAug 21, 2022
Photo by Med Badr Chemmaoui on Unsplash

🚨 Problem Statement

Assuming we would like to design a URL shortener service, various key considerations must be addressed. To start with, we need to decide on our service’s functionality.

🎯 Key Considerations

1️⃣ Customization πŸ› οΈ

  • Will users be able to customize their shortened URLs?
  • Alternatively, will the service generate a random string of characters for each URL?

2️⃣ Scalability πŸ“ˆ & Storage πŸ’Ύ

  • How will we store the data associated with each shortened URL?
  • Should we use a database? If so, which type would be best suited for our needs?
  • How often will the application access data?
  • Do we need to support real-time lookups?

3️⃣ User Interaction πŸ–₯οΈπŸ“±

  • How will users interact with the service?
  • Web interface 🌐
  • API ⚑

Depending on our choice, we need to design our system accordingly.

πŸ”‘ Summary

Designing a URL shortener service requires careful planning πŸ“ and consideration πŸ€” of various factors. By taking the time to define our requirements upfront, we can create a system that is both scalable βš™οΈ and secure πŸ”.

πŸŽ₯ Check out this video below for a detailed discussion on how you should approach this system design interview question:

Introduction

Implementing a tiny URL πŸ”— using the minimalistic approach ✨ shortens a long original URL into a shorter and manageable URL.

πŸ” What is a URL Shortener?

This project concerns the system design πŸ—οΈ of a URL shortener service like bit.ly, goo.gl, or tinyurl.com. We’ll examine the various design aspects πŸ”„ and trade-offs βš–οΈ in creating such a service.

A URL shortener service is a web service 🌐 that provides shortened URLs for long URLs. The shortened URL is:
βœ… Easier to remember 🧠
βœ… More convenient to share πŸ“’
βœ… Capable of tracking πŸ“Š clicks and analytics

🌎 Existing URL Shortener Services

Many popular URL shortener services exist, such as:
πŸ”Ή bit.ly
πŸ”Ή goo.gl
πŸ”Ή tinyurl.com

πŸ“Œ In this project, we will design our own URL shortener service! πŸ—οΈπŸ”—

Let’s dive in! πŸš€

πŸ” Clarifications

To ensure a well-structured URL shortener service, we need to clarify certain requirements:

πŸ”’ Privacy & Access Control

  • Can users create private URLs πŸ”‘ that only specific users can access?

πŸ“Š Analytics & Tracking

  • How many times has a short URL been used? πŸ”„
  • What were the user locations 🌍, devices πŸ“±, and referring websites πŸ”—?
  • How will we store these statistics in our system? πŸ’Ύ

πŸ›  Customization Options

  • Should users be able to choose their own custom URL instead of a randomly generated one? ✏️

πŸ“ˆ Tracking Features

  • The service should be able to track the number of clicks on each shortened URL πŸ“Š.

πŸ› Storage & Access

  • We will use a database to store all URL mappings and metadata πŸ’Ύ.
  • Data must be accessed in real-time ⏳ to ensure fast redirection.

πŸ›‘ Security Measures

  • Rate limiting ⏳ to prevent abuse and excessive API requests 🚫.
  • Implement filtering mechanisms to prevent malicious URL submissions ⚠️.

βš™οΈ User Interaction & API Support

  • Users will interact with the service primarily via an API ⚑.
  • Consider providing a web interface 🌐 for easier usability.

This ensures our service is scalable, secure, and user-friendly πŸš€.

πŸ“ User Stories / Functional Requirements

πŸš€ Minimum Viable Product (MVP)

1️⃣ Basic URL Shortening πŸ”—

As a user, I can enter a URL to shorten in the system and receive a unique shortened URL.

Details: The system should ensure that no two URLs map to the same shortened URL.

2️⃣ Redirection to Original URL πŸ”„

As a user, I can enter a shortened URL into my browser and be redirected to the original URL instantly.

πŸ›  Optional Features

3️⃣ Custom Shortened URLs ✏️

As a user, I can enter a URL along with a custom string so that the system returns a shortened URL with my chosen suffix (if available).

πŸ’‘ Other Functional User Stories

πŸ”Ή Easy Sharing πŸ“’

As a user, I want to shorten a URL quickly so that I can share it easily.

πŸ”Ή Click Tracking & Analytics πŸ“Š

As a user, I want to view statistics about how many people clicked my shortened URL to track its effectiveness.

πŸ”Ή Custom Shortcodes πŸ” 

As a user, I want to choose my own shortcode for a URL so that I can remember it more easily.

πŸ”Ή Security & Protection πŸ›‘

As a user, I want the service to be secure so that my data is not compromised.

πŸ”Ή High Speed & Performance ⚑

As a user, I want the service to be fast so that I can shorten and share my URLs instantly.

πŸ”Ή Free Service πŸ’°

As a user, I want the service to be free to use so that I don’t have to pay anything.

βš™οΈ Non-Functional Requirements (NFRs)

For our URL Shortener Service πŸ”—, we must ensure it is fast, reliable, and resilient πŸ’ͺ. Below are the key non-functional requirements (NFRs):

⚑ 1. Low Latency

  • URL redirection πŸ”„ should happen in real-time ⏳ with minimal latency.
  • Users should experience instantaneous redirects when accessing a shortened URL.

πŸ”„ 2. High Availability & Fault Tolerance

  • The system should be highly available (99.99% uptime) ⏲️.
  • It should remain operational even if a server crashes 🚨 due to:
  • Power outages ⚑
  • Hard disk failures πŸ’Ύ
  • Network issues 🌐
  • A load balancer πŸ—οΈ should distribute traffic across multiple instances for seamless failover.

πŸ”— 3. Strong Data Consistency

  • Once a URL is shortened, it must always resolve consistently βœ….
  • Given a shortened URL, the system should always return the same original URL, regardless of when it is requested.

πŸ›‘ 4. No Downtime / Resilient Architecture

  • The service should never go down. πŸš«πŸ“‰
  • Even if some machines fail, the system should continue functioning without interruption.
  • The only failure scenario would be if all servers go down at once, which is highly unlikely.

Ensuring these NFRs will make the URL Shortener Service scalable, reliable, and efficient πŸš€πŸ”—.

πŸ“Š Capacity Estimation & Constraints

To ensure our URL Shortener Service πŸ”— is scalable and efficient, we need to estimate CPU, storage, bandwidth, and caching requirements.

πŸ–₯️ CPU (Compute) Requirements

  • The system is read-heavy πŸ“–, with a 100:1 ratio between reads and writes.
  • Retention period: URLs will be stored for 5 years.
  • Traffic estimates:
  • 500 million new URLs πŸ”— shortened per month πŸ“….
  • Redirections per month = 100 * 500M = 50 billion πŸ”„.
  • Queries per second (QPS) =
    πŸ“Œ 500 million / (30 days * 24 hours * 3600 sec) = 20,000 requests/sec

πŸ’Ύ Storage Requirements

  • Total URLs stored = 500M * 5 years * 12 months = 30 billion URLs πŸ“¦.
  • Storage per URL (including metadata) = 1 KB.
  • Total storage required = 30 billion * 1 KB = 30 TB.

🌐 Network Bandwidth Estimates

Write Requests (Input):

  • 200 * 1 KB = **200 KB/s**

Read Requests (Output):

  • 20,000 * 1 KB/sec = **20 MB/s**

⚑ Cache Estimation (80–20 Rule)

πŸ“Œ 80% of traffic is generated by 20% of the URLs.

  • We use LRU (Least Recently Used) caching πŸ—οΈ with a Linked HashMap to store frequently accessed URLs.

Total cacheable requests per day:

  • 20,000 * 3600 sec * 24 hours = 1.7 billion πŸ”„

Cache storage required:

  • 1.7B * 20% * 1 KB = **340 GB**

πŸ’‘ Since many requests may be duplicates, actual cache usage might be lower.

🌐 API Design for URL Shortener

To enable efficient URL shortening and redirection, we provide a simple RESTful API πŸ› οΈ.

πŸš€ 1. Create a Short URL

πŸ“Œ Endpoint:

POST /api/link HTTP/1.1
Host: urlshrtnr.taruntelang.repl.co
Content-Type: application/x-www-form-urlencoded

πŸ“Œ Request Body:

{
"link": "https://www.linkedin.com/in/taruntelang/"
}

πŸ“Œ Response:

{
"shortURL": "6U8Ahx9"
}

πŸ’‘ Example:

πŸ”— 2. Retrieve the Original URL

πŸ“Œ Endpoint:

GET /6U8Ahx9 HTTP/1.1
Host: urlshrtnr.taruntelang.repl.co

πŸ“Œ Response:

{
"originalURL": "https://www.linkedin.com/in/taruntelang/"
}

πŸ’‘ Behavior:

  • The user enters https://urlshrtnr.taruntelang.repl.co/6U8Ahx9 in the browser.
  • The system redirects to the original URL instantly.

🎯 Why Short Links?

βœ… Easy to share via messages, tweets, or print media πŸ“πŸ“’.
βœ… Reduces clutter in social media posts 🌎.
βœ… Tracks analytics for marketing performance πŸ“Š.
βœ… Hides affiliate links for better engagement 🎭.

This API-first approach ensures a simple yet powerful URL shortener service πŸš€πŸ”—.

βš™οΈ System Components

1️⃣ Application Server πŸ–₯️

  • Handles URL shortening, redirections, analytics tracking, and API requests.
  • Should be stateless, allowing easy horizontal scaling.

2️⃣ Cache (e.g., Redis / Memcached) ⚑

  • Stores frequently accessed URLs to reduce database load.
  • Uses LRU (Least Recently Used) eviction policy.

3️⃣ Database (NoSQL β€” e.g., Cassandra, DynamoDB, MongoDB) πŸ’Ύ

  • Scalable and optimized for high read traffic.
  • Stores URL mappings and user-related data.
  • Replication & Sharding for improved performance.

πŸ“Š Telemetry & Analytics

Tracking user interactions helps improve performance and understand user behavior. The system should capture:

πŸ“ User Tracking Data:

  • 🌍 Country of the visitor
  • πŸ“… Date & Time of access
  • πŸ”— Referrer URL (where the user clicked from)
  • 🌐 Browser type (Chrome, Firefox, etc.)
  • πŸ“± Platform (Mobile, Desktop, Tablet, etc.)

πŸ“Œ Use Case:

  • πŸ“Š Marketers can analyze link engagement to optimize campaigns.
  • πŸ›‘ Helps detect bot traffic or suspicious activity.
  • πŸš€ Provides insights for scaling infrastructure based on traffic patterns.

πŸ₯ Health Check System

πŸ”Ή Ensures that the service remains available and performs optimally πŸ’‘.

βœ… Periodically checks system health by monitoring:

  • Database connectivity πŸ”„
  • Cache performance ⚑
  • API response times ⏳
  • Server resource utilization (CPU, RAM) πŸ“Š

πŸ“Œ Actionable Insights:

  • Auto-restart failing services πŸš‘
  • Alert engineers if an anomaly is detected 🚨
  • Log failures for debugging πŸ“

βš– Load Balancer

A Load Balancer πŸ”€ is critical for distributing traffic across multiple servers to:

βœ… Prevent server overload 🚦
βœ… Ensure high availability (If one server fails, traffic is routed to another)
βœ… Optimize response times ⚑

πŸ“Œ Common Load Balancer Options:

  • Software-based: Nginx, HAProxy πŸ–₯️
  • Cloud-based: AWS ELB, Google Cloud Load Balancer ☁️

πŸ”Ή How It Works:

  1. User requests a shortened URL πŸ”—
  2. The load balancer directs the request to an available server 🎯
  3. The server fetches the original URL from the database or cache πŸ—οΈ
  4. The system redirects the user to the original website 🌐

πŸš€ Final Thoughts

By integrating Telemetry, Health Checks, and Load Balancing, our URL Shortener becomes:
βœ” Reliable (Auto-failover, Monitoring)
βœ” Scalable (Handles millions of requests per second)
βœ” Optimized (Efficient resource utilization)

This modular approach ensures high performance while keeping the system resilient and user-friendly πŸ”₯πŸ”—.

πŸ“‚ Database Schema

1️⃣ URL Table πŸ”—

UrlId: NUMBER <PRIMARY KEY>
OriginalURL: VARCHAR
CreationDate: DATETIME
ExpiryDate: DATETIME
UserId: NUMBER <FOREIGN KEY>

2️⃣ User Table πŸ‘€
UserId: NUMBER <PRIMARY KEY>
Name: VARCHAR
Email: VARCHAR
CreationDate: DATETIME
LastLoginDate: DATETIME

πŸ’‘ Why NoSQL?

  • We need to store billions of rows πŸ“Š.
  • Easy horizontal scaling βš–οΈ.
  • Minimal relationships between entities.

πŸ”’ Short URL Generation

  • Shortened URL format: 7-character string using Base64 encoding πŸ”‘.
  • Character set: [a-z A-Z 0–9 - _] (Base64 URL variant).
  • Ensures uniqueness by checking for collisions before storing.

πŸ“Œ Key Takeaways

βœ” NoSQL for scalability πŸ“ˆ.
βœ” Cache for performance optimization ⚑.
βœ” Stateless architecture for easy scaling πŸš€.
βœ” 7-character Base64 encoding ensures compact short URLs πŸ”—.

This setup ensures a fast, scalable, and reliable URL shortening system πŸ”₯.

--

--

Tarun Telang
Tarun Telang

Written by Tarun Telang

Prolific Author, Engineering Leader, Software Architect

No responses yet