设计并实现应用离线同步机制
围绕应用数据模型设计离线优先同步方案,涵盖客户端与服务端实现、冲突处理策略及故障测试。
完整提示词
<context>
You are an engineer who has built offline-first apps used in the field. Offline sync fails in ways users notice: edits that vanish, duplicates created by retries, deleted records that come back, and silent overwrites when two people edited the same thing. A design that holds up has these parts:
- A local database as the app's source of truth for the UI (SQLite on mobile, IndexedDB on the web), so the app reads and writes locally and syncs in the background.
- Client-generated ids (UUIDs, ideally time-ordered such as UUIDv7) so records can be created offline and referenced before the server sees them.
- An outbox: every local change is recorded as a mutation with an idempotency key, sent in order, and removed only after the server confirms. Retries are safe because the server deduplicates by key.
- A change feed ordered by the server: the client pulls "changes since cursor N", where N is a server-issued sequence or version, never the device clock.
- Tombstones for deletes, kept long enough for every device to sync, so deleted records do not reappear.
- A conflict policy chosen per entity or field: last-writer-wins by server order for low-value fields; field-level merge when people usually edit different fields; domain rules (an inventory count applied as a delta, not overwritten); CRDTs (for example Yjs or Automerge) for collaborative text and lists; or surfacing the conflict to the user when the data matters and cannot merge.
- Local schema migrations, storage limits and eviction (browsers can evict site data; ask for persistent storage), and auth tokens expiring while offline.
Several backends and sync engines provide much of this; adopting one is often better than building it, and the design questions stay the same.
</context>
<task>
Design and implement offline sync.
App:
{{app}}
Data model:
{{data_model}}
1. If it is unclear how long users stay offline, which records can be edited by more than one person, or which platforms are targeted, ask up to three questions and stop.
2. State the requirements and choose the conflict policy for each entity, and for individual fields where they differ, with the reason. Say whether an existing sync engine or backend feature would fit and what it would replace; continue with the custom design unless the user asked otherwise.
3. Define the data model changes: client ids, version or sequence columns, updated-by fields, tombstones, the outbox table, and the sync cursor.
4. Define the sync protocol: push (batching, ordering, idempotency, per-mutation results including rejections) then pull (changes since cursor, paging, tombstones), and when sync runs (app start, foreground, connectivity change, after local writes, periodic).
5. Implement the client: local writes plus outbox in one local transaction, the sync loop with exponential backoff and jitter, applying pulled changes without clobbering pending local edits, conflict handling per the policy, and UI state for pending, synced and failed items.
6. Implement the server: idempotent mutation handling, validation and authorisation per mutation, conflict detection using versions, the change feed endpoint, and tombstone retention.
7. List the failure cases and write tests for them.
</task>
<constraints>
- Never order or resolve conflicts by device clocks.
- Never drop a local change silently; a rejected mutation must be visible to the user or logged with a recovery path.
- Keep sensitive data stored on devices to what the feature needs, and say whether it should be encrypted at rest.
{{> guardrails/scope-discipline}}
{{> output/uncertainty}}
</constraints>
<output_format>
## Requirements and conflict policy
Table: Entity or field | Who edits it | Policy | Why.
## Data model changes
Schema or migration code for client and server.
## Sync protocol
Request and response shapes for push and pull, as code blocks, and the triggers.
## Client
Code.
## Server
Code.
## Failure cases and tests
Tests for: a retry after a lost response not duplicating a record, a delete on one device while another edits offline, two devices editing the same field, an app killed mid-sync, a token expiring offline, and a local schema migration with pending outbox items.
</output_format>可替换内容
- {{app}}
- {{data_model}}
- {{> guardrails/scope-discipline}}
- {{> output/uncertainty}}
分类与标签
编程开发、离线优先应用、客户端同步、数据冲突处理