Triamorph Systems

Mobile & Native · 12 min read · Sep 10, 2024

Offline-First SQLite with PowerSync & ElectricSQL: Instant Reactive Mobile Sync

Deep technical guide to building local-first apps with SQLite, PowerSync, and ElectricSQL: zero-latency reads, streaming replication from PostgreSQL, and conflict resolution.

By Hammad Haider

Core Engineering Takeaways

  • In a local-first application, all reads and writes execute against local on-device SQLite, delivering instantaneous 0ms UI responsiveness.
  • PowerSync and ElectricSQL stream partial PostgreSQL replication changes directly to client SQLite databases based on tenant permissions.
  • Last-Write-Wins (LWW) and Conflict-Free Replicated Data Types (CRDTs) resolve conflicting offline edits automatically without corrupting state.

Users expect mobile apps to work instantly on the subway, in rural job sites, and during airplane flights. Traditional web architecture requires waiting for an API call before updating the screen. Local-first architecture stores data in an embedded on-device SQLite database, rendering reads in 0ms and streaming mutations to PostgreSQL in the background via replication streams.

1. The Local-First Software Manifesto: Speed & Data Ownership

Why waiting for network spinwheels is a broken user experience

Local-first software treats the device database as the primary source of truth, treating the cloud as a synchronization and backup relay. Apps open instantly without loading spinners.

2. Setting Up PowerSync with React Native & PostgreSQL

Sync rules and real-time reactive SQL queries in React

PowerSync syncs PostgreSQL tables directly into client SQLite databases using declarative sync rules defined in YAML.

src/db/queries.ts (typescript)
import { useQuery } from '@powersync/react';

export function ProjectTasksList({ projectId }: { projectId: string }) {
  // Executes synchronously against on-device SQLite
  // Automatically re-renders when background sync delivers remote changes
  const { data: tasks, isLoading } = useQuery(
    'SELECT * FROM tasks WHERE project_id = ? ORDER BY priority DESC',
    [projectId]
  );

  return (
    <div>
      {tasks.map((task) => (
        <TaskCard key={task.id} task={task} />
      ))}
    </div>
  );
}

3. Resolving Concurrent Offline Sync Conflicts

Deterministic merging with Hybrid Logical Clocks (HLC)

Hybrid Logical Clocks assign monotonically increasing timestamps that combine physical wall-clock time with logical sequence counters, ensuring fair conflict resolution.

4. Mobile Storage Quotas & Partial Sync Buckets

Syncing only relevant user data to preserve mobile disk space

Sync rules ensure mobile devices sync only active projects belonging to the logged-in user rather than the entire corporate database.

Tags: powersync sqlite postgres syncelectricsql local first syncreactive mobile database architectureconflict free offline syncOffline First

Related Engineering Practice & Case Studies

Engineering Practice Blueprint

Mobile App Development (iOS & Android) →

Cross-platform mobile applications with offline-first synchronization and native device bridges.

View Practice Blueprint & Capabilities →
Production Client System

Task Sync Kro AI Productivity App →

Contextual Android productivity application prioritizing tasks by time and geofenced physical location.

Read Deployed Case Study (4.8-star user rating with zero sync drops) →

Related Engineering Articles & Guides

Explore all technical guides in our Articles & Guides Index → or discuss your architecture with our engineers via Technical Discovery →