Skip to content
tezvyn:

How would you implement a robust offline-first repository?

Source: developer.android.comHardHow cards are made

How would you implement a robust offline-first repository?

Tests single-source-of-truth discipline and network-local sync. Outline a Room repository with Flow, background refresh, disk persistence, immediate local emission, and conflict resolution with retry.

What's really being asked

The interviewer wants to see if you understand that in an offline-first architecture the local database is the single source of truth, not the network. They are looking for separation of concerns between data producers, the repository boundary, and observable data streams. Specifically they care about how you handle reads versus writes, how you propagate network updates without janking the UI, and how you recover from errors when connectivity returns.

The full answer

First, the repository should expose a Kotlin Flow or similar observable type that queries Room directly so the UI re-emits automatically on every local change. Second, network fetching should be triggered explicitly by a refresh mechanism or by a stale-cache check, not on every read, to avoid redundant API calls. Third, the network response should be written into Room within a transaction so that downstream observers receive a single coherent update rather than partial state. Fourth, you need a conflict resolution strategy such as server-wins, client-wins, or last-write-wins based on timestamps or version fields, especially if the user can mutate data offline. Fifth, enqueue retry logic with exponential backoff for failed syncs, and expose sync status or errors through a separate channel or state flow so the UI can show banners instead of silent failures.

The mistakes people make

A major red flag is proposing that the ViewModel calls the API directly and then pushes the result into Room as an afterthought; this inverts the architecture and makes the UI depend on network timing. Another mistake is exposing suspend functions that return network payloads directly without a local persistence step, which breaks offline support entirely. Candidates also stumble by ignoring write conflicts, assuming the server state always overrides local changes without considering pending mutations queued while offline.

What usually comes next

The interviewer may ask how you would handle pagination with the Paging library while keeping Room as the source of truth, or how to structure a sync queue for mutations made offline. They might also probe how you would test this repository, including faking the DAO and API service to verify emission ordering, or how to handle schema migrations when your conflict metadata changes.

A concrete example

Imagine a task manager app. The repository exposes Flow of task list from a Room DAO. When the user opens the screen, the ViewModel collects this flow and shows cached tasks instantly. A refresh trigger launches a network request; on success the repository maps DTOs to entities and upserts them in a transaction. If the user edits a task while offline, the repository writes the change to Room with a pendingSync flag and schedules a WorkManager task to retry the upload with exponential backoff starting at ten seconds. When the sync succeeds the pending flag is cleared and the UI updates automatically via the same Flow.

Interview question

A user edits a record while offline; when connectivity returns, the server holds a newer version. What should the repository do to resolve this conflict?

  • a.Discard the pending local mutation and fetch the latest server state to guarantee consistency.
  • b.Immediately overwrite the local database with the server response so the UI always shows the latest remote state.
  • c.Emit both versions to the UI through separate Flows and let the ViewModel decide which data to display.
  • d.Apply a predefined conflict resolution strategy using timestamps or version fields before persisting the final state to Room.Correct
Why?

The card emphasizes that an offline-first repository must use a conflict resolution strategy such as server-wins or last-write-wins based on timestamps or version fields when the user mutates data offline. Simply overwriting local state with the server response is a common mistake because it ignores pending mutations queued while offline.

Just read this? Test yourself on what you have been reading.

Read the original → developer.android.com

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on android — each one lists the topics its interview covers.

See open roles