Sub topic 1
Clarifying Requirements and Scope
Prerequisites
This chapter assumes basic client-server and HTTP concepts, database and key-value store familiarity, and a basic understanding of TCP and persistent connections.
Clarifying requirements and scope
Start by defining what the chat system must do:
- Support one-to-one and group conversations.
- Deliver text messages in near real time.
- Preserve message history and per-conversation order.
- Show whether users are online.
- Support multiple devices and offline users.
Then define non-functional requirements. These describe how well the system must work. Set targets for message latency, availability, maximum group size, data retention, privacy, and abuse protection.
Define “delivered” precisely. A message may be accepted by the server, stored durably, received by a recipient device, or displayed to the user. These are different guarantees.
Estimating traffic, connections, and storage
Use simple assumptions before choosing infrastructure. For example:
| Quantity | Example assumption |
|---|---|
| Daily active users | 1,000,000 |
| Messages per user per day | 20 |
| Average message size | 500 bytes |
This produces 20 million messages per day, or about 231 messages per second on average. Design for a higher peak, such as 10 times the average. Storage also includes IDs, timestamps, indexes, replicas, and media metadata, so the raw message size is only a lower bound.
Estimate concurrent connections separately. A user may keep one persistent connection per active device. Connection count determines chat-server memory and network capacity.
Polling, long polling, and WebSocket
Polling sends repeated HTTP requests. It is simple but wastes requests when nothing changes. Long polling holds a request until an event occurs, reducing waste but adding timeout and reconnect handling.
WebSocket upgrades an HTTP connection into a bidirectional, persistent channel. The server can send events immediately, which suits chat and presence. It requires connection management, authentication, heartbeats, and routing when many servers are involved.
High-level architecture
Stateless API servers handle authentication, conversation management, and history queries. Stateful chat servers hold live connections and route events. A durable message store records accepted messages. A broker or routing layer moves events between API workers, chat servers, and notification services.
Modeling users, conversations, and messages
Use separate records for users, conversations, participants, and messages. A message should include a conversation ID, sender ID, server-assigned sequence number, client message ID, creation time, and content reference. Keep large attachments outside the message row.
Per-conversation sequence numbers make ordering explicit. Client-generated IDs let retries be recognized and deduplicated.
Exercise
Why should a message have both a client message ID and a server sequence number?
Show answer
The client ID detects retries, while the sequence number provides ordering within the conversation.