The system implements a Hybrid Architecture that combines the reliability of a central server with the resilience of offline-first clients.
- Server-Authoritative: The server is the single source of truth for the complete dataset and coordinates real-time interactions.
- Client-Offline-Capable: Clients maintain a local replica of their "working set", allowing full read/write functionality without a network connection.
graph TD
subgraph "Server Cloud (Source of Truth)"
Coordinator[Server Coordinator]
DB[(Primary Database)]
Partitions[Partitioned In-Memory Cache]
PubSub[Event Broadcaster]
end
subgraph "Client A (Online)"
AppA[Application]
CacheA[LWW-Map Replica]
SyncA[Sync Engine]
StoreA[(IndexedDB)]
end
subgraph "Client B (Offline)"
AppB[Application]
CacheB[LWW-Map Replica]
SyncB[Sync Engine]
StoreB[(IndexedDB)]
end
Coordinator <-->|WebSocket / Realtime| SyncA
SyncA <--> CacheA
CacheA <--> StoreA
Coordinator -.->|Reconnection| SyncB
SyncB <--> CacheB
CacheB <--> StoreB
Coordinator <--> Partitions
Partitions <--> DB
Partitions <--> PubSub
The server acts as the coordination hub and the durable persistence layer.
- Role: Manages client connections, authentication, and protocol handling.
- Functionality:
- Accepts WebSocket connections.
- Validates HLC timestamps.
- Routes operations to the Partition Engine.
- Role: Horizontally scalable in-memory storage.
- Functionality:
- Distributes data across
271partitions (server-side only). - Manages locks and transaction consistency for connected clients.
- Flushes data to the underlying SQL/NoSQL database (Write-Behind/Write-Through).
- Distributes data across
- Role: Real-time push notifications.
- Functionality:
- Subscribes to partition changes.
- Filters events based on client subscriptions (Queries/Predicates).
- Pushes "Delta" updates to online clients immediately.
The client is designed to be lightweight but autonomous.
- Role: The primary API surface for the application.
- Functionality:
- Online: Behaves like a local cache that syncs in the background.
- Offline: Behaves like a local database.
- Uses Hybrid Logical Clock (HLC) to tag all local mutations.
- Role: Records every mutation (
put,remove) performed locally. - Structure: Append-only log stored in IndexedDB.
- Usage:
- Replayed to server upon reconnection.
- Used to rollback/rebase if conflict resolution dictates.
- Role: Abstraction over persistence.
- Implementations:
- Browser:
IndexedDB(viaidblibrary). - Node.js:
SQLite(viabetter-sqlite3) orLevelDB. - React Native:
AsyncStorageorsqlite.
- Browser:
- Role: Manages the data lifecycle.
- States:
IDLE: Monitoring changes.SYNCING_UP: Uploading local OpLog to server.SYNCING_DOWN: Downloading missed changes (Merkle Tree diff).REALTIME: Listening for WebSocket events.
- App calls
map.put(key, val). - Client generates HLC Timestamp.
- Client writes to Local LWW-Map and OpLog (IndexedDB).
- UI updates immediately.
- Sync Engine sends operation to Server via WebSocket.
- Server applies operation (checks permissions, persists).
- Server broadcasts change to other clients.
- Server sends
ACKto originating client -> OpLog entry marked "synced" (and eventually pruned).
- App calls
map.put(key, val). - Client generates HLC Timestamp.
- Client writes to Local LWW-Map and OpLog.
- UI updates immediately.
- Sync Engine detects "Offline" -> Pauses upload.
- Connection established.
- Push Phase: Client uploads unsynced OpLog entries.
- Pull Phase:
- Client sends Merkle Tree Root Hash of its dataset.
- Server compares with its tree.
- Server sends back only the differing keys (Delta).
- Conflict Resolution:
- Client merges incoming Deltas using LWW (Last-Write-Wins) based on HLC.
- If Server Timestamp > Local Timestamp, local value is updated.
- UI refreshes to reflect the converged state.