In a mobile mail client, a push nudge, a reconnect and an app foreground arrive within seconds; how should sync triggers be designed so they cause one pull?
answer
- a nudge is a hint, not data
- one pull in flight per scope
- a rerun flag instead of a queue
- send the outbox before the pull
- the periodic pull is the safety net
basics
~20 sRoute every trigger to one sync coordinator per account that allows one pull in flight. A trigger during a pull only sets a rerun flag, so a burst costs at most one more pull; a push nudge is a hint, not data.
solid answer
~50 sTriggers are reasons to sync, not syncs. A push nudge says only "something changed", a reconnect says the network is back, foreground says the user is looking, and a periodic pull covers whatever the others missed. All of them call one coordinator per account, or per token scope. If nothing is running, it starts a cycle — send the outbox, then pull from the stored token — usually after a short debounce. If a cycle is running, the trigger only sets a **rerun flag**; when the cycle ends, it runs once more if the flag is set. Three triggers inside one cycle therefore become one follow-up pull, which often returns a cheap empty delta. I never treat the nudge's payload as the data, because push delivery is best-effort and nudges can be delayed or collapsed, and I never let two pulls share a token.
code
pseudocode · 18 linesstate running = false
state rerun = false
function on_trigger(reason):
if running:
rerun = true
return
running = true
wait_debounce()
repeat:
rerun = false
outbox.send_pending()
result = pull_changes(account)
if result == failed:
schedule_retry_with_backoff()
rerun = false
until not rerun
running = falsego deeper
Recall the trigger sources — push nudge, reconnect, foreground, periodic, pull-to-refresh — and that each one only asks for a sync; none of them is the sync.
Explain single-flight with a rerun flag and why a nudge's payload is a hint rather than data to store.
Show the cycle's order (outbox, then pull), debouncing, one coordinator per token scope, and why the periodic pull stays even when push works.
Balance freshness against battery and server load across the product: which data earns nudges, how often periodic pulls run, and how stale each screen may be.
## The triggers and what each one tells you A mobile mail client has several reasons to ask the server what changed. Each says something different, and none of them is the sync itself. | Trigger | What it tells the client | What it does not guarantee | |---|---|---| | Push nudge ("mailbox changed") | the server saw a change, roughly now | that every change produces one, or that it arrives promptly | | Reconnect | queued writes can be sent, and changes may have been missed | that anything changed | | App foreground | the user is about to look | that the stored data is stale | | Periodic pull | time has passed | that the operating system runs it on schedule | | Pull-to-refresh | the user explicitly wants fresh data | anything about the server | A design that turns each of these into its own network pull does wasteful and sometimes harmful work: unlocking the phone on a train can fire reconnect, foreground and a delayed nudge within two seconds. ## Why three triggers must not mean three pulls - **Cost.** Each pull wakes the radio and spends data and battery; the second and third return nothing new. - **Races.** Two pulls sent with the same stored token receive overlapping changes. If the slower response commits last, it can write older message versions and an older token over the newer result. - **Wasted server work.** A busy client multiplied across a large user base becomes measurable load. ## The coordinator One **sync coordinator** per account — or per token scope, if each folder has its own token — owns the decision. All triggers call it and nothing else pulls. 1. A trigger arrives. If no cycle is running, wait a short debounce (a second or two, this design's own choice) so a burst lands together, then start a cycle. 2. If a cycle is already running, set a **rerun flag** and return. Do not start a second pull, do not cancel the running one, and do not ignore the trigger: the running pull may have been sent before the change the trigger signals. 3. A cycle sends the outbox, then pulls changes from the stored token, page by page. 4. When the cycle ends, if the rerun flag is set, clear it and run one more cycle. Any number of triggers during a cycle collapse into at most one follow-up. Because each pull starts from the stored token, a follow-up that finds nothing costs one small request and an empty delta. ## Order inside a cycle: outbox, then pull Sending queued writes first means the pull returns a server state that already includes them, so the user's flags and moves come back confirmed instead of being contradicted by a state computed before they arrived. It does not remove the need to protect pending writes during a merge — some sends will fail or wait — but it shrinks the window. ## The nudge is a hint A nudge may name a folder, which is a fine hint for which scope to pull first. It is not the data. Push delivery is best-effort: nudges can be delayed, collapsed into one, or never arrive, and a payload written straight into the store bypasses the token and can roll newer data back. The token-based pull decides exactly what changed, and stays correct whether nudges are lost, duplicated or late. How push is delivered, and how much background time a nudge or a periodic task buys, belong to other parts of the design; this question only decides what a trigger causes. ## Why the periodic pull stays Even with push nudges, keep a periodic or on-foreground pull. It bounds how stale the mailbox can get when a nudge is lost, and it keeps tokens refreshed inside the server's retention window for a phone that rarely gets nudges. When nothing changed, it is cheap. ## Summary - Triggers request a sync; one coordinator per scope performs it. - A trigger during a cycle sets a rerun flag; a burst costs at most one extra pull. - Each cycle sends the outbox, then pulls from the stored token. - Nudges are hints, never data; a periodic pull remains the safety net.
- Why send the outbox before pulling rather than after?Sending first means the pull returns a server state that already includes the user's writes, so flags and moves come back confirmed rather than contradicted by a state computed before they landed. Some sends will still fail or wait, so the merge must protect pending writes anyway; the order only shrinks that window.
- What does a periodic pull add if push nudges work?A safety net. Nudges can be delayed, collapsed or lost, and a device can be offline when one is sent. A periodic or on-foreground pull from the stored token bounds how stale the mailbox gets and keeps the token inside the server's retention window; when nothing changed, it costs one small request with an empty delta.
- If a nudge names a folder, should the client pull only that folder?Pulling that scope first is a cheap win, but the nudge stays a hint: the folder's stored token decides what comes back, and other folders are still refreshed by their own triggers and the periodic pull. Data inside the nudge is never applied as truth.
- Each trigger should start its own pull immediately.
- The push payload can be written as the new data, skipping the pull.
- Cancel the running pull and start over when a new trigger arrives.
- Push nudges make periodic and foreground pulls unnecessary.
- Two pulls sent with the same token are harmless duplicates.