Offline-first and sync

Peer-to-peer sync vs cloud sync

In cloud sync, every device sends its changes to a server and fetches other devices' changes from it, so two phones in the same room still talk through the internet. In peer-to-peer sync, devices exchange changes directly with each other, over Bluetooth or a local network, so they stay in step with no internet at all. An app can combine them, syncing locally when people are together and through the cloud when a connection exists.

TL;DR

The choice usually follows the setting. Cloud sync suits teams with good connectivity and keeps authority, access control and backups on one server. Peer-to-peer sync pays off for crews who are together exactly when the internet is not, but needs mergeable data types such as CRDTs and moves authorization onto every device. Many apps combine the two.

Learning objectives

After reading this article you will be able to:

  • Distinguish cloud sync from peer-to-peer sync by the path a change takes
  • List the responsibilities peer-to-peer sync moves from the server onto devices
  • Describe how an app can combine peer-to-peer sync with a backend system of record

Cloud sync vs peer-to-peer sync paths

In cloud sync a change goes to a server, and in peer-to-peer sync it goes straight to other devices. Every sync system has to decide how a change made on one device reaches the others.

Cloud sync sends it to a server. The server stores the change and passes it on to every other device that needs it, the next time each one connects. Cloud Firestore works this way: its offline persistence keeps a local copy of the data the app is using, lets the app read and write it while offline, and synchronizes local changes with the Cloud Firestore backend when the device comes back online. Apple’s Core Data can mirror a local store to CloudKit in the same spirit.

Peer-to-peer sync sends it straight to other devices. When two phones are near each other, they find each other over Bluetooth or a shared local network and exchange the changes each is missing. No server takes part. Ditto’s documentation, for example, describes devices that sync with each other through a peer-to-peer mesh and choose which data they want with subscription queries.

The two are not exclusive. A device can sync with nearby peers now and with a server later, as long as the data model can merge changes that arrive by different routes.

Peer-to-peer sync vs cloud sync use cases

Peer-to-peer sync is better when devices are in the same room with no internet, and cloud sync is better when devices are far apart and both have internet.

Cloud syncPeer-to-peer sync
Devices far apartWorks whenever both have internetNeeds a path through other devices or a relay
Devices in the same room, no internetWaits for a connectionSyncs directly
A single authoritative copyThe server holds itNo copy is authoritative by default
Access controlEnforced in one place, the serverEnforced on every device, with keys
Backups and historyOn the serverOn the devices, unless one also syncs to a server
Running costServer, storage, and bandwidthNo server in the local path

The table shows why the choice usually follows the setting. An office team with good connectivity gets little from peer-to-peer sync. A crew on a ship, a team at a remote site, or staff in a packed venue get the most from it, because they are together exactly when the internet is not.

CRDTs and mergeable data for peer-to-peer sync

Peer-to-peer sync usually rests on data types designed to merge without coordination, such as CRDTs, because it has no server to order changes. Sync through a server can rely on the server to order changes and settle conflicts. Peer-to-peer sync cannot: two devices may edit the same record while apart and meet again later, with no referee. Yjs, a CRDT library, states that it makes no assumptions about the network: as long as all changes eventually arrive, the documents sync, in whatever order the updates are applied.

The local-first research from Ink & Switch puts this as one of seven ideals, “the network is optional”: software should keep working, and keep syncing where it can, without depending on a server being reachable. It argues that the primary copy of the data should live on the user’s devices, with servers in a supporting role.

How sync conflicts are resolved covers the merge rules in more detail.

Peer-to-peer sync trade-offs

Peer-to-peer sync moves some responsibilities from the server onto every device:

  • Authorization. Without a central server to check every write, devices need to know which peers may read and change which data. Encryption keys and signed identities take the server’s place.
  • Discovery and transport. Devices have to find each other and keep a link up, which depends on radios, permissions, and background limits on phones.
  • Storage. Each device keeps the history it needs to merge, so document size and retention matter more.
  • Business rules. Merging guarantees that copies converge, not that the result is valid. A rule such as “a slot can only be booked once” still needs an authority, which is often the backend once it is reachable.

Cloud sync keeps those in one place but stops at the edge of coverage.

Hybrid peer-to-peer and cloud sync

A common pattern for combining peer-to-peer and cloud sync is to treat the backend as the system of record and use peer-to-peer sync for the time between connections. Devices exchange changes locally while they are together and offline. When any device reaches the internet, it sends the accumulated changes to the backend, which applies its business rules and becomes the reference again. Because the merges are deterministic, the order in which changes arrive does not change the result.

Offline Protocol’s replicated documents follow this pattern. Devices in an encrypted space edit documents offline and merge them when they meet, while the application’s own database stays the system of record for everything outside that shared scope.

Frequently asked questions

Can two phones sync data without internet?

Yes, with peer-to-peer sync. When two phones are near each other, they find each other over Bluetooth or a shared local network and exchange the changes each is missing, and no server takes part.

Is Cloud Firestore sync peer-to-peer?

No. Cloud Firestore uses cloud sync. Its offline persistence keeps a local copy of the data the app is using and synchronizes local changes with the Cloud Firestore backend when the device comes back online.

How is access control handled in peer-to-peer sync?

Without a central server to check every write, devices need to know which peers may read and change which data. Encryption keys and signed identities take the server's place.

Does peer-to-peer sync guarantee the merged data is valid?

No. Merging guarantees that copies converge, not that the result is valid. A rule such as a slot only being bookable once still needs an authority, which is often the backend once it is reachable.

What does "the network is optional" mean in local-first software?

It is one of the seven ideals in Ink & Switch's local-first research. Software should keep working, and keep syncing where it can, without depending on a server being reachable.

Sources

Build it with Offline Protocol

The shared state guide shows replicated documents syncing between devices that share an encrypted space, with the internet disconnected, and how to check that both copies converge after a partition.

Read the shared state guide