Sikau

System design · Day 8 · about 55 min

Capstone: A URL shortener

Base62 codes, sharded storage on a hash ring, and an LRU cache.

The classic interview question

"Design a URL shortener" — turn a long link into sik.au/q0U. At scale: billions of links (shard them with consistent hashing), far more reads than writes (cache them), and short unique codes (count up and write the number in base62).

Short codes

const ALPHABET = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

function toBase62(n) {
  if (n === 0) return "0";
  let code = "";
  while (n > 0) {
    code = ALPHABET[n % 62] + code;
    n = Math.floor(n / 62);
  }
  return code;
}

The rest of this lesson

  • Sizing the shortenerexplain
  • The read pathexplain
  • Count uppredict
  • Design choice to course ideamatch
  • createShortenerchallenge
  • Badge quizquiz
  • System design: complete!recap

Continue this lesson free — run real code in your browser

Continue with Google

See all 8 lessons of System design