Reference path

The URL Shortener solution is read-only study material. Use it to compare your own design against a compact reference path before starting practice or after you finish a pass.

Review the problem detail before you compare trade-offs.

High-level design

At MVP scale, the creation API validates destinations, reserves a short code, and writes the mapping. The redirect service resolves short codes quickly, reads from cache first, falls back to the mapping store, and emits click events asynchronously.

POST /links
GET /{code}
GET /links/{code}

The cache is an optimization, not the source of truth. The mapping store remains authoritative for destination URLs, aliases, expiry rules, and ownership metadata when later account stories add durable history.

Trade-offs to study

Counter-based IDs are compact and easy to reason about, but the allocator needs sharding or preallocation before it becomes a bottleneck. Random IDs reduce coordination but require collision handling. Custom aliases should stay on an explicit validation path so the default allocator stays fast and predictable.

Do not model learner practice diagrams as Mermaid in this reader. Authored Mermaid explains reference content; learner high-level-design work remains a future canvas submission flow.

Diagrams

flowchart LR
  Client[Client] --> CreateAPI[Create Link API]
  Client --> Redirect[Redirect Service]
  CreateAPI --> Abuse[Abuse Check]
  CreateAPI --> Store[(Mapping Store)]
  Redirect --> Cache[(Cache)]
  Redirect --> Store
  Redirect --> Events[Async Click Events]
  Events --> Analytics[(Analytics Sink)]
Client requests split between create-link API and redirect service; create writes mapping data after abuse checks; redirect reads cache/store and emits asynchronous analytics events.
sequenceDiagram
  participant API as Create Link API
  participant Allocator as ID Allocator
  participant Store as Mapping Store
  API->>Allocator: request short code
  Allocator-->>API: candidate code
  API->>Store: reserve code and destination
  Store-->>API: reserved mapping
The create API requests a short code, receives a candidate, then reserves the code and destination in the mapping store.