KatInterview

Design a Request-Response Based Instant Messaging Architecture

System DesignModel answer
MediumModel answerSystem Designsystem-designmessagingdistributed-systems

Last updated

Architect a 1-on-1 instant messaging backend relying exclusively on standard HTTP request-response APIs, managing message synchronization, ordering, idempotency, and push delivery at scale.

Architect the backend system for a direct (1-on-1) instant messaging application similar to Messenger, operating under a strict architectural restriction: no persistent bidirectional connections (e.g., WebSockets, gRPC streams, SSE) are allowed.

All client-to-server interaction must occur over standard HTTP request/response paradigms. Users must be able to send text messages, retrieve history across multiple registered devices, and reliably receive incoming messages in correct chronological order.

Core Objectives & Requirements

  1. Functional Requirements:

    • One-on-one text messaging with multi-device support per user.
    • Strict chronological ordering of messages per conversation visible to both sender and recipient.
    • Complete message synchronization on new device logins or re-connections.
    • Reliable delivery guarantee: zero message loss and duplicate message handling (idempotency).
  2. System Constraints & Architectural Boundary:

    • No persistent connection channels: Every operation must be an isolated request/response call.
    • Explain how active (foreground), background, and offline devices learn about incoming messages without an open channel.
    • Define concrete capacity estimations and explain how storage and network infrastructure scale.

Required Discussion Structure

  • Part 1: Requirements, Sizing, APIs, and Data Storage Model
    • Define system requirements, scale assumptions, HTTP API definitions, database schema, sequence/ordering generation, and end-to-end write lifecycle.
  • Part 2: Real-Time Synchronization Without Persistent Sockets
    • Contrast alternative real-time delivery mechanics (short polling, long polling, push wakeups).
    • Design a delta-sync mechanism (cursor/sequence-based) ensuring gapless state recovery.
    • Explain how to make non-blocking/empty status checks scale efficiently.
  • Part 3: Multi-Device Fanout, Failure Modes, and Observability
    • Multi-device message dispatch, queueing, and idempotency key handling.
    • Failure scenarios (network drops, push notification losses, out-of-order retries, long-disconnected devices).
    • Monitoring metrics to measure end-to-end latency and missing delivery detections.

Hints

Hint 1

Think about how git or database replication works: if every user has a monotonically increasing sequence ID for incoming events, catching up is just requesting events after a given cursor.

Hint 2

To keep short/long polling cheap on DB resources, use a lightweight layer like Redis or an in-memory key-value cache to hold the latest sequence number per user before touching storage.

Hint 3

Distinguish between foreground sync (low-latency HTTP long polling or optimized adaptive short polling) and background/offline notification (APNs / FCM push triggers).