In this lesson · topic 1 of 7
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