Design a URL Shortener like Bit.ly
Design a URL Shortener like Bit.ly
ly
1. Design a URL Shortener like Bit.ly
1. Requirements
Functional Requirements
● Shorten a given URL (e.g., https://example.com/long-url → https://
short.ly/abc123).
● Redirect users to the original URL when they visit the short link.
● Track analytics like click count, geographic location, and timestamp.
● Allow users to create custom short links (optional).
● Handle expiration of short links (optional).
Non-Functional Requirements
● High availability and low latency.
● Scalability to handle millions of short links.
● Fault tolerance and data consistency.
2. High-Level Design
We need the following components:
. API Server – Handles requests to shorten URLs and redirect users.
. Database – Stores mappings between short and long URLs.
. Cache (Redis) – Speeds up redirection by caching URL mappings.
. Load Balancer – Distributes traffic across multiple servers.
3. Database Schema
A simple table can be used:
id short_url long_url created_ expiry_da click_cou
at te nt
1 abc123 https:// 2025-03- NULL 1024
4
example.c 27
3
om/long
2
2 xyz789 https:// 2025-03- 2025-06- 512
1
another- 27 01
site.com
• short_url is the unique key.
● long_url is the original URL.
● expiry_date helps remove old links.
● click_count is used for analytics.
5. Handling Redirections
● When a user visits https://short.ly/abc123:
○ Look up abc123 in the cache (Redis).
○ If found, redirect to the long URL.
○ If not found, query the database, cache the result, and redirect.
7. Security Considerations
● Prevent abuse: Limit requests per user/IP.
● Blacklist malicious sites: Check against a database of harmful
domains.
● HTTPS enforcement: Avoid insecure redirections.