Skip to main content
BLUEPRINT #02Mobile & Offline Architecture

Design Mobile Chat Client Architecture

Target AWS Architecture:DynamoDBS3ElastiCacheSNS
10-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

System Mission

Design a battery-optimized, ultra-responsive, offline-resilient mobile chat client architecture managing bidirectional persistent WebSocket connections, encrypted local SQLite storage (SQLCipher), end-to-end encryption (E2EE Signal Protocol / Double Ratchet), binary Protocol Buffers serialization, and silent background push synchronization.

Interactive Architecture Diagram
Synthesizing vector architecture diagram...

Functional Requirements

  1. Real-Time 1:1 and Group Chat: Send and receive text, media pointers, reactions, and threaded replies over low-latency WebSockets.
  2. Offline-First Messaging & Local Outbox: Messages authored offline are immediately rendered in the UI with a "clock" icon, committed to local SQLite, and dispatched sequentially upon reconnection.
  3. Delivery & Read Receipts: Real-time multi-state message tracking (SENDING β†’\to SENT_TO_SERVER β†’\to DELIVERED_TO_DEVICE β†’\to READ_BY_RECIPIENT).
  4. End-to-End Encryption (E2EE): Implement Double Ratchet (Signal Protocol) cryptographic session state with private keys stored in hardware keystores.
  5. Silent Push Background Synchronization: Receive background pushes (content-available: 1), waking the app for 30s to decrypt and store incoming messages in SQLite prior to user interaction.

Non-Functional Requirements (SLAs/SLOs)

  • Send-to-Display Latency: <100Β ms< 100\text{ ms} on 4G/5G; instant 0Β ms0\text{ ms} optimistic local UI rendering.
  • Battery & Radio Overhead: <2%< 2\% total device battery consumption per day for background presence.
  • Local Database Encryption: Full AES-256 encryption at rest (zero plaintext leakage in flash memory).
  • Wire Payload Efficiency: β‰₯80%\ge 80\% payload size reduction via binary Protocol Buffers vs standard JSON.
  • Reconnection Recovery: Automatic session resumption in <300Β ms< 300\text{ ms} after transient connection drop.

Out-of-Scope

  • Voice/Video calling media streaming (WebRTC RTP/RTCP transport pipelines covered in dedicated VoIP blueprints).
  • Complex channel broadcast feeds with >100,000> 100,000 concurrent subscribers.

2. Capacity & Scale Estimation

Device & Traffic Scale

  • Mobile Active Installs: 100Β Million100\text{ Million} devices (30Β Million30\text{ Million} DAU).
  • Concurrent Connected Sockets: Peak 10Β Million10\text{ Million} simultaneous WebSocket connections during peak hours.
  • Daily Messages Processed: 500Β Million500\text{ Million} messages/day. AverageΒ MessageΒ QPS=500Γ—10686,400Β sβ‰ˆ5,787Β msgs/sΒ (Peak:Β 20,000Β msgs/s)\text{Average Message QPS} = \frac{500 \times 10^6}{86,400 \text{ s}} \approx 5,787 \text{ msgs/s (Peak: } 20,000\text{ msgs/s)}

Bandwidth & Serialization Efficiency

  • Serialization Comparison:
    • Standard JSON Frame: β‰ˆ450Β Bytes\approx 450\text{ Bytes} (message_id, conversation_id, sender_id, recipient_id, timestamp, body, signature).
    • Optimized Protobuf Binary Frame: 75Β Bytes\mathbf{75\text{ Bytes}} (83.3%83.3\% bandwidth reduction). DailyΒ ClientΒ NetworkΒ EgressΒ (Protobuf)=500Γ—106Γ—75Β Bytes=37.5Β GB/day\text{Daily Client Network Egress (Protobuf)} = 500 \times 10^6 \times 75\text{ Bytes} = \mathbf{37.5\text{ GB/day}} DailyΒ ClientΒ NetworkΒ EgressΒ (JSON)=500Γ—106Γ—450Β Bytes=225Β GB/day\text{Daily Client Network Egress (JSON)} = 500 \times 10^6 \times 450\text{ Bytes} = \mathbf{225\text{ GB/day}}
  • Heartbeat Bandwidth: 10M connected clients sending a 2Β Byte2\text{ Byte} ping every 60s β€…β€ŠβŸΉβ€…β€Š107Γ—2Β B60Β sβ‰ˆ333Β KB/sβ‰ˆ2.67Β Mbps\implies \frac{10^7 \times 2\text{ B}}{60\text{ s}} \approx 333\text{ KB/s} \approx \mathbf{2.67\text{ Mbps}}.

3. High-Level Architecture & AWS Component Mapping

Interactive Architecture Diagram
Synthesizing vector architecture diagram...

Component Responsibility Breakdown

ComponentTechnologyOperational Role & Configuration
Local Encrypted StoreSQLCipher (AES-256)Encrypted relational store holding chat history, ratchet session keys, and pending outbound messages.
Hardware Key VaultiOS Keychain Secure Enclave / Android KeystoreStores root master identity keys; cryptographic signing occurs inside hardware security modules (HSM).
Connection ManagerNative Coroutines / Swift ConcurrencyState machine controlling WebSocket lifecycle, reconnects, and battery-aware ping intervals.
WebSocket Cloud GatewayAmazon API Gateway WebSocketManages 10M persistent TCP connections, performs TLS termination, and routes frames to ECS workers.
Push Notification Relay (APNs / FCM)Emits silent push wakes (content-available: 1 / priority: high) for offline clients to download messages in background.

4. API Interface Design & Protobuf Message Schema

1. Protocol Buffers Wire Schema (chat_protocol.proto)

protobuf
syntax = "proto3";
package chat.mobile.v1;

enum DeliveryStatus {
  STATUS_UNKNOWN = 0;
  SENDING = 1;
  SENT_TO_SERVER = 2;
  DELIVERED = 3;
  READ = 4;
}

message ChatMessageFrame {
  string message_id = 1;
  string conversation_id = 2;
  string sender_id = 3;
  int64 timestamp_ms = 4;
  DeliveryStatus status = 5;
  bytes encrypted_payload = 6;      // Double Ratchet encrypted ciphertext
  bytes ephemeral_public_key = 7;   // Ratchet ephemeral key (32 bytes)
  int32 ratchet_counter = 8;
  bytes hmac_auth_tag = 9;
}

message ReceiptBatchFrame {
  string conversation_id = 1;
  DeliveryStatus status = 2;
  repeated string message_ids = 3;
  int64 receipt_timestamp_ms = 4;
}

2. WebSocket Frame Envelope

json
{
  "action": "send_message",
  "payload_base64": "Cg9tc2dfOTk4MTI0YTg3YzESCGNvbnZfMTAxEgh1c3JfMjAyGICAkL21sTIy..."
}

5. Data Models & Storage Architecture

Interactive Architecture Diagram
Synthesizing vector architecture diagram...

Local SQLCipher Schema (chat_encrypted_storage.db)

sql
-- Encrypted Conversations Table
CREATE TABLE conversations (
    conversation_id TEXT PRIMARY KEY,
    peer_user_id TEXT NOT NULL,
    last_message_id TEXT,
    unread_count INTEGER NOT NULL DEFAULT 0,
    updated_at INTEGER NOT NULL
);

-- Encrypted Local Messages Table
CREATE TABLE local_messages (
    message_id TEXT PRIMARY KEY,
    conversation_id TEXT NOT NULL,
    sender_id TEXT NOT NULL,
    plaintext_body TEXT NOT NULL,
    delivery_status TEXT CHECK (delivery_status IN ('SENDING', 'SENT', 'DELIVERED', 'READ')),
    is_outgoing INTEGER NOT NULL, -- 1=true, 0=false
    timestamp_ms INTEGER NOT NULL
);
CREATE INDEX idx_conversation_timeline ON local_messages(conversation_id, timestamp_ms DESC);

-- Double Ratchet Cryptographic Session State
CREATE TABLE ratchet_sessions (
    conversation_id TEXT PRIMARY KEY,
    root_key BLOB NOT NULL,
    sending_chain_key BLOB NOT NULL,
    receiving_chain_key BLOB NOT NULL,
    sending_counter INTEGER NOT NULL DEFAULT 0,
    receiving_counter INTEGER NOT NULL DEFAULT 0,
    last_updated INTEGER NOT NULL
);

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 (~42%). 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. Core Algorithms & Deep-Dive Workflows
7. Architectural Trade-Off Matrix & Primitive Links
8. Critical Failure Modes, Resiliency & Edge Cases
9. Production Pitfalls & Anti-Patterns (The "Top 5 Gotchas")
10. Production Runbook & Observability Guide
11. System Design Interview Rubric & Deep-Dive Strategy
Keeps page unlocked for exactly 24 hoursSpend coins to fund LLM & compute infrastructure