Asia/Kolkata
BlogJanuary 5, 2025

Designing a Scalable URL Shortener

Vaibhav Jha
This article is part of the Vaibhav Jha technical writing series on backend systems, software architecture, and core computer science topics explained with practical developer context.
Designing a Scalable URL Shortener
A URL shortener converts long URLs into short, unique links that redirect users to the original destination. Examples include bit.ly and tinyurl. While simple on the surface, building a scalable version involves careful system design.
The system should:
  • Accept a long URL
  • Generate a short unique identifier
  • Store the mapping
  • Redirect users when short URL is accessed
The system consists of:
  • API Server → handles requests
  • Database → stores URL mappings
  • Cache (Redis) → speeds up lookups
  • Load Balancer → distributes traffic
  1. User sends long URL to backend
  2. System generates a unique short code
  3. Mapping is stored in database
  4. Short URL is returned
Example:
  1. User visits short URL
  2. Server looks up original URL
  3. Redirect response is returned
  • Hash the original URL
  • Encode using Base62 (letters + numbers)
Pros:
  • Deterministic
Cons:
  • Possible collisions
  • Use database ID
  • Convert to Base62
Pros:
  • Simple
  • No collisions
Cons:
  • Predictable URLs
  • Generate random 6–8 character string
Pros:
  • Hard to guess
Cons:
  • Needs collision handling
Basic schema:
  • id
  • short_code
  • original_url
  • created_at
Indexes:
  • index on short_code for fast lookup
Use Redis:
  • Store frequently accessed URLs
  • Reduce database load
  • Improve latency
Most traffic is read-heavy (redirects).
  • Cache hot URLs
  • Use read replicas
Distribute incoming requests across multiple servers.
  • Sharding by hash of short_code
  • Partitioning large datasets
If generated short code already exists:
  • Regenerate new code
  • Or append randomness
Optional features:
  • Expiry time for links
  • Background jobs to delete old entries
Track:
  • Number of clicks
  • User location
  • Device type
Store in separate analytics system to avoid slowing core service.
  • Ensuring uniqueness of short URLs
  • Handling high read traffic
  • Maintaining low latency
  • Avoiding database bottlenecks
  • Rate limiting to prevent abuse
  • Spam detection
  • Secure redirects
  • Monitoring and logging
A URL shortener is a classic example of a system that looks simple but requires thoughtful design to scale efficiently. It combines database design, caching, and distributed systems to handle large volumes of traffic reliably.
Share this post: