vlcn-io / cr-sqlite

Convergent, Replicated SQLite. Multi-writer and CRDT support for SQLite
https://vlcn.io
MIT License
2.77k stars 76 forks source link

Document how users can preserve transactionality in their networking layer #303

Open tantaman opened 1 year ago

tantaman commented 1 year ago

The core of cr-sqlite drops metadata aggressively to keep DB size down, even when offline for prolonged periods of time or doing thousands of writes per second.

This choice, however, impacts how transactions are synced (documented here: https://vlcn.io/blog/how-crsqlite-transactions-work-today) but users can maintain transactional sync in the network layer if desired.

The basic idea is that the network layer would package changes from last_seen db_version to current db_version in a single block and peers would write that block in a single transaction.

Blocks can be as small as a single db_version or as large as the user wants, so long as:

  1. The entire block is applied at once
  2. Blocks are applied in-order

If blocks have not been sent to a peer yet, they can be merged together to save space. There is a balance here between:

  1. having really large blocks but less overall data
  2. having really small blocks but more overall data