Chat: Trade-Offs and Wrap-Up
Trade-offs are the gains and losses that come with each design choice. This lesson weighs the trade-offs and wraps up the chat design.
The connection cost over a longer run
A longer scripted run carries the same three arrival steps over more empty steps. The request counts grow for polling and stay flat for a held connection.
arrival_steps = [4, 9, 15]
steps = list(range(1, 22))
polling_requests = 0
polling_delivered = 0
for step in steps:
polling_requests += 1
if step in arrival_steps:
polling_delivered += 1
long_polling_requests = len(arrival_steps)
print("polling:", polling_requests, "requests,", polling_delivered, "messages")
print("long polling:", long_polling_requests, "requests,", polling_delivered, "messages")
print("WebSocket:", 1, "connection,", polling_delivered, "messages")Shorter heartbeat intervals cost more traffic
A demo fleet sends heartbeats at three different intervals. The printout shows the traffic each interval creates and the longest time presence can be stale.
phones = 600
for interval in [10, 30, 60]:
per_minute = phones * (60 // interval)
print("interval", interval, "- heartbeats per minute:", per_minute, "- stale up to", interval, "seconds")Keeping history costs storage every day
A scripted demo adds the same small number of stored messages each day. Keeping everything grows the store, while deleting old rows holds it flat.
messages_per_day = 5
days = 30
keep_all = messages_per_day * days
keep_last_week = messages_per_day * 7
print("stored after the run, keep everything:", keep_all)
print("stored after the run, keep the last week:", keep_last_week)Example
messages_per_day = 50000000
online_users = 10000000
average = round(messages_per_day / 86400)
peak = int(round(messages_per_day / 86400 * 3, -2))
gb_per_day = messages_per_day * 100 // 1000000000
servers = online_users // 100000
heartbeats_per_second = round(online_users / 30)
print("average messages per second:", average)
print("peak messages per second:", peak)
print("GB of new messages per day:", gb_per_day)
print("connection servers needed:", servers)
print("heartbeats per second:", heartbeats_per_second)Key points
- Connection choice: a WebSocket costs one held connection and removes repeated asking.
- Ordering choice: sequence numbers give a true reading order and cost one counter per conversation.
- Storage choice: one table per conversation loads fast and leaves many small tables to manage.
- Heartbeat traffic: 10,000,000 online users sending one heartbeat every 30 seconds create about 333,333 heartbeats per second.
- Peak sizing: the design is sized for about 1,700 messages per second at peak, not for the average.
In this example, the whole design is repriced in one run, from message rate to storage growth to the connection fleet and the heartbeat traffic. Every figure matches the estimates the design was built on.
Note
no choice in this design is free, and each cost named here buys a named gain.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
What happens when the heartbeat interval is made shorter?