Design a low-latency, real-time messaging system in Android.

Instruction: Outline the architecture and technologies needed to implement a low-latency, real-time messaging system within an Android application, considering aspects like data persistence, network efficiency, and user experience.

Context: This question challenges the candidate to demonstrate their knowledge of network programming, efficient data handling, and real-time communication strategies in Android.

Updated

Official answer

Use a durable message protocol plus a transport strategy. In the foreground, an authenticated TLS WebSocket can carry incremental events with low overhead, but neither the socket nor TCP guarantees instant delivery or a permanently live mobile connection.

Store outgoing messages locally with a stable client-generated operation ID, conversation ID and pending status. The server authorizes conversation membership, deduplicates retries and returns an authoritative message ID/order. Distinguish local queued, server accepted, delivered-to-recipient and read states; a successful socket send is not proof of the latter three. Use a per-conversation server sequence/cursor to fetch missed events and apply changes locally with duplicate handling.

When backgrounded, do not rely on an unrestricted permanent socket. Use FCM as a notification or sync hint consistent with Android policy, then reconcile from the authoritative service. Notifications may be delayed, collapsed or absent. An ETag validates a particular representation; it does not by itself define a complete multi-record change log. WorkManager can handle eligible deferred retry, with visible pending/error state.

Render the local database through a scoped ViewModel and lifecycle-aware observation. Bound reconnect backoff, queues and payload sizes; support pagination and account/logout isolation. Test offline send, crash after server acceptance, duplicate/out-of-order events and a missing notification.

If end-to-end encryption is required, use a reviewed protocol and implementation with device identities, key verification, multi-device membership, rotation and recovery defined. TLS alone is not E2EE, and naming a protocol does not implement it. Measure end-to-end latency distributions, eventual completeness and battery cost under stated network/background conditions.

Offline-first data, Background work choices, FCM delivery behavior, WebSocket protocol

Related Questions