FSD Frontend System Design

Part IV — The Reach · Lesson 9.5

Background Sync & Storage Limits

In one lineThe browser decides when your queue runs and how long your data lives — design for both.

Where you've seen itA chat PWA in a tunnel. On Chrome the message goes out after you've closed the tab; on Safari it waits until you open the app again.

  • Time14 min
  • DiagramWhat triggers a flush, per browser + storage you can lose
  • Chapter9 · Offline Support

Diagram

diagram

Lessons in this diagram9.3 Offline Writes & the Sync Queue

diagram
STORAGE YOU CAN LOSE — the browser may delete it without asking

  best-effort (default)   evicted under disk pressure: least-recently
                          used sites first, a whole site's data at once
  persisted               storage.persist() granted → kept until the
                          user clears it
  Safari, not installed   script-written storage cleared after 7 days
                          of Safari use with no interaction on the site

  navigator.storage.estimate()  →  { usage, quota }     measure it

How it works

  1. Register a sync after every queued write where SyncManager exists; the service worker's sync handler calls the same flush() the page uses.
  2. Always wire the fallbacks as well: flush on online, when the tab becomes visible, and on app start — Background Sync is Chromium-only.
  3. Finish inside event.waitUntil(). If the promise rejects, the browser retries later on its own schedule and stops after a few attempts (event.lastChance).
  4. Ask for persist() at a meaningful moment — after the first offline save, not on first load.
  5. Unsent changes are the only data you can't re-fetch — flush early and show "2 notes not synced" rather than hoping.
js
// page: queue the write, then ask the browser to flush when it can
await outbox.add(op);
const reg = await navigator.serviceWorker.ready;
if ('sync' in reg) await reg.sync.register('flush-outbox');
else flush();                         // Safari, Firefox: try now
addEventListener('online', flush);    // …and on every reconnect

// sw.js: the browser wakes this when the device is back online
self.addEventListener('sync', (event) => {
  if (event.tag === 'flush-outbox') event.waitUntil(flush());
});

Trade-offs

MechanismLimit
Background SyncChromium only; no guarantee of when it runs
Periodic Background SyncChromium, installed PWAs only; the browser picks the interval
online and visibility eventsOnly while a page is open
storage.persist()Can be refused; granted by heuristics, or a prompt in Firefox

Installing helps on iOS: a web app added to the Home Screen is not subject to Safari's 7-day storage cap — another reason to make install easy (9.2).

Interview angle

"Your offline notes app loses users' unsent notes on iPhone. Why, and what do you do?"

  • Safari has no Background Sync, so the queue only flushes while the app is open — and if the site isn't installed, its storage can be cleared after 7 days of Safari use without the user interacting with it.
  • Flush on open, on online and on visibility change; encourage Add to Home Screen, and call persist() after the first offline save.
  • Show unsent items clearly so nothing disappears silently, and keep the outbox small by collapsing edits (9.3).

Recap

  • Background Sync is a Chromium bonus; online, visibility and app start are the floor.
  • Local data is best-effort until persisted — and Safari may clear it after 7 days.
  • Unsent changes are irreplaceable: flush early, show them, never drop them.