Here’s a complete solution addressing all three focus areas - offline-first sync, deduplication, and timestamp validation:
Offline-First Sync Implementation:
Implement a persistent local queue using SQLite with a sync status table. Generate client-side UUIDs immediately when operators save data:
String txnId = UUID.randomUUID().toString();
productionEntry.setTransactionId(txnId);
productionEntry.setClientCreated(System.currentTimeMillis());
localDB.insertPendingSync(productionEntry);
Use WorkManager for reliable background sync that respects network constraints and survives app restarts. Configure it to retry with exponential backoff.
Record Deduplication:
On the server side, add a unique constraint on transaction_id in your shop_floor_data table. Your sync endpoint should use INSERT IGNORE or upsert logic:
INSERT INTO shop_floor_data (transaction_id, ...)
VALUES (?, ...) ON CONFLICT (transaction_id) DO NOTHING;
This prevents duplicate records even if the client retries a sync that partially succeeded. The client should only remove entries from the local queue after receiving a 200 OK response.
Timestamp Validation:
Implement three-timestamp tracking:
- client_created: When operator entered data
- client_queued: When added to sync queue
- server_received: Server timestamp on sync
Server-side validation pseudocode:
// Pseudocode - Timestamp validation steps:
1. Calculate time_delta = server_received - client_created
2. If time_delta > 24 hours, flag for review (possible clock skew)
3. If client_created > server_received, reject (future timestamp)
4. Use server_received for production reporting to ensure consistency
5. Store all three timestamps for audit trail
Add clock synchronization checks when the app starts. If device clock differs from server by more than 5 minutes, show a warning to operators.
Additional Recommendations:
- Implement a sync status indicator in the UI showing pending count
- Add manual sync button for operators to trigger immediate sync
- Log all sync attempts with outcomes for troubleshooting
- Consider batch sync for efficiency (sync up to 50 records per request)
- Implement conflict resolution if same work order is edited offline by multiple operators
This approach has resolved sync issues for multiple shop floor deployments. The key is treating mobile as the source of truth initially (offline-first), then reconciling with server state using UUIDs and proper timestamp validation.