Skip to the page

System design prompts · core

URL shortener, rate limiter and search suggestions

Three classic read-heavy prompts: a URL shortener, a distributed rate limiter and typeahead search. Each is solved with a few numbers, a cache and one careful hot spot.

The notes

What you must know

  • URL shortener: requirements first (create, redirect, optional alias and expiry). Reads far outnumber writes. API: POST /links and GET /code with a redirect and Location header. Table: code (primary key), long_url, owner_id, created_at, expires_at.
  • Codes: a counter that hands out blocks of ids to each app server (no single hot counter) turned into base62, or random codes with a unique key and retry on collision. A plain hash needs collision handling and gives the same code for the same URL. Cache-aside in Redis or Valkey for hot codes.
  • At 500 bytes a row, five years is 6 billion rows, about 3 TB. Click counts are buffered and aggregated, never UPDATE count + 1 on one row for every click. Know 301 against 302: 301 is cached by browsers, so clicks are not counted.
  • A viral link overloads one cache shard. Fix with a local cache with short TTL, key copies, and shedding analytics writes first.
  • Distributed rate limiter: clarify the key (API key, user, IP), per-endpoint rules and how exact it must be. Coarse limits at the edge, finer per-service rules in a library or sidecar.
  • Algorithms: fixed window (double burst at the boundary), sliding window log (exact, memory heavy), sliding window counter (good approximation), token bucket (bursts up to bucket size). Shared state in Redis with an atomic Lua script or INCR plus EXPIRE, one key per client and window. Respond with 429, Retry-After and limit headers.
  • Search suggestions: under about 50 ms at p99. The app debounces (150 to 300 ms) and waits for 2 or 3 characters; popular prefixes are cached at the edge. A trie with top suggestions at each node, or a prefix map in a key-value store, or Elasticsearch edge n-grams.
  • Build pipeline: logs to Kafka, count with time decay, build new tries and swap atomically, plus a small real-time layer for trending. Shard by prefix range and watch skew. A blocklist is applied when building. Never use SQL LIKE on every keystroke.

The red pen

Common traps: mistake → fix

  • Mistake: Counters in each server memory behind round robin work

    Fix: each server counts a part; the limit is wrong.

  • Mistake: One global auto-increment counter in one database

    Fix: a single hot point.

  • Mistake: Update the trie on every search

    Fix: use a batch or stream pipeline.

  • Mistake: A hash of the URL is perfect

    Fix: collisions and duplicates need handling.

Easy to hard

Questions interviewers ask, easy to hard

  1. Design a URL shortener.
  2. How do you generate short codes without collisions?
  3. Design a distributed rate limiter. Which algorithm?
  4. A viral link slows every redirect. What do you do?
  5. Design search suggestions as the user types.

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview