Skip to content

URL Shortener (like bit.ly)

Quick Reference Guide for System Design Interviews


Problem Statement

Design a URL shortening service that creates short aliases for long URLs. Users should be able to create short URLs and be redirected to the original URL when accessing the short link.


Requirements

Functional Requirements

  • Shorten a long URL to a short URL
  • Redirect short URL to original URL
  • Optional: Custom short URLs
  • Optional: Expiration time
  • Optional: Analytics (click count, location, referrer)

Non-Functional Requirements

  • Availability: 99.99% (redirects are critical)
  • Latency: < 100ms for redirect
  • Scalability: Handle 100M URLs, 10B redirects/month
  • Durability: URLs should never be lost

Back of Envelope Estimation

Capacity Estimation


High-Level Architecture

URL Shortener Architecture


Short URL Generation

Approach 1: Base62 Encoding of Counter

Counter-Based Generation

Approach 2: Hash-Based

Hash-Based Generation

Key Generation Service


Database Schema

-- URL Mapping Table
CREATE TABLE url_mappings (
    short_url       VARCHAR(10) PRIMARY KEY,
    long_url        TEXT NOT NULL,
    user_id         UUID,
    created_at      TIMESTAMP NOT NULL,
    expires_at      TIMESTAMP,
    click_count     BIGINT DEFAULT 0,

    INDEX idx_user_id (user_id),
    INDEX idx_created_at (created_at)
);

-- For custom URLs
CREATE TABLE custom_urls (
    custom_alias    VARCHAR(50) PRIMARY KEY,
    short_url       VARCHAR(10) REFERENCES url_mappings(short_url),
    user_id         UUID NOT NULL,
    created_at      TIMESTAMP NOT NULL
);

-- Analytics (separate table or time-series DB)
CREATE TABLE click_events (
    id              UUID PRIMARY KEY,
    short_url       VARCHAR(10),
    timestamp       TIMESTAMP NOT NULL,
    ip_address      INET,
    user_agent      TEXT,
    referrer        TEXT,
    country         VARCHAR(2),

    INDEX idx_short_url_time (short_url, timestamp)
);

Caching Strategy

Caching Strategy


Redirect Types

Redirect Types


API Design

API Endpoints


Analytics Pipeline

Analytics Pipeline


Data Partitioning

Database Sharding


Security Considerations

Security Considerations


Interview Discussion Points

  1. How do you ensure unique short URLs?
  2. Pre-generated key service (recommended)
  3. Counter with base62 encoding
  4. Hash with collision handling

  5. How do you handle high read traffic?

  6. CDN caching for redirects
  7. Redis cache for hot URLs
  8. Read replicas

  9. 301 vs 302 redirect?

  10. 302 recommended for analytics
  11. 301 for permanent, SEO-focused use

  12. How do you handle URL expiration?

  13. expires_at column
  14. Background job for cleanup
  15. Check on redirect

  16. How do you scale writes?

  17. Database sharding
  18. Pre-generated keys
  19. Async analytics

Quick Reference for System Design Interviews