Try
↑ ↓ move↵ openesc close
In this lesson · topic 3 of 7
  1. 1. A short link is a tiny key-value store
  2. 2. Requirements, numbers, API and data model
  3. 3. Make codes and follow them in the lab
  4. 4. Random codes meet the birthday bound
  5. 5. Counter ranges, random codes or hashes
  6. 6. Hot links, caches and 301 against 302
  7. 7. The URL shortener in review

Topic 3

Make codes and follow them in the lab

8 min read

What the lab runs

Each tick the service creates 100 links and handles 300 clicks. A random code is drawn from 62 symbols. If it is taken, the service draws again, up to eight times. A counter hands out the next number and never repeats.

Clicks are skewed. A few links draw most of them. The server keeps a cache of the most recently used links and counts a hit when a click finds its link there.

The lab stops adding links at 60,000 so a long run stays cheap.

Predict before you press play

You switch from a 4 character random code to a counter. What happens to the cache hit ratio?

tick 40random, 4 characters, 14,776,336 codes302 redirects

Creating links

links created
4,000
code length
4 characters
collisions
1 (0.02%)
creates that gave up
0

A random code is drawn again when it is already taken. A counter never repeats, and its codes grow longer as the count grows.

Following links

clicks
12,000
reached the server
12,000
cache hit ratio
85%
clicks the server counted
100%

Reads are skewed, so a small cache holds the popular links. A 301 lets browsers skip the server on repeat clicks, which also hides those clicks from the server.

Collision share and cache hit share each tick, last 150 ticks
Collision share and cache hit share each tick, last 150 ticks. Text version available below the chart.025507510012140recent tickspercent

Full-page version with guided experiments: Short ID collisions lab.

Things to try

  1. Set random codes to 3 characters and read the collision share.
  2. Set them to 5 characters.
  3. Switch to the counter.
  4. Set the cache to 100 links, then to 5,000.
  5. Lower the click skew to 6 tenths.
  6. Switch the redirect status to 301.

The full-page lab has a guided experiment for each of these.

Your progress is saved in this browser. Sign in to keep it on every device. Privacy · Terms