docs/sync-and-op-log/background-info/operation-log-sync-best-practices.md
This document outlines industry best practices for designing and implementing a robust, offline-first synchronization server based on an operation log (event sourcing) architecture.
Instead of mutating the current state directly, the system records every change as an immutable event (operation).
In a client-server model, the server acts as the linearization point.
Seq N, and the second gets Seq N+1.For end-to-end encrypted or complex domain applications, the server should ideally treat operation payloads as opaque blobs (or just minimal validation).
git rebase).| Feature | SQLite | PostgreSQL |
|---|---|---|
| Use Case | Personal, Self-Hosted, Embedded | Multi-tenant, Enterprise, High Concurrency |
| Concurrency | Single-writer (WAL mode helps) | MVCC (Multi-Version Concurrency Control) |
| Maintenance | Zero (single file) | Moderate (requires service management) |
operations table should be effectively append-only.DELETE rows in an offline-first system, or other devices won't know an item was removed.tombstones table or a specific DEL operation type.pending operations.
[Op1, Op2], lastKnownServerSeq: 100.serverSeq (101, 102).lastKnownServerSeq < CurrentServerSeq (e.g., 105), the server could reject (Optimistic Locking) OR accept and let the client resolve later (Eventual Consistency). For "Dumb Server" models, accepting and letting the client merge is often smoother for UX.serverSeq: 100".serverSeq: 102.gzip or Brotli for HTTP responses. For the payload itself, if JSON is verbose, consider a binary format (Protobuf/MsgPack) or just compressed JSON blobs if storage space is a concern.Replaying 100,000 operations to build the initial state is slow.
Coalescing is the practice of merging multiple granular updates to the same entity into a single operation before it is synced to the server.
A "Batch" operation is a container for multiple distinct actions that should be treated atomically.
BATCH op containing an array of sub-operations.user_id partitions.payload field before sending. The server syncs encrypted blobs. (End-to-End Encryption - E2EE).