OfferGenie
All Questions

Design URL Shortener

MetaTechnicalDifficulty: Medium
Share on

Ready to answer it out loud?

Run a mock interview on this exact question and get instant AI feedback.

Practice this question

Question Explain

Design a URL shortening service like TinyURL.

Requirements:

  • Convert long URL to short URL
  • Support custom short URLs
  • Handle high traffic
  • Analytics tracking
  • URL expiration

Example: Long URL: https://www.example.com/very/long/path Short URL: http://tiny.url/abc123

Answer Example

URL Shortener System Design:

  1. Architecture Components:

    • Load Balancer
    • Application Servers
    • Database (Primary-Secondary)
    • Cache Layer (Redis)
    • Analytics Service
  2. Core Services:

interface URLShortener {
  shorten(longUrl: string): string;
  redirect(shortUrl: string): string;
  customize(longUrl: string, customPath: string): string;
}

class URLService implements URLShortener {
  private readonly baseChars: string;
  private readonly redis: Redis;
  private readonly db: Database;

  async shorten(longUrl: string): Promise<string> {
    const id = await this.generateUniqueId();
    const shortPath = this.encode(id);
    await this.store(shortPath, longUrl);
    return this.baseUrl + shortPath;
  }
}
  1. Database Schema:
CREATE TABLE urls (
  id BIGSERIAL PRIMARY KEY,
  short_path VARCHAR(10) UNIQUE,
  long_url TEXT NOT NULL,
  user_id UUID,
  created_at TIMESTAMP,
  expires_at TIMESTAMP,
  click_count INTEGER DEFAULT 0
);
  1. Key Features:

    • Base62 encoding
    • Collision handling
    • Rate limiting
    • Analytics tracking
    • Cache management
  2. Scalability:

    • Horizontal scaling
    • Database sharding
    • CDN integration
    • Cache invalidation

Company Context (Meta):

  • Focus on scalability
  • Real-time analytics
  • Global distribution
  • Privacy considerations