Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

System Design blueprint · Messaging & Collaboration

Real-time chat over WebSockets

Persistent connections, ordered messages and presence: the core of every messaging app.

Steps
6
Subjective questions
32
Objective questions
32

The problem and the outcome

Problem: Polling for new messages wastes battery and still feels laggy.

Outcome: Messages arrive in order in under a second, and offline users still get notified.

Architecture

Sender app01WebSocketgateway02Chat servicesequence IDs03Message storewrite-heavy DB04PresenceRedisPub/Subper channel05RecipientgatewayPush if offline06persistonline?offline

How it works, step by step

  1. Each app keeps one persistent WebSocket connection open.
  2. Gateways hold millions of open connections and keep no other state.
  3. The chat service stamps a per-conversation sequence ID, so messages stay in order.
  4. Messages are written to a store built for heavy writes before delivery.
  5. Pub/Sub routes each message to whichever gateway holds the recipient's connection.
  6. If presence says the user is offline, a push notification goes out instead.

Tech stack

  • WebSocket gateway
  • Redis Pub/Sub or Kafka
  • Cassandra / ScyllaDB for messages
  • Redis presence with TTL
  • APNs / FCM for offline push

Key design decisions

  • Persistent WebSocket connections, so the server can push messages instantly instead of being polled.
  • A sequence number per conversation, so clients order messages and detect gaps after reconnecting.
  • A write-heavy store partitioned by conversation ID, sorted by time, for fast 'latest 50 messages' reads.

Common mistakes

  • Ordering by client timestamps, which disagree across devices.
  • Keeping connection state in a single gateway without a routing layer to find which gateway holds a user.
  • Presence updates on every heartbeat broadcast to every contact, which floods the system.

What to measure

  • Message delivery latency
  • Concurrent connections per gateway
  • Messages delivered vs. sent
  • Reconnect rate

Most-asked interview questions

Subjective questions 32

1WebSockets, long polling or Server-Sent Events: which would you choose for chat?Asked almost every time

WebSockets give a two-way, low-overhead persistent connection, which suits chat. SSE is one-way, server to client, which suits feeds and notifications. Long polling works everywhere but costs more. Most chat apps use WebSockets, with long polling as a fallback.

2How do you deliver a message when the recipient is connected to a different server?Asked almost every time

Each gateway subscribes to a channel per connected user or conversation (Redis Pub/Sub, Kafka) or registers in a connection map. The sender's gateway publishes the message after storing it, and the recipient's gateway pushes it down the socket. If nobody is connected, a push notification goes out.

3Walk through the architecture of a chat system like WhatsApp.Asked almost every time

Clients keep WebSocket connections to stateless gateway servers. A session service maps users to gateways; messages are written to a partitioned message store keyed by conversation, then routed through pub/sub to the recipient's gateway. Offline users get push notifications and fetch missed messages on reconnect using their last acknowledged sequence number.

4How do you design group chats?Asked almost every time

Store membership per group, write each message once to the group's partition, and fan out delivery to online members' gateways through pub/sub. Very large groups use fan-out on read (members fetch from the group timeline) instead of pushing to every member.

5How do you store messages at scale?Asked almost every time

Use a wide-column store like Cassandra or ScyllaDB partitioned by conversation ID and clustered by message ID or timestamp, so a conversation's recent messages are one sequential read. Keep hot recent data in cache and archive old messages to cheaper storage.

6How do delivery and read receipts work?Asked almost every time

Each message gets states: sent (server stored it), delivered (recipient device acknowledged), read (recipient opened it). Clients send acknowledgements with message IDs; the server updates state and notifies the sender, batching receipts for groups to limit traffic.

7How do you guarantee ordering and no lost messages?Very often asked

The server assigns a per-conversation sequence number when it stores the message, and acknowledges the client. Clients send a client message ID so retries are deduplicated, and on reconnect they ask for everything after the last sequence they have.

8How do you handle reconnects without losing or duplicating messages?Very often asked

Clients remember the last sequence number they received per conversation and request everything after it on reconnect. Messages carry client-generated IDs, so resends after a timeout are deduplicated on the server.

9How does end-to-end encryption change the design?Very often asked

Messages are encrypted on the sender's device with keys only recipients hold (Signal protocol), so the server stores and routes ciphertext it cannot read. Key distribution, multi-device sync and group key management become core concerns; server-side search and moderation are no longer possible.

10How do you scale WebSocket gateways?Very often asked

Keep gateways stateless apart from live connections, spread connections with a load balancer that supports long-lived connections, track user-to-gateway mapping in Redis, and use pub/sub so any service can reach any gateway. Plan for reconnect storms after deploys by draining gradually.

11How do typing indicators and presence avoid overloading the system?Very often asked

Treat them as ephemeral: send them only to members currently viewing the conversation, throttle them on the client, never store them durably, and use heartbeats with timeouts for presence rather than per-event writes.

12How do you send images and files in chat?Very often asked

Upload the file directly to object storage with a pre-signed URL (encrypted on the client for end-to-end encryption), send a message containing the file reference and thumbnail, and let recipients download through a CDN.

13How do push notifications fit into chat?Very often asked

When the recipient has no active connection, the server sends a push via APNs or FCM with minimal content (or an encrypted payload), which wakes the app to sync. Collapse many messages into one notification per conversation.

14How do you assign message IDs that sort correctly per conversation?Very often asked

Use a per-conversation sequence number assigned by the server that owns the conversation, or a time-ordered ID such as a Snowflake or ULID combined with the conversation ID. Clients store the highest sequence they have and ask for everything after it on reconnect.

15How would you design message reactions and replies?Very often asked

Store reactions as small records keyed by message and user so each user has one reaction per emoji, keep aggregated counts on the message for fast display, and send reaction events through the same delivery path. Replies reference the parent message ID and can be shown as a thread.

16How do you support multiple devices per user?Often asked

Track each device's connection and delivery state separately, deliver messages to all active devices, sync history to new devices, and for end-to-end encryption encrypt to every device's key.

17How do you implement message search?Often asked

Without end-to-end encryption, index messages in a search engine like Elasticsearch partitioned by user or conversation with access checks. With end-to-end encryption, search must happen on the device over the local message store.

18How do you moderate or prevent spam in chat?Often asked

Rate-limit message sending per user and new account, detect spam patterns (identical messages to many users), let users block and report, and use reputation scores; with end-to-end encryption rely on metadata and user reports.

19How do you handle message edits and deletes?Often asked

Send edit or delete events referencing the original message ID; clients update their view, and the server stores the change. Deletion for everyone has time limits and cannot guarantee removal from devices that already received it.

20What happens when a gateway server crashes?Often asked

Its clients disconnect and reconnect to other gateways via the load balancer, re-register in the session store, and request missed messages by sequence number. Messages already persisted are not lost.

21How do you estimate capacity for chat?Often asked

With 100 million daily users sending 50 messages each, that is 5 billion messages a day, about 58,000 per second on average and several times that at peak; each gateway might hold 50,000–100,000 connections, so plan thousands of gateway servers at peak.

22WebSockets vs. a protocol like MQTT for chat?Often asked

WebSockets are standard in browsers and simple with custom message formats. MQTT is lightweight with built-in QoS levels and suits constrained mobile networks; WhatsApp historically used a customised XMPP. Most new systems use WebSockets plus their own protocol.

23How do you keep message ordering per conversation?Often asked

Assign a monotonically increasing sequence number per conversation at the server (from the partition leader or a counter) and have clients sort by it, not by device clocks.

24How do you handle a user who is offline for weeks?Often asked

Keep undelivered messages durable in the message store, not in gateway memory, send a push notification summary, and when the user reconnects page through missed messages by sequence number rather than pushing everything at once.

25How do you implement disappearing messages?Often asked

Store an expiry time with the conversation setting or message, have every client delete messages after the timer, and delete server-side copies with a TTL. Make clear in the UI that recipients can still take screenshots.

26How would you add voice and video calls to a chat app?Often asked

Use the chat channel for signalling (offer, answer and ICE candidates), WebRTC for peer-to-peer media, STUN to discover addresses and TURN relays when direct connections fail, and an SFU for group calls so each client uploads one stream.

27How do you show an accurate 'last seen' without heavy load?Often asked

Update last-seen in a fast store only on connect, disconnect and periodic heartbeats, coarsen it to minutes, publish changes only to contacts who have the chat open, and respect privacy settings that hide it.

28How do you archive old conversations?Sometimes asked

Move messages older than a threshold to cheaper storage tiers or compressed blobs, keep an index of where they live, and load them on demand when users scroll back.

29What metrics matter for a chat system?Sometimes asked

Message delivery latency (send to delivered), delivery success rate, connection counts and churn, reconnect rate, push delivery rate, and storage growth.

30How do you test a chat system's reliability?Sometimes asked

Simulate network drops, reconnects and gateway crashes in load tests, check for lost or duplicated messages using sequence numbers, and run soak tests with millions of simulated connections.

31How do read receipts work with privacy settings?Sometimes asked

Respect users who turn read receipts off by not sending read events for them, and often not showing others' receipts to them either, applied consistently on server and clients.

32How do you export a user's chat history on request?Sometimes asked

Run an asynchronous export job that pages through the user's conversations, includes media links or files, packages the result as an encrypted archive in object storage, and sends a time-limited download link.

Objective questions 32

1Which protocol gives full-duplex communication over one TCP connection?Asked almost every time
  1. ASMTP
  2. BFTP
  3. CHTTP/1.0
  4. DWebSocketCorrect answer

Why: WebSocket upgrades an HTTP connection to a persistent two-way channel.

2What lets clients fetch exactly the messages they missed?Asked almost every time
  1. AThe last received sequence numberCorrect answer
  2. BDevice timestamps
  3. CMessage length
  4. DThe gateway IP

Why: Ask for everything after that number.

3Which store fits chat messages partitioned by conversation?Asked almost every time
  1. AA single SQL table without indexes
  2. BBrowser localStorage only
  3. CA CSV file
  4. DWide-column like CassandraCorrect answer

Why: Recent messages are one sequential read.

4What stops resent messages from appearing twice?Asked almost every time
  1. ADisabling retries
  2. BClient-generated message IDsCorrect answer
  3. CLonger timeouts
  4. DBigger gateways

Why: The server deduplicates by ID.

5How is presence usually tracked?Very often asked
  1. AA row per login in SQL
  2. BHeartbeats that refresh a key with a short TTLCorrect answer
  3. CPolling the user's device
  4. DDNS records

Why: If the heartbeat stops, the key expires and the user shows as offline.

6With end-to-end encryption the server stores…Very often asked
  1. ANothing at all
  2. BCiphertext it cannot readCorrect answer
  3. CPlain text
  4. DOnly metadata

Why: Only recipients hold the keys.

7How are very large groups delivered efficiently?Very often asked
  1. AOne message per member row
  2. BFan-out on read from the group timelineCorrect answer
  3. CPush to every member synchronously
  4. DEmail each member

Why: Avoids huge write amplification.

8Typing indicators should be…Very often asked
  1. ASent to all users
  2. BSent by email
  3. CEphemeral and never stored durablyCorrect answer
  4. DStored forever

Why: They are only useful in the moment.

9Which tracks which gateway a user is connected to?Very often asked
  1. ADNS
  2. BThe message store
  3. CA session store like RedisCorrect answer
  4. DThe CDN

Why: Routing needs the mapping.

10What happens to clients when a gateway crashes?Very often asked
  1. AThey reconnect elsewhere and resyncCorrect answer
  2. BMessages are lost forever
  3. CUsers must reinstall
  4. DNothing changes

Why: Persisted messages are safe.

11When should a push notification be sent?Very often asked
  1. ANever
  2. BOnly for read receipts
  3. CWhen the recipient has no active connectionCorrect answer
  4. DFor every message always

Why: It wakes the app to sync.

12Per-conversation sequence numbers let clients…Very often asked
  1. AAsk for everything after the last one they sawCorrect answer
  2. BSkip encryption
  3. CAvoid WebSockets
  4. DDelete old messages

Why: Missed messages are fetched precisely.

13Where should undelivered messages live for offline users?Very often asked
  1. AThe gateway's memory
  2. BThe sender's device only
  3. CThe CDN
  4. DA durable message storeCorrect answer

Why: Gateways restart; messages must survive.

14What ensures correct message order within a chat?Often asked
  1. AClient clock timestamps
  2. BA server-assigned sequence per conversationCorrect answer
  3. CMessage length
  4. DRandom IDs

Why: One authority assigns the order, so every device sees the same sequence.

15Where should message search run with end-to-end encryption?Often asked
  1. AIn the CDN
  2. BIn the push service
  3. COn the deviceCorrect answer
  4. DOn the server index

Why: The server cannot read messages.

16How are files shared in chat?Often asked
  1. ABy email
  2. BUpload to object storage and send a referenceCorrect answer
  3. CInline inside every message
  4. DThrough the WebSocket only

Why: Recipients download via a CDN.

17What should ordering within a conversation use?Often asked
  1. AAlphabetical order
  2. BServer-assigned sequence numbersCorrect answer
  3. CEach device's clock
  4. DMessage size

Why: Device clocks drift and disagree.

18Deleting a message for everyone…Often asked
  1. ADeletes the account
  2. BCannot guarantee removal from devices that received itCorrect answer
  3. CAlways erases every copy
  4. DIs impossible

Why: Devices may keep copies.

19How are multiple devices per user handled?Often asked
  1. AUsers choose one device forever
  2. BDeliver and track state per deviceCorrect answer
  3. COnly the newest device gets messages
  4. DDevices share one connection

Why: Each device has its own state.

20About how many messages per second is 5 billion a day?Often asked
  1. AAbout 5.8 million
  2. BAbout 58
  3. CAbout 58,000Correct answer
  4. DAbout 580

Why: 5 billion divided by 86,400.

21Which protocol has built-in QoS levels for mobile messaging?Often asked
  1. ASMTP
  2. BHTTP/1.0
  3. CMQTTCorrect answer
  4. DFTP

Why: Lightweight publish/subscribe.

22What should happen to presence when heartbeats stop?Often asked
  1. AThe user is marked offline after a timeoutCorrect answer
  2. BThey stay online forever
  3. CThe account is deleted
  4. DMessages are dropped

Why: Heartbeats with timeouts.

23Which protocol carries call media between users?Often asked
  1. AWebRTCCorrect answer
  2. BSMTP
  3. CFTP
  4. DDNS

Why: WebRTC handles real-time audio and video.

24When peers cannot connect directly, calls use…Often asked
  1. AA bigger WebSocket
  2. BAn email fallback
  3. CA message queue
  4. DA TURN relayCorrect answer

Why: TURN relays media through a server.

25Reactions are best stored as…Often asked
  1. AA new message each
  2. BNothing, they are only local
  3. COne record per message, user and emojiCorrect answer
  4. DText appended to the message

Why: That enforces one reaction per user and allows counts.

26Disappearing messages are removed from servers by…Often asked
  1. AAsking users to delete them
  2. BRestarting gateways
  3. CEncrypting them twice
  4. DA TTL on stored copiesCorrect answer

Why: Server copies must expire too, not just client views.

27Which metric best reflects chat user experience?Sometimes asked
  1. ANumber of tables
  2. BCode size
  3. CSend-to-delivered latencyCorrect answer
  4. DDisk usage

Why: Users feel delivery delay.

28How are reconnect storms after deploys reduced?Sometimes asked
  1. ARestarting all at once
  2. BDisabling reconnects
  3. CDoubling heartbeats
  4. DDraining gateways graduallyCorrect answer

Why: Spread the reconnections.

29Old conversations are best kept in…Sometimes asked
  1. AThe hot cache forever
  2. BThe push service
  3. CClient memory only
  4. DCheaper archive storage loaded on demandCorrect answer

Why: Saves cost for rarely read data.

30Spam in chat is limited by…Sometimes asked
  1. ABigger servers
  2. BTurning off encryption
  3. CRate limits, reputation and reportsCorrect answer
  4. DLonger messages

Why: Especially for new accounts.

31Read receipts for users who disabled them should…Sometimes asked
  1. ANot be sentCorrect answer
  2. BBe sent anyway
  3. CBe sent by email
  4. DBe delayed a day

Why: Respect privacy settings.

32Group video calls usually use an SFU so that…Sometimes asked
  1. ANo server is needed
  2. BCalls become audio only
  3. CEach client uploads one streamCorrect answer
  4. DMedia skips encryption

Why: The SFU forwards streams instead of every peer sending to every other.

Track your progress through every blueprint in your dashboard.

Start learning