Design: Real-Time Chat Interview Questions

ADVANCED › System Design

Design a chat app like WhatsApp. What are the main components on the client?

What a strong answer covers: UI (conversation list and chat screen), ViewModels, a repository with Room as source of truth for conversations and messages, a real-time connection (WebSocket) while the app is open, FCM push for when it's closed, an outbox for sending, a sync engine for catching up on missed messages, media upload and download, and local notifications.

How do you send messages reliably, including offline and with retries?

What a strong answer covers: Write the message to Room immediately with a client-generated id and status 'sending', so it appears instantly. An outbox processor sends pending messages in order over the socket or HTTP, with the client id as an idempotency key so retries don't duplicate. On acknowledgement, update status and the server id and timestamp; on failure, retry with backoff through WorkManager, and show failed messages with a retry option.

How do you keep messages in the correct order?

What a strong answer covers: Use server-assigned sequence numbers per conversation as the source of order, not device clocks, which can be wrong. Show local unsent messages at the end until acknowledged, then place them by server order. Detect gaps in sequence numbers and fetch the missing range. Sort by sequence in the database query so the UI order is stable.

How do you receive messages in real time and when the app is in the background or killed?

What a strong answer covers: While in the foreground, keep a WebSocket with heartbeats and reconnect with backoff on network changes. In the background, the server sends an FCM high-priority message, which the app uses to fetch new messages and show a notification (or the push carries an encrypted payload). On reconnect, sync from the last known sequence per conversation so nothing is missed.

How would you implement delivery and read receipts, typing indicators and multi-device sync, and what would you not persist?

What a strong answer covers: Receipts are events sent back to the server (delivered when stored on the device, read when shown on screen), aggregated per conversation up to a sequence number rather than per message, and stored as status. Typing indicators are ephemeral socket events with a timeout and aren't persisted. For several devices, each device syncs from a server cursor, and read state syncs as an 'up to sequence N' marker. End-to-end encryption complicates multi-device and search.

Back to Design: Real-Time Chat