KatInterview

Resilient Streaming Chat Client for Android with MVVM and UDF

System DesignModel answer
MediumModel answerSystem Designandroidmvvmkotlin-coroutines

Last updated

Design a layered Android client for an LLM chat app covering streaming, cancellation, retries, offline history, immutable UI state, and lifecycle and process-death handling.

Problem

You are designing the client side of a conversational AI app for Android. Users can browse earlier conversations, type a prompt, watch the model's answer appear incrementally as it streams in, and search through old sessions.

Propose an architecture that stays correct on flaky networks, survives screen rotation and backgrounding, and remains easy for a large team to extend and test.

Requirements

Functional

  • Streaming chat and history: Prompts are sent to a backend that streams the reply (Server-Sent Events or WebSocket). Past conversations and messages are loaded through a paginated REST API.
  • Cancel and retry: The user can stop a reply while it is being generated. Failed calls should be retried with exponential backoff, or manually by the user.
  • Offline reading: Any conversation the user has already opened must remain readable without a connection.

Non-functional

  • Unidirectional data flow: State flows down, events flow up, with clear ownership at each layer.
  • Lifecycle safety: No leaks, and no duplicate network calls caused by rotation or backgrounding.
  • Testability: Business logic must not depend on Android framework classes.

What your answer should address

  1. Layers: Responsibilities of the presentation layer (Compose and ViewModel), the domain layer (use cases), and the data layer (repository and data sources).
  2. UI state: An immutable model for loading, in-progress streamed text, errors, and completed output, designed so contradictory states cannot be expressed.
  3. Concurrency: How you use coroutines and Flow (StateFlow, SharedFlow, dispatchers, structured cancellation) for background work.
  4. Data management: How the repository coordinates the network (Retrofit/OkHttp) with local storage (Room).
  5. Edge cases: Configuration changes, process death (SavedStateHandle), and connections that drop mid-stream.

State any assumptions about scale and scope up front, and call out the trade-offs behind your main decisions.

Hints

Hint 1

Make the local database the single source of truth: network results are written to it, and the UI only observes it. Consider which problems this removes (rotation, offline, pagination).

Hint 2

Split UI state into independent fields (input text, history load status) plus a sealed type for the mutually exclusive generation phase (idle, submitting, streaming, failed). Ask which combinations should be unrepresentable.

Hint 3

Think about what happens to a stream when the user cancels, when the connection drops, and when the process is killed. What identifiers or sequence numbers would let you resume or deduplicate safely?

Hint 4

Emitting UI state or writing to disk on every token is expensive. Consider batching or conflating updates and checkpointing periodically.