Sub topic 2
Estimating traffic, connections, and storage
This chapter assumes you know basic client-server and HTTP concepts, can read short Python snippets, and know what a persistent connection is. You don't need prior estimation experience.
A back-of-envelope estimate is a rough calculation done early in a design. It doesn't predict the exact load. It tells you which resource will hurt first, so you can pick an architecture that fits. For a chat system, the estimate answers three questions:
- How many messages per second must the servers accept and deliver?
- How many connections must the servers hold open at once?
- How much data accumulates over months and years?
Why estimate before designing
The three answers push the design in different directions.
- High message rates affect the message queue, the write path of the database, and routing between servers.
- Many open connections affect the number of stateful chat servers and their memory.
- Growing storage affects the choice of database, sharding, and retention policy.
A chat system with 10,000 users can run on one server and one database. A system with 100 million daily users cannot. The estimate shows which of those two cases you are in.
State your assumptions first
Every number in an estimate comes from an assumption. Write the assumptions down so that anyone can challenge them and rerun the math. These are the assumptions used in this chapter.
| Assumption | Value | Reason |
|---|---|---|
| Daily active users (DAU) | 100 million | Target scale for the design |
| Messages sent per user per day | 40 | Mix of light and heavy users |
| Average stored size per message | 200 bytes | About 100 bytes of text plus IDs, timestamps, and flags |
| Peak-to-average ratio | 3 | Traffic is uneven across the day |
| Share of DAU connected at peak | 20% | Not everyone is online at once |
| Connections per chat server | 100,000 | Typical order of magnitude for a tuned server |
| Target server utilization | 50% | Headroom for spikes and failures |
| Replication factor | 3 | Copies kept for durability |
| Heartbeat interval | 30 seconds | Used for presence, covered in a later chapter |
DAU means daily active users, the people who use the app at least once in a day. Use DAU instead of registered users because registered accounts include people who never come back.
Traffic: messages per second
Start with the daily total, then divide by the seconds in a day.
- Messages per day: 100,000,000 × 40 = 4 billion.
- Average rate: 4,000,000,000 / 86,400 ≈ 46,000 messages per second.
- Peak rate: 3 × 46,000 ≈ 139,000 messages per second.
Size the system for the peak, not the average. A system that handles the average rate will fall over every evening when usage rises.
The write rate is only half of the traffic. Each message must also be delivered, so the delivery rate is higher. In a one-to-one chat, one message produces one delivery. In a group of 50 people, one message produces 49 deliveries. If the average message reaches 1.5 recipients, deliveries run at about 1.5 times the send rate. Group chat fan-out is covered in a later chapter, but note here that groups multiply the read side of the work.
Bandwidth follows from the rate. At the peak, 139,000 messages per second at 200 bytes is about 28 MB/s of inbound payload, or roughly 220 Mbit/s. That is small for a data center. Chat is rarely limited by bandwidth until you add media, which is why attachments are handled separately.
Connections: the stateful part
Each online client holds an open connection to a chat server. Connections are the resource that makes chat different from a typical web API.
- Concurrent connections at peak: 20% of 100 million = 20 million.
- Servers needed at full capacity: 20,000,000 / 100,000 = 200.
- Servers needed at 50% target utilization: 20,000,000 / 50,000 = 400.
Memory is the next check. If each connection costs about 10 KB for buffers and session state, 100,000 connections need about 1 GB per server. That is comfortable. If the real cost were 100 KB per connection, the same server would need 10 GB, so you would re-check the connection limit. Always test the estimate of cost per connection with a load test before you rely on it.
Connections also generate background traffic that is easy to forget. If every client sends a heartbeat every 30 seconds, 20 million connections produce about 667,000 heartbeats per second. That is nearly five times the peak message rate. Heartbeats are tiny, but a design that writes each one to a database would fail. This is why presence data belongs in memory or a fast key-value store with a time-to-live, not in the message database.
Storage: growth over time
Storage is rate times size times time, then multiplied by replication.
- Per day: 4,000,000,000 × 200 bytes = 800 GB.
- Per year: 800 GB × 365 ≈ 292 TB.
- Per year with 3 replicas: about 876 TB.
- Over five years with replicas: about 4.4 PB.
These numbers say two things about the design. First, a single database server cannot hold this data, so you need a store that shards by key. Second, most reads touch only recent messages. Users open a chat and scroll back a little. This lets you keep recent data on fast storage and move older data to cheaper storage.
Media changes the picture. If one in twenty messages carries a 200 KB photo, that adds 200,000 bytes × 200,000,000 photo messages per day, which is 40 TB per day. That dwarfs the 800 GB of text. Media is stored in object storage and referenced from messages by a link, so the message database stays small.
The estimate as a pipeline
Each result feeds a design decision.
Put the math in code
A small script keeps the assumptions in one place, so you can change a number and see every result update. This also makes the estimate reviewable.
import math
SECONDS_PER_DAY = 86_400
# Assumptions
dau = 100_000_000
msgs_per_user_per_day = 40
avg_msg_bytes = 200
peak_factor = 3
concurrent_ratio = 0.2
conns_per_server = 100_000
target_utilization = 0.5
replication = 3
heartbeat_interval_s = 30
# Traffic
msgs_per_day = dau * msgs_per_user_per_day
avg_qps = msgs_per_day / SECONDS_PER_DAY
peak_qps = avg_qps * peak_factor
# Connections
connections = int(dau * concurrent_ratio)
servers = math.ceil(connections / (conns_per_server * target_utilization))
heartbeats_per_s = connections / heartbeat_interval_s
# Storage
bytes_per_day = msgs_per_day * avg_msg_bytes
tb_per_year = bytes_per_day * 365 / 1e12
tb_per_year_replicated = tb_per_year * replication
print(f"messages/day: {msgs_per_day:,}")
print(f"average msgs/s: {avg_qps:,.0f}")
print(f"peak msgs/s: {peak_qps:,.0f}")
print(f"peak connections: {connections:,}")
print(f"chat servers: {servers}")
print(f"heartbeats/s: {heartbeats_per_s:,.0f}")
print(f"storage/day (GB): {bytes_per_day / 1e9:,.0f}")
print(f"storage/year (TB): {tb_per_year:,.0f}")
print(f"with replicas (TB): {tb_per_year_replicated:,.0f}")Running it prints these values:
messages/day: 4,000,000,000
average msgs/s: 46,296
peak msgs/s: 138,889
peak connections: 20,000,000
chat servers: 400
heartbeats/s: 666,667
storage/day (GB): 800
storage/year (TB): 292
with replicas (TB): 876Common errors
- Sizing for the average. Peak load decides capacity. Always apply a peak factor.
- Using registered users instead of active users. This inflates every number by a large and unknown factor.
- Forgetting heartbeats and other background traffic. Idle connections still cost requests.
- Ignoring replication and indexes. Raw data size is a lower bound. Replicas, indexes, and backups add to it.
- Mixing units. Keep bytes and bits apart. Network capacity is usually quoted in bits per second, storage in bytes.
- False precision. Report 46,000 messages per second, not 46,296.3. Answers within a factor of two are good enough, and the goal is to find the order of magnitude.
What to carry forward
The estimate produces a short list of facts that later chapters use: about 139,000 peak messages per second, about 20 million concurrent connections spread across several hundred servers, heartbeat traffic that exceeds message traffic, and storage that grows by hundreds of terabytes a year. These facts explain why the architecture separates stateless APIs from stateful chat servers, why presence uses an in-memory store, and why history needs a sharded database.
Exercise
Your product has 20 million DAU. Each user sends 25 messages per day at an average of 160 bytes. The peak factor is 4. Find the peak messages per second and the yearly storage without replication.
Show answer
Messages per day: 20,000,000 × 25 = 500 million. Average rate: 500,000,000 / 86,400 ≈ 5,787 per second. Peak rate: 4 × 5,787 ≈ 23,000 per second. Storage per day: 500,000,000 × 160 bytes = 80 GB. Per year: 80 GB × 365 = 29.2 TB.
Exercise
In the chapter's scenario, 20 million clients are connected and each sends a heartbeat every 15 seconds instead of every 30. How many heartbeats per second does the system receive, and how does it compare with the peak message rate of about 139,000 per second?
Show answer
20,000,000 / 15 ≈ 1,333,333 heartbeats per second. That is about 9.6 times the peak message rate. Halving the interval doubles the heartbeat load, so the interval is a trade-off between how quickly you detect offline users and how much load you generate.
Exercise
Why does the chapter size servers at 50% target utilization instead of 100%?
Show answer
Load is uneven and servers fail. At 100% planned utilization, a traffic spike or the loss of one server would push the remaining servers past their limit. Headroom lets the system absorb spikes and reconnect the clients of a failed server to the others.