KatInterview

Design a Link-Shortening Service with Click Tracking

System DesignModel answer
HardModel answerSystem Designdistributed-systemscachingstream-processing

Last updated

Design a distributed URL shortener handling billions of daily redirects with sub-50ms latency and decoupled, accurate click counting.

Link-Shortening Service with Click Analytics

Build a service that converts long URLs into compact codes and tracks how often each link is used.

Functional Requirements

  1. Shorten: Accept a long URL (and an optional custom alias) and return a short URL.
  2. Redirect: Resolve a short code to its original URL and send the visitor there.
  3. Analytics: Return the total number of times a given short link has been followed.

Non-Functional Requirements

ConcernTarget
Write throughput100 million new links per day
Redirect throughput1 billion redirects per day, with bursty spikes
Redirect latencyp99 under 50 ms
Click-count accuracyAccurate; eventual consistency is acceptable
AvailabilityHigh — traffic spikes must not cause outages

What to Cover

  • API: Define the REST endpoints for creating a link, redirecting, and fetching stats.
  • Data model: Schema for the short-to-long mapping and click storage.
  • Architecture: Database choice (SQL vs. NoSQL), unique ID generation with no central-per-request bottleneck, and how click counting is kept off the critical redirect path.
  • Trade-offs: Compare counter strategies (synchronous write, Redis INCR, log-based aggregation) and explain how you handle hot keys — a single viral link receiving a disproportionate share of traffic.

Hints

Hint 1

Start by separating two paths: the latency-critical redirect path and the write-heavy analytics path. Ask yourself whether these paths should share any infrastructure at all.

Hint 2

Think carefully about your HTTP status code for redirects. 301 and 302 behave differently in browsers — one of them will silently break your click counts.

Hint 3

For ID generation, consider how range-based block allocation lets each server mint codes locally with zero per-request coordination, and how you can obscure the sequential nature of the resulting codes.