You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
docs(connectivity): document peer id collisions and replica identity
Explains why replicas must use unique peer ids, how Origin detects and
rejects collisions, how to recover, and how to interpret ops_trimmed
in sync session logs.
Copy file name to clipboardExpand all lines: docs/connectivity/sync-protocol.rdx
+31-1Lines changed: 31 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -28,6 +28,36 @@ Schema changes (new columns, new collections) are discovered automatically by co
28
28
5. Origin validates constraints (UNIQUE, FK, CHECK) via Raft
29
29
6. Committed deltas are broadcast to all connected clients with matching shape subscriptions
30
30
31
+
## Replica Identity
32
+
33
+
Every CRDT operation is attributed to the **peer id** of the replica that made it. A peer id must belong to exactly one replica of one collection.
34
+
35
+
Two replicas that share a peer id number their operations from the same counter range. The merge cannot tell those apart from a client re-sending history it has already delivered, so it discards whichever arrives second — the writes are gone, and to the client they looked accepted.
36
+
37
+
Origin therefore binds each peer id to the first producer that uses it in a collection, and refuses any other producer:
38
+
39
+
```
40
+
DeltaReject: PEER_ID_COLLISION: peer id 1 on collection 'notes' is already
41
+
owned by another replica; generate a new peer id and resync
42
+
```
43
+
44
+
To recover, generate a fresh peer id for the replica and resync. Applications should:
45
+
46
+
- Derive a peer id that is unique per **installation**, not per user or per device model.
47
+
- Generate a **new** peer id whenever the local store is reset, reinstalled, or restored from a backup taken by another replica. A replica that keeps its old peer id after wiping its store collides with its own earlier history, which Origin cannot refuse — it owns that peer id — and the writes are reported as duplicates rather than applied.
48
+
49
+
The same peer id in two different collections is not a collision; each collection is a separate document whose counter ranges never meet.
50
+
51
+
### Observing discarded operations
52
+
53
+
Each sync session's close line reports what the merge absorbed:
`ops_trimmed` counts operations that arrived already known to Origin. A healthy resync re-sends a prefix and then advances, so `applied` rises alongside it. A session whose deltas are all deduplicated with nothing applied is the peer-id collision shape.
60
+
31
61
## Shape Subscriptions
32
62
33
63
Devices subscribe to a subset of data via shape subscriptions (wire-level sync feature, not SQL):
@@ -48,6 +78,6 @@ If a local write violates a constraint on Origin, a typed `CompensationHint` is
48
78
- `IntegrityViolation` — other integrity-rule violation
49
79
- `PermissionDenied` — insufficient privilege
50
80
- `RateLimited` — quota or rate limit exceeded
51
-
- `Custom` — application-specific handling
81
+
- `Custom` — application-specific handling, including `peer_id_collision` (see [Replica Identity](#replica-identity))
52
82
53
83
The application handles the conflict — no silent data loss.
0 commit comments