Swiftorial Logo
Home
Swift Lessons
AI Tools
Learn More
Career
Resources

System Design FAQ: Top Questions

3. How would you design a URL Shortener (like Bitly)?

A URL Shortener converts long URLs into compact, unique short codes (e.g., https://my.ly/abc123) and redirects users to the original link when accessed. It must be fast, globally available, and resilient.

๐Ÿ“‹ Requirements Gathering

  • Functional: Generate short URLs, redirect to long URLs, track analytics
  • Non-Functional: High availability, low latency, short code uniqueness

๐Ÿ”ค Short Code Generation Approaches

  • Hashing: MD5/SHA-256 of long URL + base62 (risk: collision)
  • Random Generator: Create 6-8 char alphanumerics (check uniqueness in DB)
  • Auto-increment ID + base62: Scalable, predictable

๐Ÿ’ก Example: Base62 Encoding with Auto-Increment ID


def encode_base62(num):
    characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
    result = []
    while num > 0:
        result.append(characters[num % 62])
        num //= 62
    return ''.join(reversed(result))

# Example: ID 125 converts to "cb"
print(encode_base62(125))  # cb
        

๐Ÿ—๏ธ System Components

  • Frontend: UI or API for URL submission
  • Application Layer: Logic for encoding/decoding and redirection
  • Database: Store mapping between short code and original URL
  • Cache: Redis for fast lookups on redirects
  • Analytics Queue: Kafka or SQS for async click tracking

๐Ÿ—‚๏ธ Sample MySQL Table


CREATE TABLE urls (
    id BIGINT PRIMARY KEY AUTO_INCREMENT,
    long_url TEXT NOT NULL,
    short_code VARCHAR(10) UNIQUE,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
        

โšก Redis Lookup Cache (Python + Redis-py)


import redis

r = redis.Redis()
code = "abc123"

# Check Redis first
cached_url = r.get(code)
if cached_url:
    redirect_to(cached_url)
else:
    long_url = db_lookup(code)
    if long_url:
        r.setex(code, 86400, long_url)  # Cache for 1 day
        redirect_to(long_url)
        

๐Ÿ“ˆ Optional Feature: Click Analytics

  • Push to Kafka topic on every redirect
  • Async consumer stores in analytics DB or data lake

โ˜๏ธ Deployment Considerations

  • Stateless services behind load balancer (NGINX, ALB)
  • Read/write DB replicas for scaling
  • Geo-distributed cache (Cloudflare Workers/Redis)

๐Ÿงช Observability

  • Prometheus/Grafana for latency and cache hit ratio
  • Datadog or OpenTelemetry for tracing redirection flow

๐Ÿ“Œ Final Insight

A URL shortener might seem simple, but it teaches key concepts: encoding, caching, database scaling, and queue-based analytics. It's a classic system design interview problem.