Sub topic 5
Modeling users, conversations, and messages
This chapter assumes you know basic relational and key-value database ideas, and that you have seen the high-level split between stateless APIs and stateful chat servers. Here we decide what data the system stores and how it is keyed.
Three core entities
A chat system needs three entities. Everything else, such as read receipts or reactions, hangs off them.
- User: an account with an ID, a display name, and profile fields.
- Conversation: a one-to-one chat or a group. It has an ID, a type, and a set of members.
- Message: one item inside a conversation. It belongs to exactly one conversation.
Treat a one-to-one chat as a conversation with two members. A single model then covers both cases, and group chat later needs no new tables.
The message record
A message needs enough fields to order it, deduplicate it, and display it.
| Field | Purpose |
|---|---|
| chat_id | The conversation the message belongs to. |
| seq | Position within the chat, assigned by the server. |
| sender_id | The user who sent it. |
| client_msg_id | An ID the client generates, used to detect retries. |
| text | The body. Media is stored as a reference, not inline. |
| created_at | Server timestamp, for display. |
Two fields deserve explanation. The seq is a per-chat counter, so ordering only has to be correct inside one conversation. The client_msg_id lets the server recognize a resent message and avoid creating a duplicate. Later chapters cover both in detail. For now, include them in the model from the start, because adding them later means rewriting stored data.
Membership
Membership links users to conversations. Keep it as its own record rather than a list inside the conversation. You will ask two questions often: "who is in this chat?" and "which chats is this user in?" A separate record, indexed both ways, answers both quickly.
Store per-member state here too. A useful example is last_read_seq, the highest sequence number the member has seen. Unread counts then become a subtraction: the chat's latest seq minus last_read_seq.
Choosing the key
The most common read is "give me the latest messages in this chat." So the message key should be the chat ID first, then the sequence number. All messages for one chat then sit together and are already sorted.
messages = {} # key: (chat_id, seq) -> message
def latest(chat_id, limit, last_seq):
start = max(1, last_seq - limit + 1)
return [messages[(chat_id, s)] for s in range(start, last_seq + 1)
if (chat_id, s) in messages]
for i in range(1, 6):
messages[("c1", i)] = {"seq": i, "text": f"m{i}"}
assert [m["seq"] for m in latest("c1", 2, 5)] == [4, 5]This is a range read on a partition. It is cheap, and it is why this key shape is common in chat designs. Storage choices are covered in a later chapter.
Common errors
- Using a global timestamp as the order. Clocks on different servers disagree, so two messages can swap places. Use
seq. - Embedding messages inside the conversation record. The record grows without bound and every write touches it.
- Treating one-to-one chat as a special case. It forces duplicate code paths for sending, history, and sync.
- Storing media bytes in the message row. Store a reference to object storage instead.
Exercise
A user opens the app and needs to show every chat with its unread count. Which fields are used, and which record holds them?
Show answer
Look up the user's membership records to get each chatid and lastreadseq. Compare lastread_seq with the chat's latest seq. The difference is the unread count.