# Chapter 2: Back-of-the-envelope Estimation

## Power of two

In a system design interview, sometimes you are asked to estimate system capacity or performance requirements using a back-of-the-envelope estimation. According to Jeff Dean, Google Senior Fellow,

“back-of-the-envelope calculations are estimates you create using a combination of thought experiments and common performance numbers to get a good feel for which designs will meet your requirements”

You need to have a good sense of scalability basics to effectively carry out back-of-the-envelope estimation. The following concepts should be well understood:

1. power of two
2. latency numbers every programmer should know
3. availability numbers

Although data volume can become enormous when dealing with distributed systems, calculation all boils down to the basics. To obtain correct calculations, it is critical to know the data volume unit using the power of 2. A byte is a sequence of 8 bits. An ASCII character uses one byte of memory (8 bits). Below is a table explaining the data volume unit (Table 2-1).

## Latency numbers every programmer should know

Dr. Dean from Google reveals the length of typical computer operations in 2010. Some numbers are outdated as computers become faster and more powerful. However, those numbers should still be able to give us an idea of the fastness and slowness of different computer operations.

Notes
ns = nanosecond, µs = microsecond, ms = millisecond
1 ns = 10^-9 seconds
1 µs= 10^-6 seconds = 1,000 ns
1 ms = 10^-3 seconds = 1,000 µs = 1,000,000 ns

A Google software engineer built a tool to visualize Dr. Dean’s numbers. The tool also takes the time factor into consideration. Figures 2-1 shows the visualized latency numbers as of 2020 (source of figures: reference material).

By analyzing the numbers in Figure 2-1, we get the following conclusions:

1. Memory is fast but the disk is slow.
2. Avoid disk seeks if possible.
3. Simple compression algorithms are fast.
4. Compress data before sending it over the internet if possible.
5. Data centers are usually in different regions, and it takes time to send data between them.

## Availability numbers

High availability is the ability of a system to be continuously operational for a desirably long period of time. High availability is measured as a percentage, with 100% means a service that has 0 downtime. Most services fall between 99% and 100%.

A service level agreement (SLA) is a commonly used term for service providers. This is an agreement between you (the service provider) and your customer, and this agreement formally defines the level of uptime your service will deliver. Cloud providers Amazon, Google and Microsoft set their SLAs at 99.9% or above. Uptime is traditionally measured in nines. The more the nines, the better. As shown in Table 2-3, the number of nines correlate to the expected system downtime.

## Example: Estimate Twitter QPS and storage requirements

Please note the following numbers are for this exercise only as they are not real numbers from Twitter.

Assumptions:

1. 300 million monthly active users.
2. 50% of users use Twitter daily.
3. Users post 2 tweets per day on average.
4. 10% of tweets contain media.
5. Data is stored for 5 years.

Estimations: Query per second (QPS) estimate:

1. Daily active users (DAU) = 300 million * 50% = 150 million
2. Tweets QPS = 150 million * 2 tweets / 24 hour / 3600 seconds = ~3500
3. Peak QPS = 2 * QPS = ~7000

We will only estimate media storage here.

1. Average tweet size:
2. tweet_id 64 bytes
3. text 140 bytes
4. media 1 MB
5. Media storage: 150 million * 2 * 10% * 1 MB = 30 TB per day
6. 5-year media storage: 30 TB * 365 * 5 = ~55 PB

## Tips

Back-of-the-envelope estimation is all about the process. Solving the problem is more important than obtaining results. Interviewers may test your problem-solving skills. Here are a few tips to follow:

1. Rounding and Approximation. It is difficult to perform complicated math operations during the interview. For example, what is the result of “99987 / 9.1”? There is no need to spend valuable time to solve complicated math problems. Precision is not expected. Use round numbers and approximation to your advantage. The division question can be simplified as follows: “100,000 / 10”.
2. Write down your assumptions. It is a good idea to write down your assumptions to be referenced later.
3. Label your units. When you write down “5”, does it mean 5 KB or 5 MB? You might confuse yourself with this. Write down the units because “5 MB” helps to remove ambiguity.