Chaturmind
LearnDSASystem DesignBlogPremium
Sign inGet started
Chaturmind

Structured learning paths for engineers who want to go deep. Written by practitioners.

Learn

  • Java
  • DSA
  • System Design
  • Spring Boot
  • AI / ML

Company

  • Blog
  • Premium
  • Contact

Legal

  • Privacy Policy
  • Terms of Service

© 2026 Chaturmind. All rights reserved.

Built for engineers who want to go deep.


← System Design Interview Playbook

Interview Framework

  • The 6-Step Design Framework

10 Case Studies

  • Design a URL Shortener
  • Design Twitter / X
  • Design WhatsApp
  • Design Netflix
  • Design a Rate Limiter
  • Design a Search Autocomplete
  • Design a Distributed Cache
  • Design a Notification Service
  • Design Uber / Ride Sharing
Chaturmind
← System Design Interview Playbook

Interview Framework

  • The 6-Step Design Framework

10 Case Studies

  • Design a URL Shortener
  • Design Twitter / X
  • Design WhatsApp
  • Design Netflix
  • Design a Rate Limiter
  • Design a Search Autocomplete
  • Design a Distributed Cache
  • Design a Notification Service
  • Design Uber / Ride Sharing
HomeLearnSystem DesignSystem Design Interview PlaybookDesign Cases
✓ FreeIntermediate· 12 min read

Design: URL Shortener

Design a URL shortener like bit.ly: ID generation, hashing, redirection, analytics, and scaling.

Published April 20, 2025


Design: URL Shortener

Requirements

Functional:

  • Shorten long URLs to short 7-character codes
  • Redirect short URL → original URL
  • Optional: custom aliases, expiry, click analytics

Non-functional:

  • 100M URLs shortened/day, 10B redirections/day
  • Redirect latency < 10ms
  • 99.99% availability
  • Redirections: ~100K reads/sec (very read-heavy)

Core Algorithm: Short Code Generation

Option 1: Hash + truncate

String shortCode = Base62.encode(MD5(longUrl).substring(0, 8));
// Problem: collisions possible when truncating

Option 2: Auto-increment ID + Base62 (recommended)

long id = idGenerator.nextId(); // e.g., Snowflake ID or DB auto-increment
String shortCode = base62Encode(id);
// 62^7 = 3.5 trillion codes — sufficient

String BASE62 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
String base62Encode(long id) {
    StringBuilder sb = new StringBuilder();
    while (id > 0) { sb.insert(0, BASE62.charAt((int)(id % 62))); id /= 62; }
    return sb.toString();
}

Data Model

CREATE TABLE urls (
    id          BIGINT PRIMARY KEY AUTO_INCREMENT,
    short_code  VARCHAR(10) UNIQUE NOT NULL,
    long_url    VARCHAR(2048) NOT NULL,
    user_id     BIGINT,
    created_at  TIMESTAMP DEFAULT NOW(),
    expires_at  TIMESTAMP,
    click_count BIGINT DEFAULT 0
);

CREATE INDEX idx_short_code ON urls(short_code); -- critical for redirects

Architecture

Client
  ↓
CDN (cache popular short codes)
  ↓
Load Balancer
  ↓
[Redirect Service]  [Shorten Service]
  ↓                      ↓
[Redis Cache]      [ID Generator (Snowflake)]
  ↓                      ↓
[MySQL/DynamoDB] ←  [MySQL Write Master]
(read replicas)         ↓
                  [Analytics Queue → ClickHouse]

Redirect Flow

GET /abc123
  1. Check Redis cache: key="url:abc123"
  2. Cache hit → return 301/302 redirect
  3. Cache miss → query DB → cache result → return redirect
  4. Async: publish click event to message queue

301 vs 302:

  • 301 (Permanent): browser caches → faster for users but you lose analytics
  • 302 (Temporary): browser always calls your server → enables click tracking

Scaling Considerations

  • Read scaling: Redis cache with TTL handles 99%+ of reads
  • Write scaling: Single leader DB for writes + read replicas
  • ID generation: Zookeeper-based range allocation or Snowflake ID (timestamp + machine + sequence)
  • Custom aliases: unique constraint on short_code; return 409 if taken

Interview Tips

  1. The Base62 encoding approach is cleaner than hashing — no collision handling needed.
  2. Always mention cache hit ratio — with 80/20 rule, top 20% URLs account for 80% of traffic → small cache covers most redirects.
  3. Bloom filter: use to check if a short code exists before hitting DB.

Previous

The 6-Step Design Framework

Next

Design Twitter / X

AI Tutor

Lesson: Design: URL Shortener

Quick actions

AI responses can be inaccurate. Verify critical information.