Skip to content

Commit 859e7e5

Browse files
authored
docs: clarify peer id reuse safeguards (#27)
1 parent 687701b commit 859e7e5

2 files changed

Lines changed: 7 additions & 0 deletions

File tree

pages/docs/concepts/peerid_management.mdx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ doc.setPeerId("123123123"); // You can only set 64 bit integers as peer IDs
4141

4242
⚠️ **Warning**: Manual assignment requires careful conflict avoidance.
4343

44+
If you intentionally reuse a Peer ID, persist the document's local data (snapshot, updates, or durable cache) alongside that ID and load it before fetching or applying any remote updates. This ensures the `(peerId, counter)` pairs continue to reference the same operations. Skipping this step risks generating a new operation that reuses an existing operation ID, which leads to inconsistent replicas.
45+
4446
## Counter System
4547

4648
Each peer maintains a monotonic counter starting at 0:
@@ -65,6 +67,7 @@ console.log(doc.version()); // { "1": 2 }
6567
- Use user IDs as peer IDs, because an user can have multiple devices
6668
- Use fixed IDs
6769
- Reuse IDs without proper management
70+
- Allow multiple browser tabs or processes to operate on the same reused Peer ID in parallel—coordinate access with locks so that only one session emits operations for that ID at a time
6871

6972
## Related Documentation
7073

pages/docs/tutorial/tips.mdx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ from an identical baseline without generating unnecessary operations.
2020

2121
When using `setPeerId`, you must avoid having two parallel peers use the same PeerId. This can cause serious consistency problems in your application.
2222

23+
If you plan to reuse a PeerId across sessions, make sure that you persist the document's local state together with that PeerId and load it before applying any remote updates. Otherwise, the same `peerId + counter` combination could end up referring to two different operations, producing divergence that cannot be resolved automatically.
24+
2325
<details>
2426
<summary>Why</summary>
2527

@@ -34,6 +36,8 @@ Be careful when reusing PeerIds (this optimization is often unnecessary). You sh
3436

3537
If you must reuse PeerIds, you need to carefully manage your local PeerId cache with proper locking mechanisms. This would allow only one tab to "take" a specific PeerId, while other tabs use random IDs. The PeerId should be returned to the cache when no longer in use.
3638

39+
In addition to locking, avoid having multiple browser tabs (or apps on different processes) use the same PeerId concurrently. Coordinating access in this way ensures that a reused PeerId never emits two different operations with the same operation ID.
40+
3741
</details>
3842

3943
---

0 commit comments

Comments
 (0)