Making short codes
After this: Turn a counter into a base62 code by hand and in code, and show that six characters are enough for 6 billion links.
On this page · 7 sections · 3 checks
Every new link needs a code that is short and that no other link has. The table from lesson 2 refuses a repeated code, but something still has to choose the code in the first place. Where does it come from?
Count, then write the number short
The simplest source of unique numbers is a : a number that goes up by 1 for each new link. If the counter stands at 125, the next three links get 125, 126 and 127. No number is handed out twice, so no two links clash.
The catch is length. Written with the digits 0 to 9, the 6 billionth link needs ten characters. The fix is to use more symbols for each place.
Base 62
Ordinary numbers use ten symbols, and each place is worth ten times the place on its right: 125 is 1 × 100 + 2 × 10 + 5. With more symbols, each place is worth more. uses 62 symbols: 0 to 9, then a to z, then A to Z. So a stands for 10, z for 35, A for 36 and Z for 61, and each place is worth 62 times the place on its right.
Turn 125 into base 62 by hand:
- 125 % 62 = 1. The remainder is the last character:
1. - 125 divided by 62, with the remainder dropped, is 2. That is what is left to write.
- 2 % 62 = 2. Put the character
2in front:21. - 2 divided by 62 is 0, so there is nothing left. The code is
21.
Check it the other way: 2 × 62 + 1 = 125. In the same way 61 is Z, the last one-character code, and 62 is 10, the first two-character one.
The code
The function does those steps in a loop, and builds the code from right to left. Step through it for 1,000,000:
- Run the programRun it and step through. Each time round the loop, watch two boxes: code gains one character at the front, and n becomes n divided by 62 with the remainder dropped. Count how many times the loop runs before n reaches 0.
ALPHABET = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
def base62(n): if n == 0: return "0" code = "" while n > 0: code = ALPHABET[n % 62] + code n //= 62 return code
print(base62(1_000_000))run the program to see its boxes
nothing printed yetPress Run it, then step through the program one line at a time.
- lines
- 0
- prints
- 0
- calls
- 0
n % 62 is the remainder, which picks the next character from the alphabet. n //= 62n /= 62 then drops that digit from n. In Python, // divides and keeps only the whole part.In Java, n is a long, and / between two whole numbers keeps only the whole part. The new character goes in front of code, because the remainders come out last character first. The loop stops when n reaches 0. Zero itself would never enter the loop, so the function returns "0" for it at the start.
The Run button runs the Python version; the Java tab is for reading.
How many characters?
Lesson 1 said the service will hold 6 billion links. Each extra character multiplies the number of codes by 62:
for length in [5, 6, 7]: print(length, 62 ** length)5 916132832 6 56800235584 7 3521614606208
public class Main { public static void main(String[] args) { long codes = 62L * 62 * 62 * 62; for (int length = 5; length <= 7; length++) { codes = codes * 62; System.out.println(length + " " + codes); } }}5 916132832 6 56800235584 7 3521614606208
62⁵ is 916,132,832, fewer than 6 billion. 62⁶ is 56,800,235,584, more than 9 times 6 billion. So six base62 characters are enough for 6 billion links. The last six-character code is 62⁶ − 1, which prints as ZZZZZZ; the very next number, 62⁶, needs seven characters: 1000000.
Java needs care with numbers this large. The largest int is 2,147,483,647, and 62⁶ is bigger. When an int result goes past the largest value, Java gives no error: the number wraps round and comes out wrong.
print(62 * 62 * 62 * 62 * 62 * 62)56800235584
public class Main { public static void main(String[] args) { int wrong = 62 * 62 * 62 * 62 * 62 * 62; long right = 62L * 62 * 62 * 62 * 62 * 62; System.out.println(wrong); System.out.println(right); }}965660736 56800235584
In Java, the product of six 62s as an int prints 965660736, a wrong answer with no warning. Writing the first number as 62L makes the whole product a long, which holds it. Python's whole numbers have no such limit, so Python prints the right value.
Other ways to make codes
A counter is not the only way. Two others come up often:
- A hash of the URL. A turns the long URL into a long number that looks random; keep the first seven base62 characters. The same URL always gets the same code, which is sometimes wanted and sometimes not. Two different URLs can also give the same seven characters. That clash is a , and the service must check the table and try again.
- Random codes. Pick six random characters, check whether the table already has them, and pick again if so.
The counter has no collisions and needs no checks. Its weak point is that every new link must ask the same counter. The fix is to hand out blocks: app server 1 takes the numbers 1 to 1,000 and counts through them on its own, server 2 takes 1,001 to 2,000, and a server asks for a new block only when its block runs out. If a server crashes, the rest of its block is never used, but no number is ever used twice.
Check yourself
The same base62 function, called with 200. What does this print?
ALPHABET = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
def base62(n): if n == 0: return "0" code = "" while n > 0: code = ALPHABET[n % 62] + code n //= 62 return code
print(base62(200))public class Main { static final String ALPHABET = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
static String base62(long n) { if (n == 0) { return "0"; } String code = ""; while (n > 0) { code = ALPHABET.charAt((int) (n % 62)) + code; n /= 62; } return code; }
public static void main(String[] args) { System.out.println(base62(200)); }}Complete the line that drops the digit the loop has used, so that the loop ends and the program prints 21.
ALPHABET = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
def base62(n): if n == 0: return "0" code = "" while n > 0: code = ALPHABET[n % 62] + code ________ # ← drop the digit the line above used return code
print(base62(125))public class Main { static final String ALPHABET = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
static String base62(long n) { if (n == 0) { return "0"; } String code = ""; while (n > 0) { code = ALPHABET.charAt((int) (n % 62)) + code; ________ // ← drop the digit the line above used } return code; }
public static void main(String[] args) { System.out.println(base62(125)); }}A smaller service expects 1 billion links in all. What is the shortest base62 code length that gives every link its own code?
Honesty notes
- Give every new link a number from a counter, so no two links clash.
- Turn a number into base62 by taking the remainder by 62 for each character and dividing by 62, building the code from the right: 125 is 21.
- Show that six characters are enough for 6 billion links: 62 to the power 5 is about 916 million, 62 to the power 6 about 56.8 billion.
- Keep 62 to the power 6 in a long in Java: as an int it wraps round and prints 965660736.
- Compare a counter with a hash of the URL and random codes, and hand each server a block of counter values.