|
| 1 | +# Architecture Overview |
| 2 | + |
| 3 | +The **SQLite AI offline-sync solution** consists of three main components: |
| 4 | +* **sqlite-sync**: Native client-side SQLite extension |
| 5 | +* **cloud-sync**: Synchronization microservice |
| 6 | +* **postgres-sync**: Native PostgreSQL extension |
| 7 | + |
| 8 | +Together, these components provide a complete, production-grade **offline-first synchronization stack** for SQLite and PostgreSQL. |
| 9 | + |
| 10 | +# sqlite-sync |
| 11 | + |
| 12 | +**sqlite-sync** is a native SQLite extension that must be installed and loaded on all client devices. |
| 13 | +We provide prebuilt binaries for: |
| 14 | +* Desktop and mobile platforms |
| 15 | +* WebAssembly (WASM) |
| 16 | +* Popular frameworks including React, Expo, npm, and more |
| 17 | + |
| 18 | +**Note:** The latest version (v0.9.96) is not yet available in the official sqlite-sync repository.
Please use our development fork instead:~[https://github.com/sqliteai/sqlite-sync-dev](https://github.com/sqliteai/sqlite-sync-dev)~ |
| 19 | + |
| 20 | +### Architecture Refactoring |
| 21 | +The extension has been refactored to support both **SQLite** and **PostgreSQL** backends. |
| 22 | +* All database-specific native calls have been isolated in database.h |
| 23 | +* Each database engine implements its own engine-dependent layer |
| 24 | +* The core **CRDT logic** is fully shared across engines |
| 25 | + |
| 26 | +This modular design improves **portability**, **maintainability**, and **cross-database consistency**. |
| 27 | +### Testing & Reliability |
| 28 | +* Shared CRDT and SQLite components include extensive unit tests |
| 29 | +* Code coverage exceeds **90%** |
| 30 | +* PostgreSQL-specific code has its own dedicated test suite |
| 31 | + |
| 32 | +Key Features |
| 33 | +* Deep integration with SQLite — the default database for Edge applications |
| 34 | +* Built-in network layer exposed as ordinary SQLite functions |
| 35 | +* Cross-platform, language-agnostic payload format |
| 36 | +* Works seamlessly in any framework or programming language |
| 37 | + |
| 38 | +Unlike other offline-sync solutions, **sqlite-sync embeds networking directly inside SQLite**, eliminating external sync SDKs. |
| 39 | + |
| 40 | +### Supported CRDTs |
| 41 | +Currently implemented CRDT algorithms: |
| 42 | +* **Last-Write-Wins (LWW)** |
| 43 | +* **Grow-Only Set (G-Set)** |
| 44 | + |
| 45 | +Additional CRDTs can be implemented if needed, though LWW covers most real-world use cases. |
| 46 | + |
| 47 | + |
| 48 | + |
| 49 | +# cloud-sync |
| 50 | + |
| 51 | +**cloudsync** is a lightweight, stateless microservice responsible for synchronizing clients with central servers. |
| 52 | +### Responsibilities |
| 53 | +* Synchronizes clients with: |
| 54 | + * **SQLiteCloud servers** |
| 55 | + * **PostgreSQL servers** |
| 56 | +* Manages upload and download of CRDT payloads |
| 57 | +* Stores payloads via **AWS S3** |
| 58 | +* Collects operational metrics (connected devices, sync volume, traffic, etc.) |
| 59 | +* Exposes a complete **REST API** |
| 60 | + |
| 61 | +⠀ |
| 62 | + |
| 63 | +Technology Stack |
| 64 | + |
| 65 | +* Written in **Go** |
| 66 | +* Built on the high-performance **Gin Web Framework** |
| 67 | +* Fully **multitenant** |
| 68 | +* Connects to multiple DBMS backends |
| 69 | +* Stateless architecture enables horizontal scaling simply by adding nodes |
| 70 | +* Serialized job queue ensures **no job loss**, even after restarts |
| 71 | + |
| 72 | +⠀ |
| 73 | + |
| 74 | +Observability |
| 75 | + |
| 76 | +* Metrics dashboard available in grafana-dashboard.json |
| 77 | + |
| 78 | +* Additional logs available via the Fly.io monitoring dashboard |
| 79 | + |
| 80 | + |
| 81 | + |
| 82 | +Demo Deployment |
| 83 | +For the current demo, a single cloudsync node is deployed in **Europe** on Fly.io. |
| 84 | +If testing from other regions, latency will reflect this single-node deployment.
A production deployment would use **geographically distributed nodes with regional routing** for global coverage. |
| 85 | + |
| 86 | + |
| 87 | + |
| 88 | +# postgres-sync |
| 89 | + |
| 90 | +**postgres-sync** is a native PostgreSQL extension derived from sqlite-sync. |
| 91 | +### Features |
| 92 | +* Implements the same CRDT algorithms available in sqlite-sync |
| 93 | +* Applies CRDT logic to: |
| 94 | + * Changes coming from synchronized clients |
| 95 | + * Changes made directly in PostgreSQL (CLI, Drizzle, dashboards, etc.) |
| 96 | + |
| 97 | +This ensures **full bidirectional consistency**, regardless of where changes originate. |
| 98 | + |
| 99 | +### Schema Handling |
| 100 | +SQLite does not support schemas, while PostgreSQL does.
To bridge this difference, postgres-sync introduces a mechanism to: |
| 101 | +* Associate each synchronized table with a specific PostgreSQL schema |
| 102 | +* Allow different schemas per table |
| 103 | +This preserves PostgreSQL-native organization while maintaining SQLite compatibility. |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | +# Current Limitations |
| 108 | + |
| 109 | +The PostgreSQL integration is actively evolving. Current limitations include: |
| 110 | +* **User Impersonation**
: The microservice currently applies server changes using the Supabase Admin user.
In the next version, changes will be applied under the identity associated with the client’s JWT. |
| 111 | +* **Table Creation**
: Tables must currently be created manually in PostgreSQL before synchronization.
We are implementing automatic translation of SQLite CREATE TABLE statements to PostgreSQL syntax. |
| 112 | +* **Row-Level Security**: RLS is fully implemented for SQLiteCloud servers.
PostgreSQL RLS integration is in progress and will be included in the final release. |
| 113 | +* **Beta Status**
: While extensively tested, the PostgreSQL sync stack should currently be considered **beta software**. Please report any issues, we are committed to resolving them quickly. |
0 commit comments