Skip to content
Low Level Design Mastery Logo
LowLevelDesign Mastery

Design a URL Shortener Service

Design a URL Shortener Service that converts long URLs into shorter, unique URLs with redirection, expiration, and analytics.

Design a URL Shortener Service that converts long URLs into shorter, unique URLs. The system should handle URL shortening, redirection, expiration management, and analytics tracking. The design should support different ID generation strategies, flexible storage mechanisms, and decoupled analytics tracking using appropriate design patterns.

In this problem, you’ll design a service that maps long, cumbersome URLs to short, easy-to-share strings, while tracking usage analytics and enforcing expiration dates.


Design a service that creates unique aliases for long URLs and redirects users to the original destination with minimal latency.

Functional Requirements:

  • URL Shortening: Convert long URLs into unique, shortened codes.
  • Redirection: Redirect from a short code back to the original long URL.
  • Custom Aliases: Support user-provided short codes while ensuring global uniqueness.
  • Expiration: Set optional expiration times; expired codes should no longer redirect.
  • Analytics: Track metrics like click counts and access timestamps for each URL.
  • Analytics Querying: Allow retrieving analytics data for specific shortened URLs.
  • Swappable Components: Switch ID generators and storage strategies at runtime.

Non-Functional Requirements:

  • Thread Safety: Handle concurrent shortening and redirection requests without data corruption.
  • Scalability: Design for high availability and low redirection latency.
  • Extensibility: Use OOD principles to add new generators or storage sinks easily.
  • Uniqueness: ID generation must strictly minimize collisions and ensure unique codes.
  • Reliability: Background tasks like expiration cleanup shouldn’t block core operations.

The service coordinates between an IDGenerator, a StorageEngine, and an AnalyticsQueue.

Diagram
classDiagram
    class ShortenerService {
        -IDGenerator idGen
        -StorageStrategy storage
        +shorten(longUrl) String
        +resolve(shortCode) String
    }
    
    class IDGenerator {
        <<interface>>
        +generate() String
    }
    
    class Base62Generator {
        -long counter
        +generate()
    }

    class URLMapping {
        -String shortCode
        -String longUrl
        -DateTime expiry
        -long clickCount
    }

    ShortenerService --> IDGenerator
    ShortenerService --> URLMapping
    ShortenerService o-- StorageStrategy

Diagram

How do you ensure every ID is unique without checking the database every time?

Solution: Use Base62 Encoding on a global counter. Base62 (a-z, A-Z, 0-9) allows for $62^7$ (over 3.5 trillion) unique combinations with just 7 characters. Using a counter ensures $100%$ uniqueness and $O(1)$ ID generation.

Scanning millions of URLs to find expired ones can kill database performance.

Solution: Lazy Deletion + Background Cleanup. When a user accesses a URL, check the expiry timestamp. If it’s passed, delete it and return a 404. Additionally, run a low-priority background job once a day to prune old entries.

Database lookups for every click are slow.

Solution: Use an LRU Cache. Since 20% of URLs typically generate 80% of traffic, keeping the most popular mappings in memory (Redis or local Cache) dramatically reduces latency.


By solving this problem, you’ll master:

  • Encoding & Hashing - Shrinking data while maintaining uniqueness.
  • Scalable ID Generation - Building distributed counter systems.
  • Cache Strategies - Optimizing read-heavy workloads.
  • Persistence Logic - Swapping between SQL and NoSQL storage.

Ready to see the full implementation? Open the interactive playground to access:

  • 🎯 Step-by-step guidance through the 8-step LLD approach
  • 📊 Interactive UML builder to visualize your design
  • 💻 Complete Code Solutions in Python, Java, C++, TypeScript, JavaScript, C#
  • 🤖 AI-powered review of your design and code

After mastering URL Shortener, try these similar problems:

  • Hash Map - Designing the underlying storage mechanics.
  • Rate Limiter - Protecting your shortener from abuse.
  • Cache Manager - Advanced eviction policies for high-traffic URLs.