Skill — Real-Time Sync
When this skill activates
Any task involving real-time synchronization, conflict resolution, CRDTs,
operational transforms, offline-first architecture, or collaborative editing.
Mandatory actions when this skill is active
Before writing any code
- Classify data types to determine the conflict resolution strategy.
- Define the consistency model (strong, eventual, causal).
- Identify offline requirements and maximum acceptable sync lag.
During implementation
- Implement conflict detection at field level, not document level.
- Use vector clocks or hybrid logical clocks for causal ordering.
- Design the sync engine as a separate layer from business logic.
- Handle network partitions gracefully (queue changes, reconcile on reconnect).
After implementation
- Test with simulated partitions and concurrent edits.
- Verify convergence (all clients reach same state eventually).
- Document conflict resolution strategy per data type.
Conflict Resolution Strategies
| Strategy |
Pro |
Con |
Use For |
| LWW (Last Write Wins) |
Simple, no user action |
Silent data loss |
Preferences, status fields |
| CRDTs |
Auto-merge, no conflicts |
Limited types, storage overhead |
Counters, sets, registers |
| OT (Operational Transform) |
Precise intent preservation |
Complex, needs server |
Real-time text editing |
CRDT Catalog
- G-Counter: grow-only. Each node keeps own count, total = sum. Use for page views.
- PN-Counter: two G-Counters (increment/decrement). Use for inventory, votes.
- LWW-Register: single value + timestamp, latest wins. Use for simple fields.
- OR-Set: add/remove with unique tags, add-wins bias. Use for carts, tag lists.
Offline-First Architecture
[Local DB] <-> [Sync Engine] <-> [Remote Server]
- All writes go to local DB first (instant UX, zero latency).
- Sync engine runs in background, uploads pending changes.
- On reconnect: pull remote → detect conflicts → resolve → merge.
- Unresolvable conflicts surface to user (never silently drop).
Sync Protocols
- Push-Based: server pushes via WebSocket/SSE. Lowest latency, needs persistent connection.
- Pull-Based: client polls with cursor. Simple, works everywhere, higher latency.
- Hybrid (recommended): push notification "changes exist" → client fetches via HTTP.
Optimistic Replication
- Apply locally immediately, replicate async.
- On conflict: reconcile via chosen strategy (LWW, CRDT, manual).
- Rollback if server rejects (revert local, show error to user).
Vector Clocks
- Each node maintains a vector of logical timestamps (one per node).
- Determines causality: happened-before, concurrent, or identical.
- Concurrent events require conflict resolution; causal events merge cleanly.
Self-check before task completion
1---2name: real-time-sync3description: Skill — Real-Time Sync4---56# Skill — Real-Time Sync78## When this skill activates9Any task involving real-time synchronization, conflict resolution, CRDTs,10operational transforms, offline-first architecture, or collaborative editing.1112## Mandatory actions when this skill is active1314### Before writing any code151. Classify data types to determine the conflict resolution strategy.162. Define the consistency model (strong, eventual, causal).173. Identify offline requirements and maximum acceptable sync lag.1819### During implementation20- Implement conflict detection at field level, not document level.21- Use vector clocks or hybrid logical clocks for causal ordering.22- Design the sync engine as a separate layer from business logic.23- Handle network partitions gracefully (queue changes, reconcile on reconnect).2425### After implementation26- Test with simulated partitions and concurrent edits.27- Verify convergence (all clients reach same state eventually).28- Document conflict resolution strategy per data type.2930## Conflict Resolution Strategies3132| Strategy | Pro | Con | Use For |33|----------|-----|-----|---------|34| LWW (Last Write Wins) | Simple, no user action | Silent data loss | Preferences, status fields |35| CRDTs | Auto-merge, no conflicts | Limited types, storage overhead | Counters, sets, registers |36| OT (Operational Transform) | Precise intent preservation | Complex, needs server | Real-time text editing |3738## CRDT Catalog3940- **G-Counter**: grow-only. Each node keeps own count, total = sum. Use for page views.41- **PN-Counter**: two G-Counters (increment/decrement). Use for inventory, votes.42- **LWW-Register**: single value + timestamp, latest wins. Use for simple fields.43- **OR-Set**: add/remove with unique tags, add-wins bias. Use for carts, tag lists.4445## Offline-First Architecture4647```48[Local DB] <-> [Sync Engine] <-> [Remote Server]49```5051- All writes go to local DB first (instant UX, zero latency).52- Sync engine runs in background, uploads pending changes.53- On reconnect: pull remote → detect conflicts → resolve → merge.54- Unresolvable conflicts surface to user (never silently drop).5556## Sync Protocols5758- **Push-Based**: server pushes via WebSocket/SSE. Lowest latency, needs persistent connection.59- **Pull-Based**: client polls with cursor. Simple, works everywhere, higher latency.60- **Hybrid** (recommended): push notification "changes exist" → client fetches via HTTP.6162## Optimistic Replication63- Apply locally immediately, replicate async.64- On conflict: reconcile via chosen strategy (LWW, CRDT, manual).65- Rollback if server rejects (revert local, show error to user).6667## Vector Clocks68- Each node maintains a vector of logical timestamps (one per node).69- Determines causality: happened-before, concurrent, or identical.70- Concurrent events require conflict resolution; causal events merge cleanly.7172## Self-check before task completion7374- [ ] Is conflict resolution appropriate for each data type?75- [ ] Does the system handle network partitions without data loss?76- [ ] Do all clients converge to the same state eventually?77- [ ] Is offline support implemented with local-first writes?78- [ ] Are concurrent edits tested with realistic scenarios?79- [ ] Is the sync protocol resilient to disconnection/reconnection?80- [ ] Are unresolvable conflicts surfaced to the user?