skip to content

On first launch of a mobile mail client with 40,000 messages, what should it sync first, and how should it backfill the rest?

level: middleimportance: should knowfreq 38%

answer

  1. usable beats complete
  2. headers before bodies
  3. newest inbox page first
  4. capture the token before the long walk
  5. persist backfill progress

basics

~20 s

Fetch the folder list and the newest page of inbox headers first, capture a change token, and show the inbox. Then backfill older mail in resumable pages, fetching bodies on demand, while delta pulls from that token keep it current.

solid answer

~50 s

The goal is a usable inbox in seconds, not a complete mailbox. I fetch the folder list, then the newest few hundred inbox headers — sender, subject, snippet, flags, no bodies or attachments — and render them from the local store. I capture the change token at the start of the enumeration, so anything that changes during a backfill lasting many minutes arrives in the next delta pull rather than being lost. Backfill then walks older inbox pages and the other folders in the background, each page committed with a durable progress marker so a kill resumes instead of restarting. Bodies come when a message is opened, plus perhaps the most recent ones for offline reading; attachments only on demand. The screen says what is still loading. Syncing only a bounded window per folder, with older mail fetched on request, is also a legitimate scope decision.

go deeper

for a junior

Recall that the first screen needs only the newest headers; bodies, attachments and old mail can all arrive later without blocking the user.

for a middle

Explain the order — folders, newest inbox headers, token, then resumable backfill — and why the token is captured before the long walk.

for a senior

Show resumability and race handling: committed progress markers, one writer for deltas and backfill, and honest partial-state labels on search and counts.

for a principal

Decide the sync scope across the product: a bounded window per folder versus a full mirror trades offline completeness against storage, battery and first-launch time.

## What "usable" means on first launch The first sync of a mail client is the most expensive one it will ever run, and it happens at the worst moment: the user has just signed in and is waiting. A mailbox of 40,000 messages across several folders cannot arrive in seconds, so the design question is **what to fetch first so the inbox works**, and how the rest follows without blocking anyone. A worked size, using this scenario's own assumptions rather than any standard: if a message **header summary** (sender, subject, a short snippet, date, flags, folder) is about 1 KB, all 40,000 come to roughly 40 MB. If an average **body** is about 50 KB, the bodies alone are about 2 GB. Headers are what the list needs; bodies are what one opened message needs. That ratio drives the whole plan. ## The order of work 1. **Account and folder list.** Small, and needed to draw navigation. 2. **Newest page of inbox headers** — a few hundred, newest first. Commit them and render from the local store. The inbox is now usable. 3. **Capture the change token** for later incremental pulls (see below). 4. **Backfill**: older inbox pages, then the other folders, oldest last, in the background. 5. **Bodies on demand**, when a message is opened, plus optionally the most recent ones for offline reading. **Attachments** only when asked for. The screen reads from the local store throughout, so each committed page simply appears; nothing waits for "sync complete". ## Capturing the token before the long walk Backfill can take minutes, or hours on a slow network with the app going in and out of the background. Messages will be flagged, moved and deleted on other devices meanwhile. If the client took its change token only after the walk finished, a change to a page it had already stored would happen *before* that token and never be reported. So the token is captured at the **start** of the enumeration — or, if the listing itself returns one, taken from its first response. Every change made during the walk then arrives in the next delta pull. Some of it overlaps what backfill already stored; because applying changes is **idempotent** (upsert by id, delete if present), the overlap is harmless. One caution: if deltas and backfill pages write concurrently, a backfill page fetched earlier can overwrite a newer version a delta just applied. Run both through one serial writer, or compare a per-record server revision before overwriting. ## Resumable backfill The operating system may end the app's process whenever it is in the background, and users kill apps. A backfill that restarts from zero each time may never finish. Make it resumable: - Commit each backfilled page together with a **progress marker** (the cursor or position of the next page) in one transaction. - On relaunch, read the marker and continue from it. - Keep a per-folder state such as `not_started`, `backfilling`, `complete`, so the client can tell "not fetched yet" from "does not exist". ## How much to keep | Scope | Offline completeness | Cost | Fits when | |---|---|---|---| | Full mirror of every header and body | complete | largest storage, longest first sync | small mailboxes, strict offline needs | | All headers, recent bodies | full list, recent mail readable offline | moderate | most mail clients | | Bounded window per folder (say, the last few months), older on demand | recent mail only | smallest | large mailboxes, constrained devices | The window is a product decision, not a bug: older mail can be fetched from the server when the user scrolls or searches past the window, with the screen saying so. ## What the user sees meanwhile - The inbox as soon as the first page commits, with a "loading older mail" row at the bottom rather than a full-screen spinner. - Search and counts labelled as covering synced mail only until backfill completes, so nobody concludes an old message is gone. - New writes — flags, moves, drafts — accepted immediately and queued as usual; first launch is not a read-only mode. ## Summary - Usable first, complete later: folders, newest inbox headers, then backfill. - The token is captured before the long walk, and the overlap is absorbed by idempotent apply. - Backfill is resumable through committed progress markers. - How much of the mailbox lives on the phone is a scope decision with real trade-offs.

  • Why capture the change token before backfilling rather than after?
    Backfill can run for minutes or hours. A token taken at the end describes a state after changes the walk already passed, so a message flagged or deleted mid-walk in a page already stored would never be reported. Taken at the start, every change during the walk arrives in the next delta pull, and idempotent apply makes the overlap harmless.
  • What should the inbox show while older mail is still backfilling?
    Everything already stored, read from the local database, with an honest marker at the bottom such as "loading older mail" rather than a spinner over the whole screen. Search results and counts should say they cover synced mail only until backfill completes, so a user does not conclude an old message is gone.

  • Download every message body before showing the inbox.
  • Take the sync token only after the backfill finishes.
  • If the app is killed mid-backfill, restart the whole download.
  • Every installed mail client must mirror the full mailbox to work offline.
  • Show an empty inbox with a spinner until sync is complete.