TOUGH TONGUE AI
  • Home
    LogoTOUGH TONGUE AI

    RESOURCES

    Loading...

    HomeLibrarySD Mock · Design Facebook Messenger

    SD Mock · Design Facebook Messenger

    SD Mock · Design Facebook Messenger

    Overview

    Design a real-time messaging system like Facebook Messenger on a live architecture board — drag components, wire them up, and defend your design to an expert interviewer who watches every move.

    Your Instructions

    Design a chat system like Facebook Messenger

    A 20-minute mock system design interview. You drive the pace. Jump to the board when you are ready; skip capacity math if you want. The interviewer grades your thinking, not a template match.

    Two shared surfaces (only one is on screen at a time; use the panel arrow to switch):

    • Notepad. Shared notes. Requirements, capacity math, schema, and end-of-session feedback live here. You can type in it too.
    • Architecture board. Your design canvas. Drag typed components, wire them, label edges, mark scaled tiers as distributed. If it is not on the board, it is not in your design.

    Build incrementally while you talk. Trace one message end to end through your diagram. Argue the tradeoff, not "industry standard."

    Design choices interviewers probe

    Read this before you go in. These are the decisions, and why they matter.

    Protocol

    Clients never talk peer to peer. Everything goes through a chat service that receives, routes, relays, and holds messages for offline users.

    Polling. The client keeps asking "any new messages?" Mostly empty responses. Wasteful at scale.

    Long polling. The server holds the request until a message arrives or it times out. Better, but sender and receiver can land on different servers, disconnects are hard to detect, and quiet users still cycle connections.

    WebSocket. Client-initiated, persistent, bidirectional. Starts as HTTP, upgrades, works on ports 80/443. Best default for send and receive. Login, signup, and profile stay plain HTTP.

    WebSocket is not a rival to TCP. It is a persistent TCP connection with a thin handshake and framing layer, which is why it keeps TCP's ordering and reliability. Facebook's real mobile client uses MQTT over TCP instead: smaller frames on lossy networks. That is equally defensible if you can say why.

    Shape of the system

    Three kinds of pieces:

    1. Stateless services behind a load balancer: login, signup, profile, service discovery (which chat server should this client use).
    2. Stateful chat servers: each client holds a persistent socket to one server and sticks to it. Discovery picks by geography and capacity so you do not overload a box.
    3. Third party: push notifications (APNs / FCM) for offline devices.

    A and B are rarely on the same chat server. You need something that routes between servers (Kafka, a pub/sub topic, Redis pub/sub, RabbitMQ). Without that hop, "I sent hi" never reaches the other socket.

    One server holding every connection is a starting sketch only. Call it out as a single point of failure, then go to a fleet.

    Capacity (why you need a fleet)

    Method: DAU × messages/day × bytes/message, then convert to per-second rates. Rough numbers are fine; the method is what gets graded.

    For this interview, a sound landing is:

    • ~100M DAU × 40 msgs/day ≈ 4B msgs/day ≈ 46K msgs/sec average (peak 3 to 5×)
    • ~1KB per text message with metadata ≈ 4TB/day
    • The famous 65K limit is per client IP:port, not a server cap. A tuned box can hold 100K to 1M sockets; plan 10K to 50K per server for reliability, so concurrent users imply a fleet of hundreds of connection servers

    Chat is read/write ≈ 1:1 (not a read-heavy feed). Group fan-out is O(n) per member.

    Storage

    Two data types, two stores:

    • Profiles, friends, generic data: relational DB, with replication and sharding.
    • Chat history: a key-value / wide-column store (HBase at Messenger, Cassandra at Discord, DynamoDB is a fine analogue). Volume is huge, only recent chats are hot, but you still need random access (search, jump to a message). Read/write is about 1:1. KV scales horizontally with low latency; relational indexes rot on long-tail message data.

    Partition so a user's (or a channel's) messages stay together: hash on user ID, or channel_id as the partition key for groups. Redis caches recent threads so opening a chat does not hit the database.

    Message IDs and ordering

    created_at is not enough. Two messages can share a timestamp. The ID must be unique and time-sortable.

    Options:

    • Auto-increment (usually unavailable in NoSQL)
    • A global 64-bit generator (Snowflake-style)
    • A local per-conversation sequence (simpler: order only has to hold inside one chat)

    1:1 table keyed by message_id. Group table keyed by (channel_id, message_id) with channel_id as the partition key.

    One-to-one flow

    User A → chat server → mint a message_id → persist to the KV store. If B is online, forward to B's chat server and push down B's socket. If B is offline, send a push notification; the message waits in the store until reconnect.

    Usual contract: at-least-once delivery with per-conversation ordering. Persist before you ack.

    Multi-device

    Same account on a phone and a laptop. Each device stores cur_max_message_id. A message is new for that device if the recipient is the logged-in user and id > cursor. Each device pulls independently from the store, so both converge on the same history.

    Cross-device history wants strong reads. Live delivery often prefers availability. Pick one, and say what you sacrificed.

    Small groups

    For groups up to ~100, copy the message into each member's inbox (WeChat caps around 500 for this reason). Sync stays simple. It breaks at huge scale: a 100K-member group is a write fan-out explosion per message. If they ask about that, say you would stop copying per inbox and fan out on read, or cap group size.

    Presence

    Login writes online + last_active_at; logout writes offline. Naive online/offline on every TCP blip flaps the green dot (tunnels, flaky networks).

    Heartbeat instead: client pings every ~5s; mark offline if none arrive for ~30s. Fan out via pub/sub, one channel per friend pair, for small graphs. For a huge group, do not publish 100K events per status change. Fetch lazily on group entry, list refresh, or viewport.

    Failure

    When a chat server holding tens of thousands of sockets dies, you do not fail over live TCP. Clients detect the drop and reconnect through discovery / the load balancer to a new server. Retry and queue so in-flight messages are not lost. Replicate stored history (copies, or erasure coding) so one node loss is not data loss.

    Media

    Compression, object storage, thumbnails. The chat message stores a URL, not the file. Serve through a CDN. End-to-end encryption is a wrap-up topic, not required up front.

    Reference solution

    One complete design you can study, then rebuild in your own words. Do not recite it. In the interview, draw your graph and defend the choices.

    Requirements (lock these or take the defaults)

    • 1:1 chat and small group chat (max ~100 members)
    • Web and mobile
    • ~100M DAU
    • Text and media; text under 100k characters
    • Online presence, multi-device login, push notifications
    • History kept; users should see the same history on every device
    • End-to-end encryption: out of scope unless there is time at the end

    Capacity

    • 100M DAU × 40 msgs/day = 4B msgs/day ≈ 46K msgs/sec average (peak 3 to 5×)
    • ~1KB per message with metadata ≈ 4TB/day of new data
    • Plan 10K to 50K WebSocket connections per server → a fleet of hundreds of connection servers
    • Chat is read/write ≈ 1:1. Peak is 3 to 5× average.

    Components to put on the board

    How the graph works: clients never talk to each other. Each holds a sticky socket to one chat server. That server writes the message, then either pushes it to the recipient's server (if they are online on a different box) or hands it to push + the store (if they are not). Login and profile stay off this path.

    Web Client, Mobile Client
        → Load Balancer
        → WebSocket Server fleet (mark distributed)
        → Chat Service
            → Cassandra (history)
            → Redis (recent threads)
            → Kafka / Pub-Sub Topic  → peer WebSocket Servers → recipients
            → Blob Store + CDN (media URLs only)
            → Third-party API (APNs / FCM) for offline push
    

    Components:

    • Clients (Web, Mobile). The user's apps. They open one persistent connection for chat send/receive (WebSocket, or MQTT on mobile). Everything else (login, profile) is ordinary HTTP.
    • Load balancer. Spreads millions of incoming sockets across the chat-server fleet. After the handshake, the client sticks to one server. Discovery (geo, capacity) helps pick which server so you do not melt one box.
    • WebSocket / chat-server fleet (stateful, mark distributed). Holds the live connections. This is the only place that can push to a client. Each server keeps a map of user → connection. If the box dies, clients reconnect elsewhere; you do not fail over live TCP.
    • API servers (stateless). Login, signup, profile, friends. Sit behind the same or a separate load balancer. They do not hold chat sockets.
    • Chat service. The brain of one send: mint a message_id, persist, publish for the other servers, ack the sender. Keep this as its own box (or name it on the chat server) so the walk is traceable.
    • Kafka / pub-sub. The hop between chat servers. A's server almost never holds B's socket, so it publishes "new message for B." B's server is subscribed, picks it up, and pushes down B's connection. Without this, the design only works if both users land on the same machine.
    • Cassandra (or similar KV). Durable chat history. Write-heavy, ~1:1 reads, random jump-to-message still has to work. Shard so one conversation's messages stay together. Replicate so one node loss is not data loss.
    • Redis. Recent threads and presence (online, last_active). Opening a chat should hit cache, not the full history store. Heartbeats land here so the green dot does not flap.
    • Blob store + CDN. Photos and files live in object storage. The chat row stores only a URL. The CDN absorbs downloads so chat servers never ship bytes of media.
    • Third-party push (APNs / FCM). When the recipient has no live socket, the chat service fires a push. The message is already in Cassandra; on reconnect the device pulls anything newer than its cursor.

    Walk one message: A sends "hi" to B

    1. A is connected to Chat Server 1 over WebSocket (or MQTT on mobile).
    2. Server 1 mints a time-sortable message_id (Snowflake-style, or a per-conversation counter).
    3. Persist the message to Cassandra (or another KV / wide-column store). Ack A only after the write.
    4. Publish onto Kafka / pub-sub so the server that holds B can pick it up. A and B are usually on different boxes.
    5. If B is online: B's server pushes down B's socket.
    6. If B is offline: fire APNs / FCM; the row waits in the store until B reconnects.
    7. Each of B's devices (phone, laptop) has its own cur_max_message_id. A message is new on that device if id > cursor. Devices pull independently, so they converge.

    Group of ~100: copy the message into each member's inbox. Cheap here. A 100K-member group explodes write fan-out; cap size or switch to fan-out on read.

    Presence: heartbeat every ~5s; offline after ~30s of silence. Do not flip the green dot on every TCP blip.

    Server death: live TCP does not fail over. Clients reconnect via the load balancer / discovery to a new server. History is replicated so one node loss is not data loss.

    Schema

    Users(id, name, last_active)
    Messages(id, sender_id, conversation_id, text, media_url, ts)
    Conversations(id, name?)
    ConversationUsers(conversation_id, user_id)
    
    • 1:1 rows keyed by message_id
    • Group rows keyed by (conversation_id, message_id) with conversation_id as the partition key
    • Shard by conversation_id or user ID; replicate 3×
    • Profiles and friends stay in a relational DB

    Scaling and wrap-up

    • WebSocket fleet scales horizontally behind the load balancer
    • Kafka partitions by user or conversation
    • Cassandra scales writes; Redis absorbs recent reads; CDN absorbs media
    • First metrics: delivery latency, socket churn, queue lag
    • If time: encryption, client-side cache, geo edge caches, what breaks first at 10×

    Equally defensible variants

    RabbitMQ or Redis pub/sub instead of Kafka. DynamoDB instead of Cassandra. Long-polling as a fallback on hostile networks. MQTT over TCP instead of raw WebSocket on mobile. What matters is that you can argue the tradeoff.

    More in System Design Interview Prep: AI Coach Edition

    • SD Coach · File Sync: Dropbox and Google Drive
    • SD Mock · Design Dropbox
    • SD Coach · Transactional Systems
    • SD Mock · Design Ticketmaster
    • SD Coach · Batch and Stream Pipelines
    • SD Coach · Real-Time Chat and Collaboration
    • SD Mock · Design Uber Backend
    • SD Mock · Design Yelp / Nearby Friends
    • SD Coach · Media Streaming
    • SD Mock · Design YouTube / Netflix
    • SD Mock · Design Instagram
    • SD Coach · Agentic AI
    View the full course (43 sessions) →