Where a short code comes from
Every new link needs a seven-character code that no other link has. There are three ways to get one, and interviewers like to ask why you chose yours. A good answer compares the ways by three properties: whether collisions happen, how long the code is and whether someone else's code can be guessed.
Let's go back to the cloakroom from the API lesson, only now the tags are handed out in order and never come back into use. Then the attendant does not need to check whether a number is free: the next one has never been given out. A counter works the same way. But the cloakroom has a weakness a shortener cannot afford: your own tag tells you your neighbor's at once.
Three sources of a code
Hash of the URL
Take a hash of the long URL, for example MD5, write it in base62 and keep the first seven characters. There is one advantage: the code comes without a trip to the database.
There are two drawbacks:
- a truncated hash can be the same for different URLs, and the shorter the code, the more often, so a check and a retry are still needed before the write;
- identical URLs get the same code, although two users' links to one URL need different codes: they have different owners and expiry dates.
Random code
Seven random base62 characters. The code reveals nothing about the link, and the neighboring one cannot be guessed.
But before every write you have to check that the code is free. By the end of the fifth year 6 × 10⁹ of 3.5 × 10¹² codes are taken, about 0.17%: collisions are rare but not impossible. Making them rarer still would take a longer code. The check and the write must be one atomic operation, or two service instances will take the same code at the same moment.
Counter
Each new link gets the counter's next number, and the number is converted to base62. A counter never repeats, so two generated codes never collide and there is no need to look for a free code before the write. A code can only match a seven-character alias, and the conditional write from the API lesson catches that rare case.
Converting to base62 is the same as writing a number in decimal, only with 62 digits instead of 10: 0–9, a–z, A–Z. The number 61 is written as Z, the number 62 as 10. The code is padded with zeros on the left up to seven characters: 0000010.
A larger number is converted by dividing by 62 with a remainder, the way decimal divides by 10. Take 10,000:
- 10,000 = 161 × 62 + 18, remainder 18, which is
i. - 161 = 2 × 62 + 37, remainder 37, which is
B. - 2 = 0 × 62 + 2, remainder 2, which is
2.
The remainders read from the bottom up: 2Bi, or 00002Bi with the zeros on the left.
Codes are never reused
A link may expire or be deleted, but its code still never comes back into use. An old short link printed in someone's article must answer "not found", not lead to somebody else's page. A counter gives this property for free: it only moves forward.
Ranges of 1,000
If every link-service instance went to the counter for every code, the counter would become a bottleneck and a single point of failure. So the design adds a range allocator service:
- A link-service instance asks for the next range.
- The allocator moves the counter forward by 1,000 in one atomic
UPDATE … RETURNINGand learns where the range starts. - The instance hands out these 1,000 values itself, without going to the database.
The counter database now gets one write per 1,000 links instead of one per link. If an instance restarts, the rest of its range is lost, and that does no harm: there are hundreds of times more codes than needed.
Build this part of the diagram yourself.
The link service creates 40 links a second, 400 at peak. Each needs a 7-character code that matches no other, even without a check before the write. Add where the service gets its codes from, and connect the blocks.
Shuffling against enumeration
Sequential codes reveal the order: 0000abc is followed by 0000abd, and anyone can walk through the neighboring links. The requirements forbid this.
So before base62 the counter value is shuffled by a fixed one-to-one transformation. The simplest is (n × M + C) mod 62⁷, where the multiplier M is odd and not divisible by 31: 62⁷ = 2⁷ × 31⁷, and M must share no factor with it. Then different n always give different results, there are still no collisions, and neighboring counter values land far apart.
A tiny example shows it at once. Say there are only 10 codes, M = 3 and C = 1. Then the counter values 0, 1, 2, 3 and 4 turn into 1, 4, 7, 0 and 3: none repeats, and neighboring counter values no longer sit next to each other.
Tell the interviewer honestly that this is not encryption. The order is hidden, but creating two links in a row and subtracting their codes is enough to learn M, and then enumeration works again. Stronger answers are a Feistel network or a block cipher with a secret key on top of the counter: they are one-to-one too, but cannot be reversed without the key.
Common mistake
Taking the first seven characters of a hash and writing them without a check. Two different URLs will one day give the same prefix, the second write will overwrite the first, and someone's old link will lead to another URL. If you choose a hash, a collision check and a retry are mandatory.
Key points
- A hash and a random code need a collision check and a retry, while counter codes never collide with each other.
- Seven base62 characters hold 3.5 × 10¹² codes against the 6 × 10⁹ needed, and codes are never reused.
- Instances take codes in ranges of 1,000: one counter write per thousand links.
- A one-to-one shuffle hides the order of codes, but it is not encryption.