/Learn
Mobile systems at scale · Part 1 of 7

Background sync for an app the OS will kill

Sync huge data sets and files on Android, iOS and React Native while the OS can stop your process at any moment. Includes a simulator, an interview-ready analogy, and a stack proven at millions of users.

01
AndroidiOSReact NativeBackend

Mobile engineeringReact Native in one place13 of 22Next: The 95/5 Rule and Performance Budgets in CI →

Priya stands in a basement with no signal. This post explains why that case shapes the whole design: phones lose networks, and the OS kills your process. Start from the basics in Day 1 →

Study with AI

If you only read this

  1. You can't keep an app alive in the background. Make every step safe to kill and resume.
  2. The UI reads only the local database. A write goes to the DB and an outbox in one transaction.
  3. A sync engine pushes the outbox with idempotent op_ids and pulls changes by a server sequence cursor.
  4. Files travel separately: hashed, split into parts, uploaded straight to object storage, checkpointed per part.
  5. The OS runs the work: WorkManager on Android; BGTaskScheduler and background URLSession on iOS. React Native keeps this native.
Prefer reels?Read this whole topic as 10 short courses, one paragraph at a time.Read as reels →

01The basement upload

A field engineer finishes an inspection in a basement with one bar of signal. She taps Submit on 40 photos, watches a spinner, and swipes the app away to take a call.

In most apps those photos are now in limbo. In a well-built one, nothing is lost. The upload pauses. The OS wakes the app when signal returns. Only the parts that never reached the server are sent again, and she never has to think about it.

This post is about building the second app, and about explaining how you built it to a hiring panel.

02The rule the OS enforces

Both platforms protect battery and memory by stopping apps that are not on screen. Here is what your code can do in each state.

StateAndroidiOSYour code can
ForegroundRunsRunsDo anything
Just backgroundedRuns briefly, then cachedA few seconds via beginBackgroundTaskFinish the current write
SuspendedCached process, no guaranteeFrozen, no code runsNothing
KilledMemory pressure, Force Stop, OEM battery killersMemory pressure, user swipeNothing. Only disk survives
Woken by OSWorkManager job, FCM messageBGTask, URLSession event, silent pushRun a short, bounded job
If it isn't on disk, it doesn't exist.

Every design decision below follows from that line. Pending edits live in a table. Upload progress lives in a table. The sync position lives in a table. The process is disposable.

03Explain it like a restaurant

When an interviewer says "explain it to me simply", use a busy restaurant. Every part of the system has a counterpart that everyone already understands.

The waiter writes the order on a pad before walking to the kitchen.→Local write + outbox row, one transaction
Every ticket has a unique number. The kitchen ignores a ticket it already cooked.→op_id idempotency on the server
The kitchen screen numbers every dish it sends out, in order.→Server sequence number per partition
A waiter back from break asks "what happened since dish 1041?"→Pull changes by cursor
The "86" board lists dishes taken off the menu.→Tombstones for deletes
The manager says "we're full, come back in ten minutes, not all at once".→429 + Retry-After + jitter
A catering order goes out tray by tray, each tray signed for.→Multipart upload, checkpoint per part
A waiter's shift ends mid-order. The next waiter picks up the same pad.→Process killed, job resumes from disk
Say it out loud"Think of a restaurant. The waiter writes every order on a numbered ticket before going to the kitchen, so nothing is lost if he's interrupted. The kitchen ignores duplicate tickets and numbers every dish it sends out. Anyone coming back from a break just asks what happened since the last number they saw. My sync engine works the same way: local outbox, idempotent op IDs, and a server sequence cursor."

04The architecture on one screen

The local database is the hub. The UI only reads it. Workers that the OS schedules move its changes to and from the backend, and file bytes skip the API entirely.

ON THE DEVICE UICompose · SwiftUI · React ViewModel / storeone-way data flow Repositoryrow + outbox, one txn observewrite Local SQLite: the single source of truthentities · outbox · sync cursor · file-transfer parts Transfer managerhash · parts · resume Sync enginepush outbox · pull cursor OS schedulerWorkManager · BGTask bytes, presigned URLsHTTPS push / pull push hint IN THE CLOUD S3 + CDNmultipart · Range reads Sync API (stateless)dedupe op_id · pages FCM / APNs notifiercoalesced hints Postgres + change logper-partition sequence numbers · CDC to workers
Five boxes do the work: local DB, repository, sync engine, transfer manager and the OS scheduler.

05Kill it yourself

This is a working model of the design. Add notes, attach a video, cut the network and kill the app. Watch what survives in SQLite and what the server receives.

9:415G
Site notes
Process killedRAM is gone. SQLite, the outbox and saved upload parts are still on disk.
SQLite · outbox0
SQLite · sync state
cursor1041
parts savednone
processalive
MB re-sent0
Server · change logseq 1041
Newest log line on top. Everything here runs in your browser.Try: attach the video, kill the app at 50%, then wake it.

Five things to try

  1. Turn the network off and add three notes. They show instantly and wait in the outbox.
  2. Attach the video, kill the app halfway, then press OS wakes app. Only missing parts upload.
  3. Press Replay last request. The server spots the repeated op_id and writes nothing new.
  4. Kill the app, then Edit on another device. Nothing arrives until the OS wakes the app.
  5. Watch cursor. It only moves after a page of changes is safely applied.

06Sync in four moves

1

Write locally

Row + outbox entry in one transaction. UI updates now.

2

Push

Send outbox ops with op_id. Server dedupes and orders them.

3

Pull

Ask for changes since the cursor, one page at a time.

4

Commit

Apply the page and save the new cursor in the same transaction.

Push first, so the server sees your edits before it answers what changed. Here is the whole contract.

five-things-try.httpHTTP
1// push: idempotent by op_id2POST /v1/sync/push3{ "ops": [{ "op_id": "018f3c7a-9b", "entity": "note", "id": "n-77",4            "type": "update", "base_version": 12, "fields": { "title": "Pump room B" } }] }5→ { "results": [{ "op_id": "018f3c7a-9b", "status": "applied", "version": 13, "seq": 1042 }] }6 7// pull: ordered by server sequence, never by timestamps8GET /v1/sync/pull?cursor=c_1041&limit=5009→ { "changes": [ { "seq": 1042, "entity": "note", "id": "n-77", "version": 13, "fields": { … } },10                 { "seq": 1043, "entity": "note", "id": "n-12", "deleted": true } ],11    "next_cursor": "c_1043", "has_more": false }
11 linesUTF-8HTTP
Why not updated_at?Device and server clocks drift, and two rows written in the same millisecond can both be skipped. A server sequence gives one total order and a safe place to resume.

07Pick a conflict strategy

There is no single right answer. Choose per entity by asking what a lost edit would cost. Pick an example.

08Moving huge files

Records and bytes are different problems, so they get different pipelines. A file upload is a small state machine, and every state is a value in a table.

Captured→Hashed→Already on server?→Uploading parts⇄Paused→Completing→Done
  • Copy first. Copy the file into app storage. A gallery URI can disappear.
  • Hash it. SHA-256 lets the server say "I already have this" and verify the result.
  • Split it. S3 multipart parts of 8–16 MB (minimum 5 MB, except the last).
  • Go direct. Presigned part URLs send bytes straight to storage, never through your API.
  • Checkpoint. Save each part's ETag the moment it lands. A kill costs one part at most.

What a checkpoint saves you

Restart from zero
Resume from saved parts

09Platform playbook

Same design, three runtimes. The tools differ, and so do the traps.

NeedUse
Sync soon after an editWorkManager one-time, expedited, unique name
Periodic catch-upPeriodicWorkRequest (15 min minimum), network constraint
Huge transfer the user startedUser-initiated data transfer job (API 34+), or a typed foreground service
Server has new dataFCM data message → enqueue sync work
what-checkpoint-saves.ktKotlin
1class UploadWorker(ctx: Context, p: WorkerParameters) : CoroutineWorker(ctx, p) {2  override suspend fun doWork(): Result {3    val t = repo.get(inputData.getString("fileId")!!) ?: return Result.success()4    for (part in t.missingParts()) {          // resume from the DB5      if (isStopped) return Result.retry()6      repo.markPartDone(t.id, part.n, repo.put(part))  // checkpoint each part7    }8    repo.complete(t.id); return Result.success()9  }10}
10 linesUTF-8Kotlin
  • Android 14+ requires a declared foreground-service type, and Android 15 caps dataSync services at about 6 hours a day.
  • Doze and App Standby Buckets slow down rarely used apps. OEMs like Xiaomi and Oppo kill harder, so test on them.
  • Force Stop cancels all scheduled work until the user opens the app again.

10Scaling the backend

The backend scales because no request needs server memory of a client. The client carries its cursor, every write is idempotent, and bytes bypass the API. Add each stage when a signal says you need it.

1 · MVPunder 10k users
  • one Postgres
  • monolith sync API
  • presigned S3
2 · Growth10k–500k users
  • read replicas
  • Redis for op_id dedupe
  • push hints + CDN
3 · Scale500k–5M users
  • partitioned change log
  • CDC to Kafka
  • CDN snapshots for first install
4 · Large5M+ users
  • sharded DB
  • multi-region cells
  • data residency
gate: reads saturate the primary
gate: first sync too slow
gate: one region hurts everyone
Back-of-envelope1M daily users × 10 syncs a day ≈ 115 requests per second on average; plan for 5–10× at peak. 1M users × 20 MB a day = 20 TB of uploads a day, all of it straight to object storage.

11Myths that cost offers

Tap a card to see what's actually true.

12Interview kit

I design for the app being killed. The UI reads only a local SQLite database, every write lands in an outbox in the same transaction, and a sync engine pushes it with idempotent op IDs and pulls changes by a server sequence cursor. Files go separately as checkpointed multipart uploads straight to S3. The OS runs it all: WorkManager on Android, BGTaskScheduler and background URLSession on iOS.

Worked examples to reach for

When a panel says "give me a concrete example", pick the one closest to their product.

Field inspection app

200k technicians · 2 GB photos each per week

That's about 400 TB of uploads a week, so bytes must go direct to S3 with dedupe by hash and lifecycle rules to cheaper storage. Sync only each technician's assigned jobs. Prefetch job packs on Wi-Fi and charging. Compress photos on device before upload.

The follow-up they'll ask"What if a tech is offline for three weeks?" Deletes are tombstones with a 90-day TTL. Past that, the server returns 410 and the app rebuilds from a CDN snapshot, then pulls deltas.

Clinic booking app

appointment slots · patient notes · offline front desk

Bookings are server-authoritative: the app proposes a slot offline and shows "pending confirmation". Notes use 409-and-merge so no clinician's edit is lost. Patient data is encrypted at rest with keys in Keystore or Keychain.

The follow-up they'll ask"Two receptionists book the same slot offline." The server accepts the first and returns 409 for the second. The app shows the conflict and offers the next free slot.

Retail point of sale

stores keep selling during an outage

Sales write to a local ledger. Inventory changes are operations ("−2 units"), not new totals, so two tills never overwrite each other. Every sale carries an op_id, so replaying after reconnect can't double-charge or double-count.

The follow-up they'll ask"Can you take cards offline?" Only as store-and-forward with a per-transaction limit, which is a business risk decision. Say who signs off on that risk.

Rapid-fire answers

How do you keep the app running in the background?
You don't. Both OSes will stop it. Persist intent on disk and let WorkManager or BGTaskScheduler run short, resumable jobs.
What survives a kill?
Only disk: SQLite rows, the outbox, the cursor and saved upload parts. Anything in memory is gone.
Push or poll?
Both. A coalesced push hint triggers a pull. A periodic job and app-foreground sync are the fallback, because push is best effort.
How do deletes reach a device that was offline?
Tombstones in the change log with a TTL. If the device's cursor is older than the TTL, the server returns 410 and the device rebuilds from a snapshot.
How do you stop a retry storm after an outage?
Full-jitter exponential backoff on clients, 429 with Retry-After on the server, rate limits per device, and push fan-out spread over minutes.
How do you know sync is healthy?
p95 freshness (time since last successful sync), outbox depth, upload completion rate, conflict rate and background-task expirations, split by app version and device model.
What would you build first with three months?
Outbox, cursor pull, last-write-wins, presigned multipart uploads, OS scheduling and basic telemetry. CRDTs, snapshots and real-time channels wait until the metrics ask for them.

Check yourself

13Glossary and further reading

Outbox
A local table of pending changes, written in the same transaction as the data.
op_id
A client-generated ID per change. The server uses it to ignore duplicates.
Cursor
The last server sequence number the device has applied. It asks for changes after it.
Tombstone
A record saying "this was deleted", kept long enough for offline devices to learn it.
Presigned URL
A short-lived URL that lets the device upload one object straight to storage.
Multipart upload
A large file sent as numbered parts, then assembled by the storage service.
UIDT job
Android's user-initiated data transfer job, for long transfers the user started.
Background URLSession
iOS transfers run by a system daemon, outside your app's process.

The whole story in 15 slides

Swipe through the article's ideas in order. Notes flag where a slide differs from the article.

Title slide: resilient offline sync, with a server rack, local data and a warning about network drops and process termination
1 / 15
Full size

The constraint: unreliable connectivity and clocks, on Android, iOS and React Native, for millions of users.

  1. The constraint: unreliable connectivity and clocks, on Android, iOS and React Native, for millions of users.
  2. The OS will kill your app. If it isn't on disk, it doesn't exist.
  3. The waiter model: persist the intent, let the server number the changes, resume from a cursor.
  4. The five boxes. The UI reads only the local database, and the OS decides when the engine wakes.
  5. Move 1: the data row and the outbox row commit in one transaction.
  6. Moves 2 and 3: push first with idempotent ids, then pull one page after the cursor.
  7. Never sync by time. Two rows can share a millisecond, and clocks drift.
  8. Move 4: apply the page, clear the outbox and advance the cursor, all in one transaction.
  9. Choose a conflict strategy per kind of data, and treat the user's choice as a new edit.
  10. Hand timing to the OS: WorkManager on Android, BGTaskScheduler and URLSession on iOS.
  11. In React Native the engine stays native. JavaScript only observes the database.
  12. Large files go around the API, in chunks, with progress saved per chunk.
  13. A stateless server scales because the client carries its own cursor and operation ids.
  14. Deletes are tombstones. A device behind the limit gets 410 Gone and rebuilds from a snapshot.
  15. The blueprint in four rules.

Tested code, explained

The save, and the conflict resolution

This is the code behind the tap. It's the only way a screen changes data, and it has no network call in it.

LocalDb.ktKotlin
1/** The only way a screen changes data. Nothing here touches the network. */2class JobRepository(3  private val db: LocalDb,4  private val newOpId: () -> String,5  private val by: String = "device",6  private val requestSync: (Trigger) -> Unit = {},7) {8  fun setAnswer(jobId: String, field: String, value: String) {9    db.transaction {10      val job = jobs.getValue(jobId)11      jobs = jobs + (jobId to job.copy(12        answers = job.answers + (field to value),13        syncState = SyncState.PENDING,14      ))15      outbox = outbox + Op(newOpId(), jobId, field, value, job.version, by)16    }17    requestSync(Trigger.EDIT)            // only after the commit: the edit is already safe18  }19 20  /** The user chose a value in the conflict screen. It is a new edit, based on what they just saw. */21  fun resolve(jobId: String, value: String) {22    db.transaction {23      val job = jobs.getValue(jobId)24      val theirs = checkNotNull(job.theirs) { "$jobId has no conflict" }25      jobs = jobs + (jobId to job.copy(26        answers = job.answers + (theirs.field to value),27        syncState = SyncState.PENDING,28        theirs = null,29      ))30      outbox = outbox + Op(newOpId(), jobId, theirs.field, value, theirs.version, by)31    }32    requestSync(Trigger.EDIT)33  }34}
34 linesUTF-8Kotlin
db.transaction { ... }
The job row and the outbox row commit together. A crash can't leave one without the other.
SyncState.PENDING
Makes the row show its clock. The screen reacts by itself, because it watches this table.
job.version
Copied into the op as its base version: 4 in Priya's case.
requestSync(Trigger.EDIT)
Runs after the commit, so even if scheduling fails, the edit is already safe.
fun resolve
Choosing a value in the conflict screen creates a new edit, based on the version the user just saw. Nothing gets patched in place.

Compiled and tested by npm run test:mobile.

Every trigger becomes one job

Edits, pushes, timers and reconnects all want a sync. Without this funnel, a busy minute would start five of them.

SyncTriggers.ktKotlin
1/** The part of WorkManager this code needs. Returns false when a job with this name already waits. */2fun interface JobScheduler {3  fun enqueueUnique(name: String, requiresNetwork: Boolean): Boolean4}5 6/** Every reason to sync comes through here, and they all become the same one job. */7class TriggerHub(private val scheduler: JobScheduler) {8  var lastTrigger: Trigger? = null9    private set10 11  fun fire(trigger: Trigger): Boolean {12    lastTrigger = trigger13    return scheduler.enqueueUnique("sync", requiresNetwork = true)14  }15}
15 linesUTF-8Kotlin
fun interface JobScheduler
The one WorkManager feature this code needs. Hiding it behind an interface lets a plain test run this code.
enqueueUnique("sync", ...)
On Android this is enqueueUniqueWork with KEEP: if a job named sync already waits, nothing new is added.
requiresNetwork = true
The condition the OS waits for. Your code doesn't watch the network at all.

Compiled and tested by npm run test:mobile.

The worker, and its time budget

The OS gives you a few minutes. This code spends them, and leaves things tidy if it runs out.

SyncTriggers.ktKotlin
1sealed interface WorkResult {2  /** Done. The worker enqueues a follow-up job when [morePending] is true. */3  data class Success(val morePending: Boolean) : WorkResult4  data object Retry : WorkResult5}6 7/** What the Android worker does, with no Android types, so a plain test can run it. */8class SyncWork(9  private val engine: SyncEngine,10  private val hub: TriggerHub,11  private val clockMs: () -> Long,12  private val budgetMs: Long = 8 * 60_000,13) {14  suspend fun doWork(): WorkResult {15    val deadline = clockMs() + budgetMs16    return when (val out = engine.run(hasTime = { clockMs() < deadline })) {17      is RunOutcome.Finished -> {18        if (out.morePending) hub.fire(Trigger.PERIODIC)19        WorkResult.Success(out.morePending)20      }21      is RunOutcome.RetryLater -> WorkResult.Retry       // the OS applies its own backoff22      RunOutcome.AlreadyRunning -> WorkResult.Success(morePending = false)23      is RunOutcome.RebuildNeeded -> WorkResult.Success(morePending = false)24    }25  }26}
26 linesUTF-8Kotlin
deadline = clockMs() + budgetMs
Eight minutes, a little under what Android allows, so the code stops itself before the OS stops it.
hasTime = { ... }
The engine checks this between batches, so it always stops at a safe point.
if (out.morePending) hub.fire(...)
Leftover work gets a follow-up job, so nothing depends on one run finishing.
RetryLater -> WorkResult.Retry
Tells the OS to apply its own backoff. The ops are still in the outbox.

Compiled and tested by npm run test:mobile.

AndroidiOSReact NativeBackend
Written by

Rizvan

Builds mobile and AI systems at scale and writes the series he wished existed before his own principal loop: one rule, one analogy, something to break, and the interview kit.

All 7 parts
AI Engineer · written by Rizvan · part 1 of 7