Requirements


Functional Requirements:


  • Shorten URL → short link
  • Redirect short link → original URL



Non-Functional Requirements:


  • Low latency (read-heavy)
  • High availability
  • Scalable (millions–billions URLs)
  • Durable storage


API Design

POST /shorten { long_url } → { short_url } GET /{short_code} → 302 redirect




High-Level Design

Client

|

API Gateway

|

+-------------------+

| |

Shorten Service Redirect Service

| |

ID Generator Cache (Redis)

| |

+-------> Database



Detailed Component Design

4.1 ID Generation

  • Use Snowflake ID or auto-increment
  • Encode → Base62
  • No collision

4.2 Database

urls( id BIGINT PK, short_code VARCHAR UNIQUE, long_url TEXT, created_at, expires_at )
  • Index: short_code

4.3 Cache (Redis)

  • Key: short_code
  • Value: long_url

Flow:

Read → Cache → DB fallback → update cache