Design a URL shortener Your results Progress Loading your progress
Results and the cheat sheet fill in as you go. Nothing here is locked; come back whenever you like.
Next in this path: Design a news feed · 6 lessons · 1 h 15 min
Mastery by outcome Each outcome is backed by the checks that test it. Solid means every check was right first time; with hints means you got there with a nudge; worth a revisit means an answer was revealed or is still wrong.
1 Ask the questions that shape a design, and turn the answers into numbers: writes and reads per second, links over five years, and the space they take.
Not yet 2 Write the two endpoints and the table of a short-link service, with the right status codes and the right key.
Not yet 3 Make short codes from a counter with base62 in Python and Java, and work out how many characters a code needs.
Not yet 4 Put a cache in front of the database for the busiest links, work out what it saves, and say where the design runs out of room first.
Not yet Your cheat sheet What each lesson leaves you able to do, in one place. Lessons you have not finished are dimmed, not hidden.
Start a design with questions: what the service must do, and how big it is and for how long. Turn a number per month into a number per second by dividing by 2,592,000, the seconds in a 30-day month. Work out the shortener's numbers: 38.6 new links and 3,858 redirects a second, 6 billion links over five years, 3 TB at 500 bytes each. Say that reads outnumber writes 100 to 1, so the service is read-heavy, and that a count of 6 billion needs a long in Java. Name the two endpoints: POST /links to make a link, answered with 201 Created, and GET /{code} to follow one. Answer a redirect with 302 Found and a Location header, and an unknown code with 404 Not Found. Write the links table with code as the primary key, and say why the key is the code and not the long URL. Say what 301 changes: browsers remember the redirect, which cuts load but hides the clicks. 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. Serve redirects cache-aside: look in the cache, and on a miss read the database and fill the cache. Read the flow: three creates and eight redirects give 5 hits, 3 misses, 3 database writes and 3 reads, 38 ms of redirects and 30 ms of creates. Size the cache: 10 million busy links at 500 bytes is 5 GB, and a hit ratio of 0.9 leaves about 386 database reads a second. Share the remaining reads across read replicas, and ask the primary when a brand-new link is not on a replica yet. Draw the whole design: load balancer, app servers, cache, primary database and read replicas, each with its number. Work out the app servers: about 3,900 requests a second at 400 each is about 10 servers on average, and about 20 at a peak of twice the average. Name the bottleneck part by part: the database without a cache, the misses with one, a single counter, a single cache server. Talk through a design in order, and say each trade-off out loud with its number. Estimate the redirects a second of a smaller shortener from its links a month and visits per link. Predict a base62 code by hand, and complete the line of the loop that puts each character in front. Explain why Java prints 965660736 for 62 to the power 6 as an int, and work out the database reads a cache leaves.