Basis Lab

A System Design in Forty Minutes

The One Machine Version, and the Number at Which It Breaks

Last timeNumbers Before Boxes

Design a link shortener on a single machine, then compute exactly which of its three ceilings the demand breaks and by what factor, so every later component has something it repairs.

The arithmetic is already agreed. A link shortener with click analytics, taking

100,000,000 new links a day, which is 1,157 a second and is carried through as

1,200 writes a second. Each link is followed about 100 times over its life, so

120,000 redirects a second at the mean. The peak is three times the mean, so

3,600 writes and 360,000 reads a second. A link row is 500 bytes, so 50 GB a day

and 18 TB a year, and a click event is 100 bytes, so 1 TB a day and 365 TB a

year. The read to write ratio is 100 to 1.

The temptation is to start drawing. Spend five minutes instead on the version

that runs on one machine, because a component added later can only be defended

by the baseline number it repairs, and without a baseline there is no such

number.

The one machine version

One process on one box. A table mapping short codes to destination addresses,

held in memory so a redirect is a lookup rather than a disk seek. The same rows

written to a disk backed store so that a restart does not lose them. An append

only file for click events. Two operations, which are the two the brief asked

for: create a link, and follow one.

FIG 1The whole design, on one machine
Three stores on one disk and one of them in memory as well. Nothing in the picture is wrong at small volume, and every arrow in it is a candidate for a ceiling.

The one decision worth making here is where short codes come from, because it

is the only part of the design that cannot be bought and it changes the write

path. A counter rendered in base 62 gives the shortest possible codes and needs

one process to own the counter. Hashing the destination gives codes without

coordination and needs a collision check, which is a read before every write.

The lesson stops here

8 more paragraphs to go

You have read the opening. The rest of the argument, the problems that check whether it landed, and the lines worth keeping at the end all come with a plan.

The first lesson of every course in the library reads the whole way through, free, so you can see exactly what the rest of them are.

See the planThe contents

This is the reading half

Starting the course gives you your own copy of it. Every idea on every page has problems standing under it, marked with a reason rather than a tick, and any sentence you do not believe can be opened and argued with. None of that can happen on a page nobody owns.

The contents

The rest of this course

  1. 01What a One Line Design Request Actually Asks For
  2. 02Estimating the Load Before You Draw Anythingopening only
  3. 03The One Machine Version, and the Number at Which It Breaksyou are here
  4. 04Where 360,000 Reads a Second Actually Goopening only
  5. 05Splitting the Writes, and the Number That Decides How Many Piecesopening only
  6. 06What a Reader Sees While Something Is Brokenopening only
  7. 07Justifying Every Box in the Drawing With the Number That Forced Itopening only
  8. 08Where Each of the Forty Minutes Goes, and the Two Mistakes That Cost Tenopening only

Read alongside