high complexity extracted Offline & Sync Confidence: 100%
7
Components
39
Shared
0
User Stories
Yes
Analyzed

Description

Background Sync is the network transport and retry layer that drains the mutation outbox created by Offline Data Support. It monitors connectivity changes and, when a connection is available, dispatches queued mutations to the REST API in order, applying exponential backoff with jitter for failed requests. The sync queue service runs both in the foreground (triggered immediately when the app regains connectivity) and in the background (using platform background execution APIs so sync can complete even when the app is not in the foreground). The service wraps the existing ApiHttpClient and reuses its JWT auto-refresh and typed ApiException hierarchy. Sync progress and errors are surfaced to the UI through a Riverpod provider so users can see pending item counts and last-sync timestamps. A retry backoff service tracks per-item failure counts and moves items to a dead-letter state after a configurable maximum number of retries, notifying the user so they can resolve the conflict manually or discard the mutation.

Analysis

Business Value

The mutation outbox created by offline-first persistence is only valuable if mutations reliably reach the server. Background Sync closes that loop: it transforms the local store from a private cache into a durable, eventually consistent replica of the server state. Without it, peer mentors would need to manually trigger uploads, reintroducing the friction the offline layer was designed to eliminate - and creating a serious risk that activity records are never submitted to the server and therefore never included in Bufdir reports. Exponential backoff with jitter is important for multi-organization deployments: if many devices simultaneously regain connectivity (e.g., after a conference or training event), naive retry logic would generate a thundering herd that degrades API performance for all users. The backoff strategy protects server stability and ensures fair resource sharing across organizations. Dead-letter handling with user notification ensures no mutation is silently lost, which is critical for organizations whose Bufdir funding depends on complete and accurate activity records.

Implementation Notes

Flutter's connectivity_plus package is used to detect network state changes; a stream subscription in the sync queue service triggers an immediate drain attempt when the device transitions from offline to online. Background execution is implemented via WorkManager (Android) and BGTaskScheduler (iOS) through the workmanager Flutter plugin, scheduling a periodic sync task with a minimum interval of 15 minutes when the app is backgrounded. The sync loop reads the outbox in FIFO order, dispatches each mutation via ApiHttpClient, and commits the result (success or failure) back to the outbox within a Drift transaction to guarantee atomicity. The retry backoff service implements truncated exponential backoff: delay = min(base * 2^attempt + jitter, max_delay), with base=30s, max_delay=3600s, and jitter drawn from a uniform random range of ±20% of the computed delay. After a configurable threshold (default: 10 retries), the item is marked dead-letter and a local notification is dispatched via the push notification service to alert the user. Sync state (pending count, last sync timestamp, dead-letter count) is exposed as a Riverpod AsyncNotifier and consumed by the home screen status bar widget and the settings screen sync diagnostics panel.

Components (46)

User Interface (2)

Service Layer (3)

Data Layer (1)

Infrastructure (1)

Shared Components

These components are reused across multiple features

User Stories

No user stories have been generated for this feature yet.