DEV Community

Cover image for Building Offline-First Mobile Sync: Lessons from the Trenches
Omar Anajar
Omar Anajar

Posted on

Building Offline-First Mobile Sync: Lessons from the Trenches

One of the most challenging features I've built was a mobile synchronization
engine that needed to work reliably even when users had no internet connection.

The Challenge

Field sales teams needed to access customer data, place orders, and record
visits while traveling through areas with poor connectivity. When they
reconnected, everything needed to sync seamlessly without data loss.

The Constraints

  • 20+ different data types (customers, products, orders, prices, inventory)
  • 1,000+ concurrent mobile users
  • Complex relationships between entities
  • Must handle conflicts when same data is edited offline by multiple users
  • Limited mobile storage and bandwidth

The Architecture

1. Polymorphic Data Handler
I built a flexible system that could handle any entity type without hardcoding:

class SyncEngine {
    public function sync($entityType, $data) {
        $handler = $this->getHandler($entityType);
        return $handler->process($data);
    }
}
Enter fullscreen mode Exit fullscreen mode

2. Role-Based Data Filtering
Different users need different data. A vendor doesn't need shipper information:

class VendorFormatter {
    public function format($data) {
        return [
            'customers' => $this->filterByTerritory($data->customers),
            'products' => $this->filterByCategory($data->products),
            // Only send what this user needs
        ];
    }
}
Enter fullscreen mode Exit fullscreen mode

3. Conflict Resolution
Last-write-wins with server timestamp as source of truth:

if ($serverTimestamp > $localTimestamp) {
    // Server version is newer, update local
    $this->updateLocal($serverData);
} else {
    // Local version is newer, push to server
    $this->pushToServer($localData);
}
Enter fullscreen mode Exit fullscreen mode

What I Learned

Technical Lessons:

  • Start with simple conflict resolution, add complexity only if needed
  • Optimize payload size—mobile bandwidth is precious
  • Database transactions are critical for data consistency
  • Comprehensive logging saves hours of debugging

Process Lessons:

  • Test with real users in real conditions (not just office WiFi)
  • Edge cases will surface in production—plan for them
  • Clear error messages help users understand what's happening
  • Incremental rollout catches issues before they scale

The Outcome

The system now processes thousands of sync operations daily with a 99.9%
success rate. Field teams can work confidently offline, knowing their data
will sync when connection returns.

Building offline-first is hard, but the user experience payoff is worth it.

Top comments (0)