Sub topic 2
Estimating traffic, connections, and storage
This chapter assumes you know basic client-server concepts, what a persistent connection is, and how to read short Python snippets. You will turn the requirements from the previous chapter into rough numbers. Those numbers drive the choices in later chapters: protocol, server count, and storage.
Why estimate first
A back-of-envelope estimate is a calculation good to within a factor of two or so. It tells you whether one machine is enough, whether you need sharding, and which resource limits you first. Round aggressively and write down your assumptions.
Assumptions
Suppose the requirements gave us these figures:
- 50 million daily active users (DAU).
- Each user sends 40 messages per day.
- Peak traffic is 3 times the average.
- 20% of users are connected at the busiest moment.
- Average message size is 100 bytes of text, plus about 100 bytes of metadata (IDs, timestamps, sender).
- Messages are kept for 5 years.
Traffic
A day has about 86,400 seconds. We round it to 100,000 to keep the arithmetic easy.
- Messages per day: 50 million × 40 = 2 billion.
- Average rate: 2 billion / 100,000 s = 20,000 messages per second.
- Peak rate: 20,000 × 3 = 60,000 messages per second.
Each message in a one-to-one chat is written once and delivered once. Group chat multiplies deliveries, which a later chapter covers.
Connections
Every online user holds one persistent connection, so connection count matters more than request rate.
- Concurrent connections at peak: 50 million × 20% = 10 million.
- If one chat server safely holds 100,000 connections, you need about 100 servers.
The per-server limit depends on memory per connection, file descriptor limits, and heartbeat traffic. Treat 100,000 as an assumption to measure, not a fact.
Storage
- Bytes per message: 100 + 100 = 200 bytes.
- Per day: 2 billion × 200 B = 400 GB.
- Per year: 400 GB × 365 ≈ 146 TB.
- Five years: about 730 TB, before replication.
With three replicas this becomes about 2.2 PB. A single database server cannot hold this, so storage must be partitioned. That conclusion is the real output of the estimate.
The same math in code
DAU = 50_000_000
MSGS_PER_USER = 40
SECONDS_PER_DAY = 100_000
PEAK_FACTOR = 3
ONLINE_RATIO = 0.20
CONN_PER_SERVER = 100_000
BYTES_PER_MSG = 200
YEARS = 5
REPLICAS = 3
msgs_per_day = DAU * MSGS_PER_USER
avg_qps = msgs_per_day / SECONDS_PER_DAY
peak_qps = avg_qps * PEAK_FACTOR
connections = DAU * ONLINE_RATIO
servers = connections / CONN_PER_SERVER
daily_bytes = msgs_per_day * BYTES_PER_MSG
total_bytes = daily_bytes * 365 * YEARS
print(f"avg {avg_qps:,.0f} msg/s, peak {peak_qps:,.0f} msg/s")
print(f"connections {connections:,.0f}, servers {servers:,.0f}")
print(f"per day {daily_bytes / 1e9:,.0f} GB")
print(f"{YEARS} years {total_bytes / 1e12:,.0f} TB, "
f"replicated {total_bytes * REPLICAS / 1e15:,.1f} PB")Running it prints 20,000 and 60,000 messages per second, 10,000,000 connections, 100 servers, 400 GB per day, 730 TB over five years, and 2.2 PB replicated.
Common errors
- Mixing units, such as bits and bytes, or GB and GiB. Keep one unit throughout.
- Using the average rate for capacity. Size for the peak.
- Forgetting replication, indexes, and metadata in storage.
- Treating the result as exact. Only the order of magnitude matters.
Exercise
Using the assumptions in this chapter, the product team says users now send 80 messages per day, and each message is 300 bytes in total with metadata. What are the new average messages per second and the storage per day?
Show answer
Messages per day = 50 million × 80 = 4 billion. Average rate = 4 billion / 100,000 = 40,000 messages per second. Storage per day = 4 billion × 300 B = 1.2 TB.
Exercise
Which estimate decides the number of chat servers, and why is it not the message rate?
Show answer
The number of concurrent connections decides it. Each online user holds a persistent connection, and a server is limited by how many connections it can keep open (memory, file descriptors), not only by messages per second.