3 Topics
Chapter•100 Minutes

Capstone Project 1: Design a Production URL Shortener

Apply requirements, capacity estimation, APIs, storage, caching, abuse controls, and reliability to a complete URL-shortening service.

Requirements, APIs, and Capacity Estimates

Core functional requirements: create a short URL, redirect a code to its destination, optionally set expiration, and allow authenticated owners to inspect or disable links. Non-functional goals may include low redirect latency, high read availability, abuse resistance, and durable mappings.

Define POST /v1/links, GET /{code}, and owner-management endpoints. Use an opaque code and store destination, owner, creation time, expiry, and status. Estimate create and redirect rates separately; redirects are usually much more frequent than link creation.

Choose a code-generation strategy that avoids collisions at the expected scale. Random codes need collision detection and retry; a counter-based generator needs coordination and may expose creation order. The code length should be based on the alphabet size and collision expectations, not guessed casually.

Key Takeaways
  • Redirect reads and link creation have different workloads.
  • Define expiration, ownership, and abuse requirements early.
  • Code generation needs a collision and enumeration strategy.
Knowledge Check

Why should click analytics usually be decoupled from the URL redirect path?

Architecture, Data Model, and Redirect Path

A baseline includes a stateless API, durable mapping database, cache, rate limiter, and analytics pipeline. On creation, validate the URL, apply abuse policy, generate a code, persist the mapping, and return the short URL. On redirect, validate the code, check cache, read the mapping on a miss, verify status/expiry, and return a redirect response.

Cache popular mappings with a TTL that respects link expiration and disable operations. Invalidation is important when a link is disabled or destination changes. Analytics should be asynchronous so a slow event sink does not block redirects; accept that analytics may be delayed or sampled if the product allows it.

Keep the mapping store authoritative. If a cache is unavailable, redirects should fall back to the database with protection against overwhelming it. Add rate limits, bot detection, malware/phishing checks, and controls against code enumeration.

URL shortener request paths
Key Takeaways
  • Keep redirects fast and analytics asynchronous.
  • Cache invalidation must respect expiry and link disablement.
  • Protect the service against malicious URLs and enumeration.

Failure Modes, Security, and Scaling Evolution

Potential failures include database unavailability, cache stampedes, code collisions, analytics backlog, malicious destinations, and accidental deletion. Define behavior for each. For example, if analytics is down, redirecting may continue while events are buffered or dropped according to product requirements.

At higher scale, add read replicas or partition mappings by code hash if measured bottlenecks justify them. Use CDN or edge caching only when revocation and freshness requirements permit. Add regional replicas and a recovery plan if global availability becomes necessary.

Validate with load tests covering hot codes, random codes, expired links, disabled links, and cache misses. Monitor redirect p95/p99 latency, error ratio, cache hit rate, database saturation, queue age, abuse rates, and collision retries.

Key Takeaways
  • Plan graceful degradation for noncritical analytics.
  • Scale based on measured bottlenecks and explicit consistency needs.
  • Test security and edge cases alongside throughput.