BLUEPRINT #02Social & Real-Time
Design a Real-Time Chat & Instant Messaging System
Referenced Architecture Primitives (5)
Click any primitive to study its algorithmic deep dive10-Stage Structure:1. Requirementsβ2. Sizingβ3. Topologyβ4. Data Modelβ5. AWS Topologyβ6. Deep-Diveβ7. Failuresβ8. SRE Playbooks
1. Problem Statement & Scope Clarification
System Mission
Design a globally distributed, highly scalable, real-time instant messaging platform (similar to WhatsApp, Slack, and Discord) capable of supporting ultra-low latency 1-on-1 direct messaging, group conversations (up to 1,000 members), real-time presence tracking (Online/Offline/Last Seen), multi-device synchronization, read receipts, and durable offline catch-up delivery.
Functional Requirements
- 1-on-1 Direct Messaging: Bi-directional real-time text and media messaging with delivery confirmation ().
- Group Chats (Fan-Out): Scalable group channels supporting up to 1,000 participants with read-state tracking.
- Presence & Heartbeat Engine: Real-time user online/offline status detection with debounce mechanisms to suppress flapping.
- Message Delivery Receipts: Three-stage message lifecycle states:
SENT(Server Ack),DELIVERED(Device Ack), andREAD(User Seen). - Offline Message Synchronization: Durable message storage allowing offline devices to fetch missed messages using sequential sync cursors upon reconnect.
- Push Notifications: Fallback push notification dispatch (Apple APNs / Google FCM via Amazon SNS) when a recipient is disconnected.
Non-Functional Requirements (SLAs & SLOs)
- High Availability: uptime SLA globally across multi-AZ and multi-region AWS deployments.
- Latency:
- 1-on-1 Message Delivery: , .
- Presence State Broadcast: .
- Data Durability: Zero message loss () for all acknowledged messages.
- Connection Scale: Support concurrent active persistent WebSocket connections.
2. Capacity & Scale Estimation (Back-of-the-Envelope Math)
User & Traffic Profile
- Daily Active Users (DAU): ().
- Peak Concurrent WebSocket Connections: ().
- Daily Messages Sent: (5 Billion messages/day).
- Average Message Size: (Text + metadata headers).
Throughput Derivations
- Average Ingest QPS:
- Peak Ingest QPS ( multiplier):
- Group Fan-Out Amplification ( average group delivery multiplier):
Memory & Connection Fleet Sizing
- RAM per Idle WebSocket Connection: (TCP socket buffer + SSL session context in kernel/user space).
- Total In-Memory Socket State:
- WebSocket Gateway Cluster Provisioning:
Using
c6g.2xlargeECS Fargate tasks (8 vCPU, 16 GB RAM), capped at sockets per task:
Storage Footprint (5-Year Horizon)
- Daily Ingestion Storage:
- 5-Year Storage Footprint (Single Copy):
- Provisioned on Amazon DynamoDB with tiered offload of older messages () to Amazon S3 Standard-IA / Glacier.
3. High-Level Architecture & AWS Component Mapping
Interactive Architecture DiagramSynthesizing vector architecture diagram...
4. API Interface Design & Wire Protocols
1. Client-to-Gateway WebSocket JSON Protocol
json// 1. Client Sending a Message { "action": "SEND_MESSAGE", "client_msg_id": "9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d", "conversation_id": "conv_991823", "recipient_id": "usr_bob_402", "content_type": "TEXT", "body": "Hey Bob, let's review the architectural spec.", "timestamp_client_ms": 1767225600120 } // 2. Server Acknowledgment to Sender (Status: SENT) { "event": "MESSAGE_SENT_ACK", "client_msg_id": "9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d", "server_msg_id": "718293847561029384", "conversation_id": "conv_991823", "sequence_number": 10482, "timestamp_server_ms": 1767225600135 } // 3. Server Delivery to Receiver Device { "event": "RECEIVE_MESSAGE", "server_msg_id": "718293847561029384", "conversation_id": "conv_991823", "sender_id": "usr_alice_101", "content_type": "TEXT", "body": "Hey Bob, let's review the architectural spec.", "sequence_number": 10482, "timestamp_server_ms": 1767225600135 } // 4. Client Read Receipt Update { "action": "READ_RECEIPT", "conversation_id": "conv_991823", "last_read_sequence_number": 10482, "timestamp_read_ms": 1767225605000 }
5. Data Models & DynamoDB Single-Table Schema
To achieve sub-millisecond query performance at PB scale, we utilize a single-table DynamoDB design partitioned by conversation_id and indexed by time-sortable Snowflake IDs.
DynamoDB Table: ChatPlatformTable
PK (Partition Key) | SK (Sort Key) | Attributes & Payloads | Access Pattern / Query |
|---|---|---|---|
USER#<user_id> | CONN#<connection_id> | gateway_ip, connected_at, ttl_epoch | Active WebSocket connection lookup |
USER#<user_id> | CONV#<conversation_id> | unread_count, last_read_seq, joined_at | Fetch user's inbox & conversation list |
CONV#<conversation_id> | META | type (DIRECT/GROUP), members_count, name | Conversation metadata |
CONV#<conversation_id> | MEMBER#<user_id> | role (ADMIN/MEMBER), joined_at | Fetch all members of a group chat |
CONV#<conversation_id> | MSG#<snowflake_msg_id> | sender_id, body, type, status, seq | Fetch paginated chat history via range query |
Range Query for Message History & Offline Catch-Up
json{ "TableName": "ChatPlatformTable", "KeyConditionExpression": "PK = :conv_pk AND SK BETWEEN :last_sync_sk AND :latest_sk", "ExpressionAttributeValues": { ":conv_pk": {"S": "CONV#conv_991823"}, ":last_sync_sk": {"S": "MSG#718293800000000000"}, ":latest_sk": {"S": "MSG#718293999999999999"} }, "Limit": 50, "ScanIndexForward": true }
Part 2: Production Deep-Dive Locked1 Coin = 24 Hours
Unlock Complete Architecture & Production Runbooks
Your Balance:40 Coins
You have explored the free architectural preview (~44%). Spend 1 Coin to unlock the remaining 6 production deep-dive sections for a full 24 hours.
Sections Included in This 24-Hour Pass:
6. Detailed Request Flow & Communication Workflows
7. Architectural Trade-Off Analysis & Matrix
8. Critical Failure Modes & Edge Case Engineering
9. Production Pitfalls & Anti-Patterns (The "Gotchas")
10. Production Runbook & Observability Guide
11. Interview Strategy & System Design Rubric
Keeps page unlocked for exactly 24 hoursSpend coins to fund LLM & compute infrastructure