ArchitectureCase StudiesProduction Ready15 lessons

URL Shortener Design

Design a low-latency URL shortening service with durable mappings, analytics, and abuse protection.

Advanced Architecture

Overview

URL Shortener Design is a case-study chapter for designing a URL shortener at large scale. The objective is to move from requirements to APIs, capacity estimates, storage choices, cache strategy, scaling strategy, and failure handling.

API Design

  • POST /urls creates a short code for a long URL
  • GET /{code} redirects to the long URL
  • GET /urls/{code}/stats returns click analytics

Capacity Estimation

  • Reads usually dominate writes because redirects are frequent
  • Estimate redirect RPS, new URL writes per day, mapping size, and click event volume
  • Design redirect path for very low p99 latency

Database Selection

  • Use a key-value or relational table keyed by short code
  • Store code, long_url, owner_id, created_at, expiry, and status
  • Use append-only analytics storage for click events

Cache Layer

  • Cache code to long_url mappings in Redis
  • Use CDN or edge cache for extremely hot public redirects when safe
  • Expire or invalidate cache when a URL is disabled

Load Balancing

  • Route redirects through global load balancers
  • Keep redirect services stateless
  • Use health checks to remove unhealthy instances

Sharding Strategy

  • Shard by short code hash
  • Avoid sequential code ranges that create hot partitions
  • Keep analytics partitioned by code and time

Replication Strategy

  • Replicate mapping data for high read availability
  • Use asynchronous analytics pipelines for click events
  • Route critical admin reads to primary if immediate consistency is required

Failure Handling

  • Serve cached mappings if the database is briefly unavailable
  • Fail closed for disabled or abusive links
  • Buffer analytics events when the stream is slow

Real Production Considerations

  • Abuse detection and phishing takedowns are essential
  • Redirect latency is the product experience
  • Analytics accuracy can be eventually consistent

Advanced Architecture

Problem Statement

Design a URL shortener so users get a fast and reliable experience while writes, reads, media, fanout, location updates, or stream metadata grow independently. The design must explain the first practical architecture and the path to scale it without losing operational control.

Advanced Architecture

Functional Requirements

  • Expose APIs for the core user workflow
  • Persist authoritative business data
  • Serve read-heavy views with acceptable freshness
  • Support asynchronous background processing
  • Provide moderation, abuse prevention, or operational repair paths where needed

Advanced Architecture

Non Functional Requirements

  • Low p99 latency for interactive reads
  • High availability during regional or dependency failures
  • Durable writes and recoverable workflows
  • Horizontal scaling for hot paths
  • Observable request traces across services

Advanced Architecture

High Level Architecture

The high-level architecture uses edge routing, stateless API services, domain services, durable storage, cache layers, asynchronous workers, event streams, and observability.

Core entities: ShortUrl, LongUrl, User, ClickEvent, AbuseRecord.

Advanced Architecture

Architecture Diagram

URL Shortener Design High Level Architecture
Architecture
Client | v API / Redirect Service | +-- Redis cache | +-- URL mapping DB | `-- Kafka -> Analytics workers

A senior answer should start simple, then layer in cache, queues, partitioning, replication, and failure handling as scale demands.

Advanced Architecture

Request Flow

  • Create request validates URL and user quota
  • Service generates or reserves a unique code
  • Mapping is stored in the database and optionally warmed in cache
  • Redirect request checks cache, falls back to DB, then returns 301 or 302
  • Click event is published asynchronously

Advanced Architecture

Database Design

  • Use a key-value or relational table keyed by short code
  • Store code, long_url, owner_id, created_at, expiry, and status
  • Use append-only analytics storage for click events

Advanced Architecture

Cache Design

  • Cache code to long_url mappings in Redis
  • Use CDN or edge cache for extremely hot public redirects when safe
  • Expire or invalidate cache when a URL is disabled

Advanced Architecture

Scaling Strategy

  • Route redirects through global load balancers
  • Keep redirect services stateless
  • Use health checks to remove unhealthy instances
  • Shard by short code hash
  • Avoid sequential code ranges that create hot partitions
  • Keep analytics partitioned by code and time
  • Replicate mapping data for high read availability
  • Use asynchronous analytics pipelines for click events
  • Route critical admin reads to primary if immediate consistency is required

Advanced Architecture

Bottlenecks

Advanced Architecture

Tradeoffs

Advanced Architecture

Production Notes

Advanced Architecture

Best Practices

Advanced Architecture

Interview Questions

  1. What is the first design for URL Shortener Design?

Answer: Start with API services, durable storage, cache for hot reads, asynchronous workers for heavy tasks, and a load balancer.

  1. How do you scale the design?

Answer: Identify hot reads, hot writes, large objects, fanout, and regional traffic, then add caches, queues, sharding, replicas, and regional routing deliberately.

  1. What should you discuss after drawing the diagram?

Answer: Discuss capacity, database selection, cache invalidation, sharding, replication, failure handling, observability, and tradeoffs.