Queue and synchronization
Persistence before upload, acknowledgment by ID, deduplication, and recovery from route transport failures.
The delivery rule
A location leaves the device queue only when the server acknowledges its eventId. The normal sequence is capture → persist → send → acknowledge → remove.
This acknowledgment is known as an ACK. HTTP 2xx alone is not enough: the upload response must contain IDs from the submitted batch and a valid remote configuration.
The mechanism supports retries. It aims to preserve events through network failures, but does not promise that an HTTP request will run exactly once.
What is persisted
| Item | Android | iOS |
|---|---|---|
| Pending positions | SQLite geo-tracking.db | SQLite outbox.sqlite |
| Local order | Auto-incrementing sequence | Auto-incrementing sequence |
| Local uniqueness | Unique event_id | Unique event_id |
| Latest position and counters | metadata table | Transactional JSON in the state table |
| Remote configuration | Persisted metadata | Persisted state |
| Local authorization | Private preferences | Persisted state |
| Native tracking token | Private preferences | Keychain |
Both databases use WAL and synchronous=FULL. Inserting a location and updating its associated state happen in the same transaction.
The record stores the complete event, including its source, timestamp, and configuration version. A retry reads that record; it does not create another UUID or change capturedAt to the retry time.
Batches and ordering
The next upload contains up to 100 positions in local database insertion order. This matches the maximum accepted by the upload API.
Only one synchronization can be in progress per native service. While a request awaits its response, new events can enter the queue and will be processed afterward.
Upon receiving a valid ACK, the service removes acknowledged IDs, updates uploadedCount, and checks for pending records. If any remain, it starts the next batch.
Local upload order does not determine how history is displayed. The server queries positions by capturedAt and eventId, preserving the temporal meaning of samples received late.
Synchronization contract
| Situation | Request | Expected result |
|---|---|---|
| Empty queue | GET /api/devices/:deviceId/config | Remote configuration |
| Records in the queue | POST /api/devices/:deviceId/locations | acknowledgedEventIds and config |
| Authentication | Authorization: Bearer … header | Credential authorized for the device |
| Normal transport | HTTPS | Local HTTP only in development builds |
The token is not sent in the URL, and redirects are disabled in the native transports. The queue's identity is the endpoint and device pair; changing it cannot redirect existing pending records.
Android requires a stopped session, an empty queue, and no upload in progress to change identity. iOS requires an empty queue and resets session state when identity changes.
Partial acknowledgment and invalid responses
The client determines which response IDs belong to the submitted batch. An ID from another batch cannot delete an arbitrary pending position.
If the server acknowledges only part of a batch, only that part leaves SQLite. The remaining records stay available for another upload.
If no ID from the batch is acknowledged, the operation is treated as a failure and the queue remains intact. Invalid JSON, invalid configuration, HTTP errors, or persistence failures also prevent normal acknowledgment.
Local counters track the removal of acknowledged events. uploadedCount is not a live query of the server's global total; records sent by other sources or installations do not automatically contribute to this counter.
How the server prevents duplication
The Worker routes the device to its Durable Object with SQLite. The batch is written in a transaction; the server returns acknowledged IDs only after the persistence step.
For each event, the server checks eventId:
- New ID: inserts the record and
receivedAt. - Existing ID with identical content: acknowledges the retry without duplicating the row.
- Existing ID with different content: returns
409 event_conflictand rolls back the batch.
This handles lost ACKs: the server may have saved a position before the connection dropped. The device sends it again with the same ID; the server acknowledges the existing row; the client then removes it from the queue.
WebSocket notifications include only newly inserted records. An identical retry does not need to produce a second position on the dashboard.
Retries and connectivity recovery
| Behavior | Android | iOS |
|---|---|---|
| Initial delay after failure | 2 seconds | 2 seconds plus 0–1 second of jitter |
| Progression | Exponential | Exponential |
| Current effective maximum delay | 256 seconds | 256 seconds plus 0–1 second of jitter |
| Network recovery | Connectivity callback | NWPathMonitor |
| Periodic check | Every 15 seconds with a session or queued records | 2-second timer while executing |
| Empty-queue query after success | Applicable periodic check | Next configuration query after at least 10 seconds |
The calculation includes a 300-second ceiling, but the failure counter saturates at 8, so the exponential value reaches 256 seconds. These timings are process policies, not guarantees that the system will wake the app.
Manual synchronization can bypass the retry delay, but does not create a second simultaneous request. Captures and connectivity events can also provide opportunities to synchronize.
Storage limits
The client does not discard positions at a 10,000-record threshold. The SQLite queue grows according to available storage. Native tests include scenarios with 10,005 records; this demonstrates an exercised scenario, not a guaranteed storage reservation on every device.
If a new sample cannot be persisted, collection pauses or stops with an explicit error. The code preserves previous rows and does not silently select older events for deletion.
On the server, the current limit is 100,000 events per device. A batch that would exceed capacity receives 507 device_capacity and is rolled back. Retries of existing IDs continue to be acknowledged when they do not require new rows.
There is no automatic implementation of time-based retention, archiving, or export followed by cleanup. Reaching capacity requires an operational decision; retrying the request does not free space.
Diagnosing state in the interface
| Indicator | Interpretation |
|---|---|
Increasing pendingCount | Captures are being persisted, but the queue has not yet been acknowledged |
Increasing uploadedCount | The client removed acknowledged positions |
Recent lastSyncAt | Synchronization succeeded; it may have only fetched configuration |
lastError | Storage, collection, operation, or transport failure, depending on the platform |
lastLocation | Latest locally persisted position, including synthetic positions |
A zero count does not prove that GPS produced positions: a newly configured session can also have an empty queue. To validate collection, compare the source, timestamps, counts, and dashboard history.
Transport errors are reduced to operational messages; the module's public state does not contain the token. Shared logs and evidence should preserve this separation too.
Two different queues in the project
The queue described on this page is the HTTP route queue. The polygon demo keeps events such as polygon_enter and polygon_proximity in native preferences, with pending, delivering, failed, delivered, and superseded states.
In that demo, simulate delivery only changes local state. It is not an acknowledgment from the Worker and does not use the SQLite/ACK mechanism described above. See Polygons.
Repository sources
geo-demo/modules/geo-monitoring/android/src/main/java/expo/modules/geomonitoring/TrackingStore.ktgeo-demo/modules/geo-monitoring/android/src/main/java/expo/modules/geomonitoring/TrackingEngine.ktgeo-demo/modules/geo-monitoring/android/src/main/java/expo/modules/geomonitoring/TrackingTransport.ktgeo-demo/modules/geo-monitoring/ios/GeoTrackingStore.swiftgeo-demo/modules/geo-monitoring/ios/GeoTrackingService.swifttracking-server/src/index.tsdocs/tracking-contract.md