Start with questions
After this: Ask what the service must do and how big it is, and turn the answers into requests per second and storage.
On this page · 5 sections · 3 checks
Someone asks you to build a service that makes long web addresses short. You give it https://example.com/recipes/lemon-cake, and it gives you back https://short.example/21. Anyone who opens the short address lands on the lemon cake page. Where do you start?
Not with boxes and arrows. This tutorial designs the whole service, one decision at a time, and the first step is to ask questions.
What must it do?
A is one thing the finished service must do, or one limit it must work within. Two kinds of question find them. The first kind asks what the service does for the people who use it:
- Make a for a long URL (a web address). The part after the slash,
21, is the short code. - When anyone opens the short link, send them on to the long URL. Sending a browser on to another address is called a .
The second kind asks how big and for how long:
- Links last 5 years.
- 100 million new links are made each month.
- Each link is opened 100 times on average.
In an interview you ask these questions out loud, and the interviewer gives you the numbers. Here they are given. Write each one down, because every later decision depends on them.
From a month to a second
The Estimating tutorial turned numbers per day into numbers per second. Here the numbers are per month. A month of 30 days has 30 × 24 × 60 × 60 = 2,592,000 seconds. Work through the rest one step at a time:
- New links. 100,000,000 / 2,592,000 = 38.6 per second. Each new link is a write: the service saves something.
- Redirects. Each link is opened 100 times, so there are 10 billion redirects a month. 10,000,000,000 / 2,592,000 = 3,858 redirects per second, about 333 million a day. Each redirect is a read: the service only looks something up.
- Links kept. 100 million a month × 12 months × 5 years = 6 billion links.
- Space. Say one stored link takes 500 bytes, most of it the long URL. 6,000,000,000 × 500 bytes = 3,000,000,000,000 bytes, which is 3 TB.
Reads outnumber writes 100 to 1. A service like this is called , and that one fact steers the rest of the design. Lesson 4 is about serving those reads.
The estimate as code
Here are the same steps as a program. The Run button below runs the Python version in your browser. Java does not run here: the Java tab shows the same program for reading, and you can paste it into a Java tool on your computer.
The program prints the seconds in a month, the writes per second rounded to one decimal place, the reads per second rounded to a whole number, the links over five years, and the space. Change 100 redirects per link to 50 and run it again: the reads halve, and the space stays the same, because opening a link stores nothing new.
In Java, links is a long. 6 billion is bigger than the largest int, which is 2,147,483,647. Lesson 3 shows what Java prints when a number that size is kept in an int.
Check yourself
You are asked to design a short-link service. Before you draw any boxes, what do you do first?
A different service makes 50 million new links per month, and each link is opened 100 times. About how many redirects per second is that? A 30-day month has 2,592,000 seconds.
Keep the lesson's numbers, 100 million new links a month for 5 years, but say each stored link takes 1,000 bytes instead of 500. How much space do the links take?
Honesty notes
- 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.