Designing a chat app

Mobile system design Senior Kotlin 2.0+ API 24+ Reviewed By Best Latest Apps

Pick the fix

Messages appear out of order when two people's phones have different clocks. Fix?

Options
Hint 1 of 1

Who should decide the order?

Choose an answer, then open Show answer below to check it.

Show answer

Answer: A Order by a server-assigned sequence number

Correct. One authority decides the order.

Why not the others?
  • B Sync device clocks with NTP. Still unreliable.
  • C Sort by message length. Not related.
  • D Order by when the app received them. Breaks when syncing history.

30-second answer

Keep a persistent connection, such as a WebSocket, while the app is in the foreground, and rely on FCM high-priority messages when it isn't. Store conversations and messages in Room and render from there. Send messages with a client ID and 'sending' state, retry through WorkManager, and mark them sent on server ack. Order by server sequence numbers, and sync missed messages on reconnect.

Practise saying it · 45 s. No recording, just a timer

Design a chat client.

Say your answer out loud, then check which of these points you covered:

  • Socket in foreground, FCM in background
  • Room as source of truth
  • Client IDs and acks
  • Server sequence ordering
  • Sync on reconnect

Deep dive

Don't keep a socket open in the background; Android will restrict it and it drains battery.

Pagination loads older messages from Room and the server.

Read receipts and typing indicators are lightweight events, not stored messages.

End-to-end encryption changes where search and backup can happen.

Common mistakes

  • A background service holding a socket forever.
  • Ordering by device clock.
  • Losing messages typed while offline.

Senior take & follow-ups

Split foreground (socket) from background (FCM), then the send pipeline and ordering.

Follow-up questions

Think of your answer first, then open the hint.

  1. How do you order messages?

    Show hint

    By server-assigned sequence numbers, not device time.

  2. What happens when the app is killed?

    Show hint

    FCM wakes it; it syncs from the last sequence number.

Compare on iOS: A chat timeline that survives retries The same design; background delivery uses APNs instead of FCM.

Suggest a fix if something here is wrong or out of date.