Try
↑ ↓ move↵ openesc close
In this lesson · topic 1 of 7
  1. 1. A short link is a tiny key-value store
  2. 2. Requirements, numbers, API and data model
  3. 3. Make codes and follow them in the lab
  4. 4. Random codes meet the birthday bound
  5. 5. Counter ranges, random codes or hashes
  6. 6. Hot links, caches and 301 against 302
  7. 7. The URL shortener in review

Topic 1

A short link is a tiny key-value store

4 min read

The job

A short link service does two things. A user gives it a long address and gets back a short code. Later, anyone who opens the short link is sent to the long address.

That is a key-value store with a twist. The key is a short code the service invents. The value is the long address. Each code must point at exactly one address, forever. Two different addresses that share a code are a bug that sends people to the wrong page.

Where it gets hard

Reading is easy to scale. The same code is read many times, so a cache works well. See caching for how that behaves.

Writing is the problem. Every new link needs a code that nobody has used. If many servers create links at once, they must agree without asking one another for every code.

Short codes make this worse. A code of three characters from 62 symbols has only 0.24 millionmeasured possible values. That sounds like plenty until the service picks codes at random and the table starts to fill.

In the lab, the service made 40000 linksmeasured with random codes of three characters. 8.6 percentmeasured of all draws landed on a code that was already taken. Each of those draws had to be thrown away and tried again.

This lesson walks the whole design: the numbers, the API, the data model, the ways to make a code, the cache and the redirect. The choice of code scheme is the part interviewers care about.

Predict first

A service picks random codes of 3 characters and has made 40,000 links. Roughly what share of new draws hit a taken code?

Your progress is saved in this browser. Sign in to keep it on every device. Privacy · Terms