Case study demo

URL shortener

Turn a long URL into a short Base62 code and resolve it again.

An interactive demo of how this system behaves. It runs in your browser.

Case study · Live model

How a URL shortener behaves

Browsers click short links and create new ones. Change the cache, the redirect type and how codes are made, then stop the counter service, and watch what each choice costs.

Try it

Lab time 3.0 s
code → URLnext IDBrowsers, 30 clicks/sBrowsers30 clicks/s302 redirectApp servers A, B, ID rangesApp servers A, BID rangesIDs left: A 46, B 46Cache, 50/50 linksCache50/50 linkshit rate 44%Database, 508 linksDatabase508 links18/s readsCounter, runningCounterrunningnext 1,000,601
  • Browsers — 30 clicks/s — healthy — 302 redirect
  • App servers A, B — ID ranges — healthy — IDs left: A 46, B 46
  • Cache — 50/50 links — healthy — hit rate 44%
  • Database — 508 links — healthy — 18/s reads
  • Counter — running — healthy — next 1,000,601
  • Request
  • Cache hit
  • Database read
  • Copied into the cache
  • New link saved
  • Redirect
  • Link not created
Clicks
31/s
that reach the servers
Cache hit rate
44%
Database reads
18/s
Clicks counted
100%
last 2 s; 94 of 94 in all
Links made
8
0 failed
Collisions
0
code already taken

Numbers add up:94 clicks = 0 answered by browsers + 39 cache hits + 55 database reads; 8 creates = 8 made + 0 failed; no code used twiceThe lab checks this sum as it runs, so a counting mistake would show here.

Controls

Clicks
30 a second
Redirect

Browsers ask every time, so every click reaches the servers and is counted.

50 of 500 links
New links
2 a second
Code from

Each app server takes a block of 50 IDs at a time from the counter. A server that restarts loses the rest of its block.

Newest links

CodeHow the code was madeServer
4chYID 1,000,554 in Base62B
4chaID 1,000,504 in Base62A
4chXID 1,000,553 in Base62B
4ch9ID 1,000,503 in Base62A
4chWID 1,000,552 in Base62B
4ch8ID 1,000,502 in Base62A
How ID 1,000,001 becomes code 4c93
  1. 1000001 % 62 = 3 → "3"
  2. 16129 % 62 = 9 → "9"
  3. 260 % 62 = 12 → "c"
  4. 4 % 62 = 4 → "4"

Divide by 62 again and again. The remainders, read from the last one to the first, spell 4c93. Base62 uses 0–9, a–z and A–Z, so 7 characters give about 3.5 trillion codes.

Cache hit rate
0%25%50%75%100%1s2s3s
Show data table
TimeHit rate
1s33%
1s38%
2s39%
2s36%
3s42%
3s44%
Database reads per second
012.52537.5501s2s3s
Show data table
TimeReads/s
1s28
1s20
2s18.7
2s18.5
3s15.5
3s17.5

What just happened

  1. 0.5sInfo: Server B takes IDs 1,000,551–1,000,600 from the counter.
  2. 0.2sInfo: Server A takes IDs 1,000,501–1,000,550 from the counter.
What this model simplifies
  • Clicks pick among 500 existing links on a fixed popularity curve. New links get no clicks here.
  • 4 in 10 clicks are repeat visits from the same browser. With a 301, all of them stay in the browser; real browsers can clear their cache.
  • Two app servers share one database and one counter. ID ranges are 50 IDs; real services use far larger ranges.
  • A hashed code is cut from a 32-bit hash here. Real services often use a longer hash, and a database rule that refuses a code already taken.
  • A 301 here stays in the browser for good. Real shorteners often add a Cache-Control time limit, so browsers ask again later and clicks are still counted.