Part 2 of 7 · 2 min read
Requirements and numbers
Count before drawing.
Think of a coat-check at a theatre. A long coat goes in and a small numbered tag comes out. Later, the tag brings back the coat. A short URL is the tag, and the long URL is the coat.
The book's example turns a long address with many parameters into a short alias such as tinyurl.com/y7keocwj. Clicking the alias sends the user to the original.
Questions and the answers assumed here (from the book's sample talk).
- What traffic? 100 million new URLs per day.
- How short? As short as possible.
- Which characters? Digits 0 to 9, lower case a to z and upper case A to Z. That is 10 + 26 + 26 = 62 characters.
- Can links be deleted or changed? No, to keep it simple.
Use cases. Shortening: given a long URL, return a short one. Redirecting: given a short one, send the user to the long one. Plus high availability, scale and fault tolerance.
Numbers.
- Writes per second: 100,000,000 / 86,400 = about 1,157, which the book rounds to 1,160.
- Reads per second, at a read to write ratio of 10 to 1: about 11,600.
- Records over 10 years: 100,000,000 × 365 × 10 = 365 billion.
- Size per record, assuming an average long URL of 100 bytes: 365,000,000,000 × 100 bytes = 36.5 TB.
A note on the last line. The book prints the storage as 365 TB. The arithmetic gives 36.5 TB, because the 10 years are already inside the 365 billion records, and multiplying by 10 again overcounts. Either way, the main lesson is the same: the data is large but fits a sharded database, and reads are about ten times writes, so reads need a cache. Check numbers with the interviewer aloud, as the book advises.
Read-heavy
A system is read-heavy when it serves many more reads than writes.
Example: a short link is created once and may be clicked thousands of times, so reads far outnumber writes.
Check yourself: 1. How many characters are in the allowed alphabet? 2. Why is 365 TB an overcount? 3. What does a 10 to 1 read ratio suggest adding?