๐ŸŽ“ A1Learner
๐Ÿ”
0 of 42 lessons completed

Design a Chat System: The Ask

A chat system is a service that sends short text messages between people in near real time. This lesson clarifies the ask and estimates the scale.

Splitting the ask into two kinds

The ask splits into what the service must do and how well it must do it. The printout counts the recorded decisions of each kind.

requirements = [
    ("send a message to one person", "functional"),
    ("send a message to a group", "functional"),
    ("show whether a contact is online", "functional"),
    ("answer within a moment, even at peak times", "non-functional"),
    ("keep working when one server fails", "non-functional"),
]
counts = {"functional": 0, "non-functional": 0}
for text, kind in requirements:
    counts[kind] += 1
for kind in ["functional", "non-functional"]:
    print(kind, "decisions:", counts[kind])

From a daily count to a per-second rate

One day holds 86,400 seconds. Dividing the daily count by that figure gives the average rate, and busy times run higher.

messages_per_day = 50000000
seconds_per_day = 86400
average = round(messages_per_day / seconds_per_day)
peak = int(round(messages_per_day / seconds_per_day * 3, -2))
print("messages per day:", messages_per_day)
print("average per second:", average)
print("peak per second:", peak)

Growing one message into a year of storage

Each message stores a small fixed amount of data. The printout grows that amount to one day and then to one year.

messages_per_day = 50000000
bytes_per_message = 100
bytes_per_day = messages_per_day * bytes_per_message
gb_per_day = bytes_per_day // 1000000000
tb_per_year = round(bytes_per_day * 365 / 1000000000000, 1)
print("bytes per message:", bytes_per_message)
print("GB per day:", gb_per_day)
print("TB per year:", tb_per_year)

Example

messages_per_day = 50000000
bytes_per_message = 100
online_users = 10000000
connections_per_server = 100000
average = round(messages_per_day / 86400)
gb_per_day = messages_per_day * bytes_per_message // 1000000000
servers = online_users // connections_per_server
print("average messages per second:", average)
print("GB of new messages per day:", gb_per_day)
print("users online at once:", online_users)
print("connection servers needed:", servers)

Key points

  • One-to-one chat: two people exchange messages in a private conversation.
  • Group chat: several people exchange messages in one shared conversation.
  • Delivery proof: the sender gets a sign that a message reached the other person.
  • Presence: the service shows whether a contact is online now.
  • Message rate: 50,000,000 messages a day averages 579 messages per second.
  • Peak rate: busy times run at about 1,700 messages per second.
  • Storage growth: one message stores 100 bytes, so a day adds 5 GB and a year adds about 1.8 TB.
  • Online load: 10,000,000 users can be online at the same time.

In this example, one set of starting figures gives the message rate, the daily storage growth and the size of the connection fleet. The design is sized by these estimates from the start.

Note

the scope is text messages in this design, with calls and photos left out.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

50,000,000 messages a day averages about how many messages per second?