Chat: Storing Messages and Proving Delivery
Message storage is the saved copy of every message, kept so it can be delivered and read later. This lesson designs the data model for that storage and for delivery proof.
Counting the rows a load must touch
Loading one conversation from its own table touches only its rows. Loading the same conversation by scanning one big shared table touches every row in that table.
tables = {
"chat A": ["a1", "a2", "a3", "a4"],
"chat B": ["b1", "b2", "b3"],
"chat C": ["c1", "c2", "c3", "c4", "c5"],
}
own_rows = len(tables["chat A"])
big_rows = sum(len(rows) for rows in tables.values())
print("rows loaded:", own_rows)
print("rows touched from its own table:", own_rows)
print("rows touched scanning one big table:", big_rows)A stored state moving step by step
The state lives on the stored row, not just on a screen. Each step updates the row and the printout reads the row back.
row = {"text": "hello", "state": "sent"}
print("stored state:", row["state"])
row["state"] = "delivered"
print("stored state:", row["state"])
row["state"] = "read"
print("stored state:", row["state"])Stored rows waiting for an offline receiver
The receiver is offline, so stored messages wait in sequence order. When the receiver returns, the waiting rows are delivered in that same order.
stored = [(1, "are you free today"), (2, "yes, five works"), (3, "see you then")]
receiver_online = False
print("waiting while offline:", len(stored))
receiver_online = True
delivered = 0
if receiver_online:
for seq, text in sorted(stored):
delivered += 1
print("delivered", seq, text)
print("delivered after return:", delivered)Example
tables = {
"chat A": [{"seq": 1, "text": "did it work", "state": "read"}, {"seq": 2, "text": "yes, it worked", "state": "read"}],
"chat B": [{"seq": 1, "text": "the photos are up", "state": "delivered"}],
}
new_row = {"seq": 2, "text": "thanks", "state": "sent"}
tables["chat B"].append(new_row)
new_row["state"] = "delivered"
loaded = tables["chat B"]
print("rows loaded for chat B:", len(loaded))
for row in sorted(loaded, key=lambda r: r["seq"]):
print(row["seq"], row["text"], "-", row["state"])
print("total stored rows:", sum(len(rows) for rows in tables.values()))Key points
- Stored row: one saved record holding a message, its conversation and its delivery state.
- Delivery states: the stored labels that record how far a message has travelled.
- Sent: the server has accepted the message and stored it.
- Delivered: the message has reached the receiver phone.
- Read: the receiver has opened the conversation and seen the message.
- Conversation table: one table holds the messages of a single conversation.
- Shared table: one table holds the messages of every conversation.
- Offline delivery: a stored message waits and is delivered when the receiver comes back online.
In this example, each conversation keeps its own table of stored rows and a new message joins one table with the state sent. The state moves to delivered, and loading that table returns its rows in sequence order with their states.
Note
an index on a shared table can cut a scan down, but its rows still live in one large table.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Delivery states
| State | Meaning |
|---|---|
| Sent | The server has accepted and stored the message. |
| Delivered | The message has reached the receiver phone. |
| Read | The receiver has opened the conversation and seen the message. |
Exercise
A message shows the state read. What does that state prove?