URL Shortener

Link Shortening and Redirection Service

Design the classes of a URL shortener: generate unique short codes, accept custom aliases safely, resolve codes with expiry and disabling, and count clicks, with swappable code generation.

Overview

A URL shortener turns a long address into a short code, such as https://sho.rt/1a2B, and sends anyone who opens the short link to the original address. The low-level design question is about the objects inside one service: how codes are made, how two writers are kept from taking the same code, and what happens when a link expires or its owner turns it off. The distributed version (many servers, a database, caches) is a system design question; here it appears only in the follow-ups.

The interviewer listens for: a code generator you can swap (counter or random), one atomic 'insert if absent' that every writer goes through, a clear answer on 301 versus 302, and the arithmetic of how many codes a given length gives you.

Scoping the problem

Six questions shape the design, and each one points at a part of the code that would change.

  • Must codes be hard to guess? Counter codes are sequential and guessable. Ask, and offer both behind one interface.
  • Custom aliases? An alias is a second way to claim a code, and it must not race. The usual answer is yes, validated, with reserved words refused.
  • Same URL shortened twice: same code or a new one? The same code needs a reverse index, per owner or global. The usual answer is a new code, with dedupe as a follow-up.
  • Expiry and disabling? Then resolve() needs more than found or not found. The usual answer is yes: expired or disabled means gone.
  • Analytics? They add writes on the hot read path. The usual answer is a click count per code.
  • Which redirect status? A 301 response may be cached by browsers, so the answer is 302 when clicks must be counted.

Functional requirements

  • shorten(url, owner, now, alias=None, ttl=None) returns a new code; only http and https URLs are accepted
  • Custom aliases: 3 to 30 characters from letters, digits, dash and underscore; reserved words refused; a taken alias raises AliasTaken
  • resolve(code, now) returns the URL and counts a click; unknown codes raise NotFound; expired or disabled links raise Gone
  • disable(code, owner) turns a link off; only its owner may do it
  • clicks(code) returns the number of successful resolves

Non-functional requirements

  • No two links ever share a code, even under concurrent writes
  • resolve is an O(1) dictionary lookup
  • Code generation is swappable (counter or random) without changing Shortener
  • Time is injected, so expiry is exact in tests

Interview questions

  • Counter, random or hash of the URL: which generator would you use?
  • 301 or 302 for the redirect?
  • How would you generate unique counter codes across many servers?
  • Suppose redirects far outnumber new links. Where does the time go and what do you change?

Sign up free to read the full topic

More Low-Level Design topics

Start free on Cariara · Coding problems · System design studio