Sub topic 3
Powers of two and data size units
This chapter covers the size units and powers of two you need for storage and memory estimates. It assumes you know what a byte and a bit are, can read exponents, and have seen simple Python. The earlier chapters on why we estimate and how to state assumptions apply here. Every figure below is meant to be rounded, so you can do the arithmetic in your head.
Why powers of two matter
Computers address memory and store data in binary, so natural sizes are powers of two: 2, 4, 8, 16, and so on. In estimation you rarely need exact powers. You need the few that map onto the words you use in conversation: thousand, million, billion, trillion.
The key fact is that 2^10 = 1,024, which is very close to 1,000. Every step of 10 in the exponent multiplies the value by about a thousand. That lets you convert between "how many bits do I need" and "how many items is that" without a calculator.
| Power | Exact value | Approximate value | Common name |
|---|---|---|---|
| 2^10 | 1,024 | 1 thousand | 1 KB |
| 2^20 | 1,048,576 | 1 million | 1 MB |
| 2^30 | 1,073,741,824 | 1 billion | 1 GB |
| 2^40 | 1,099,511,627,776 | 1 trillion | 1 TB |
| 2^50 | about 1.126 × 10^15 | 1 quadrillion | 1 PB |
For other exponents, split them into a multiple of 10 plus a remainder, and use the small powers 2^1 to 2^9 (2, 4, 8, 16, 32, 64, 128, 256, 512). For example, 2^37 = 2^30 × 2^7 = 1 billion × 128 ≈ 128 billion. The exact value is 137,438,953,472, so the shortcut is off by about 7%, which is fine for an estimate.
Decimal and binary units
There are two conventions for unit prefixes, and they differ slightly.
- Decimal (SI) units use powers of ten. 1 KB = 1,000 bytes, 1 MB = 1,000,000 bytes, 1 GB = 10^9 bytes.
- Binary (IEC) units use powers of two. 1 KiB = 1,024 bytes, 1 MiB = 2^20 bytes, 1 GiB = 2^30 bytes.
In everyday speech, and in many operating systems and documents, "KB" and "GB" are used for either. Disk vendors usually use decimal units. Memory is usually measured in binary units. The gap grows with each prefix, as this script shows.
decimal = {"KB": 10**3, "MB": 10**6, "GB": 10**9, "TB": 10**12, "PB": 10**15}
binary = {"KiB": 2**10, "MiB": 2**20, "GiB": 2**30, "TiB": 2**40, "PiB": 2**50}
for (d_name, d_size), (b_name, b_size) in zip(decimal.items(), binary.items()):
gap = 100 * (b_size / d_size - 1)
print(f"{b_name} is {gap:.1f}% larger than {d_name}")
# KiB is 2.4% larger than KB
# MiB is 4.9% larger than MB
# GiB is 7.4% larger than GB
# TiB is 10.0% larger than TB
# PiB is 12.6% larger than PBThe error stays below 3% at the kilobyte level and reaches about 10% at the terabyte level. In a system design estimate, where inputs such as record size are guesses accurate to a factor of two, a 10% unit difference is smaller than your other uncertainties. So the practical rule is:
- Use decimal units (1 KB = 1,000 bytes) for estimates. The arithmetic is easier.
- Say which convention you used if the number will be compared against a real device or a cloud bill.
- Switch to exact binary values only when a hard limit matters, such as a 32-bit counter or a fixed-size buffer.
The storage example from the spine used decimal units. 100 million records per day at 500 bytes for 5 years gave 91.25 TB. In binary units the same quantity is 91.25 × 10^12 / 2^40, or about 83 TiB. Both are correct. They answer slightly different questions.
Bits versus bytes
A bit is a single 0 or 1. A byte is 8 bits. Storage and memory are quoted in bytes (capital B). Network speeds are quoted in bits per second (lowercase b). Mixing these up gives a factor-of-8 error.
- A 1 Gbps link carries 10^9 bits per second.
- That is 10^9 / 8 = 125 million bytes per second, or 125 MB/s.
link_gbps = 1
bytes_per_second = link_gbps * 1e9 / 8
print(bytes_per_second / 1e6) # 125.0 (MB per second)
data_bytes = 1e12 # 1 TB
seconds = data_bytes / bytes_per_second
print(round(seconds), round(seconds / 3600, 1)) # 8000 2.2Moving 1 TB over a fully used 1 Gbps link takes about 8,000 seconds, a little over two hours. Real links rarely run at full speed, so the real transfer takes longer. Later chapters on bandwidth estimation build on this conversion.
Typical object sizes
To estimate storage you need a sense of how big common things are. These are round figures to start from. Replace them with measured values when you have them.
| Item | Rough size |
|---|---|
| One ASCII character | 1 byte |
| Integer (32-bit) | 4 bytes |
| Integer or timestamp (64-bit) | 8 bytes |
| UUID in binary form | 16 bytes |
| Short URL or username | 10 to 100 bytes |
| A typical database row | 100 bytes to 1 KB |
| A text post or message | 1 to 5 KB |
| A compressed photo | 200 KB to 2 MB |
| One minute of compressed video | 5 to 50 MB |
Notice that many of these span a factor of ten or more. That is normal. Choose a value in the middle, write it down as an assumption, and note how the answer changes if the true figure is at either end.
Text encoding is a common source of error. ASCII uses 1 byte per character. UTF-8 uses 1 to 4 bytes, so text in many non-Latin scripts averages 2 to 3 bytes per character. A string field of 200 characters may need 200 bytes or 600 bytes depending on the content.
Counting identifiers with bits
Powers of two also answer the question "how many bits do I need to give every item a unique ID?" If you have N items, you need at least log2(N) bits, rounded up.
import math
def bits_needed(n):
return math.ceil(math.log2(n))
print(bits_needed(1_000_000_000)) # 30
print(bits_needed(100_000_000_000)) # 37One billion items fit in 30 bits, since 2^30 is just over a billion. One hundred billion items need 37 bits. A few facts to memorize:
- A 32-bit unsigned integer holds values up to 2^32 − 1, about 4.3 billion. A signed 32-bit integer tops out near 2.1 billion.
- A 64-bit integer holds about 1.8 × 10^19 values, which is more than enough for any ID counter you will meet.
- A 128-bit ID such as a UUID has 2^128 possible values, about 3.4 × 10^38.
You can use the same approach for text IDs. An ID of length L over an alphabet of size A has A^L possible values. With 62 characters (a-z, A-Z, 0-9) and 7 positions, that is 62^7 ≈ 3.5 × 10^12, or 3.5 trillion. We reuse this calculation in the URL shortener example later.
Common mistakes with units
- Mixing bits and bytes. Check every network figure. Divide bits by 8 to get bytes.
- Mixing unit levels. Writing 500 bytes × 100 million and calling the result "50 MB" drops three orders of magnitude. The result is 5 × 10^10 bytes, which is 50 GB. Track the exponent explicitly: 5 × 10^2 × 10^8 = 5 × 10^10.
- Dropping a factor of 1,000 in conversion. Each prefix is a factor of 1,000. From bytes to TB is four steps (KB, MB, GB, TB), so divide by 10^12.
- Treating storage as free of overhead. Raw record size is a lower bound. Indexes, replicas, and metadata add more. Chapter material on storage estimation covers a multiplier for this.
A reliable habit is to write numbers in scientific notation while you calculate (5 × 10^10) and convert to a named unit only at the end. Exponents add when you multiply and subtract when you divide, so you can check each step by looking at the exponent alone.
Worked mini-example
Suppose a service stores 20 million new photos per day, with an average size of 300 KB. How much new data is that per day?
- 20 million = 2 × 10^7 photos.
- 300 KB = 3 × 10^5 bytes.
- Multiply: 2 × 3 = 6, and 10^7 × 10^5 = 10^12. The total is 6 × 10^12 bytes.
- 10^12 bytes is 1 TB, so the answer is about 6 TB per day.
Now check the scale. 6 TB per day over a year is about 6 × 365 ≈ 2,200 TB, or about 2.2 PB. That is a large but plausible figure for a major photo service, so the estimate passes a first sanity check.
Exercise
A messaging app stores 50 million messages per day, averaging 2 KB each. How much new data is that per day in decimal units? Then state how many bits you would need to give every message from one year of traffic a unique numeric ID.
Show answer
50 million = 5 × 10^7 messages and 2 KB = 2 × 10^3 bytes. Multiplying gives 10 × 10^10 = 10^11 bytes, which is 100 GB per day. One year is about 365 × 5 × 10^7 ≈ 1.8 × 10^10 messages, or about 18 billion. Since 2^34 ≈ 17.2 billion and 2^35 ≈ 34.4 billion, you need 35 bits. In practice you would use a 64-bit integer.
Exercise
A vendor sells a "4 TB" disk (decimal units). An operating system reports capacity in binary units. Roughly how many TiB will the OS show, and why does it look smaller?
Show answer
4 × 10^12 bytes divided by 2^40 (about 1.0995 × 10^12) is about 3.64 TiB. The disk holds exactly the advertised number of bytes. The OS uses a larger unit, the tebibyte, which is about 10% bigger than a terabyte, so the number is smaller.