Ditto automatically synchronizes data between devices in your mesh. Devices are able to express which data they would like to synchronize using subscription queries.
REGISTERs
to a predefined value, the resulting merge accurately represents the original input and some rational interpretation of that input."123abc"
on Peer A.
"A"
has incorporated change from other peers "B"
and "C"
at times 1
and 4
respectively.
If an incoming change arrives at Peer "A"
with "B": 4
, then Document will merge the incoming data. This is because Peer "A"
determined that the document’s current state has yet not seen the new change.
REGISTER
stores a single scalar value. You can encode the value using any JSON-compatible scalar subtype, such as a string
, boolean
, JSON object to represent two or more fields as a single object, and so on.
Since a REGISTER
acts as a single object, to update just a single nested field of an object stored in a REGISTER
, you’d need to update the entire object by providing the entire set of nested key-value pairs.
The REGISTER
type adheres to the last-write-wins principle when handling concurrency conflicts. This means that when changes occur, all peers observing the change will sequence these changes in the same order. This ensures that only a consistent single value syncs across the entire peer-to-peer mesh.
For example, one flight attendant updates a customer’s seat number to 6
and another to seat 9
. When the two conflicting versions merge, the edit with the latest timestamp wins.
Put another way, by enforcing the last-write-wins merge strategy, for events A and B, where B is a result of A, event A always occurs before B.
MAP
type uses the add-wins approach to resolving concurrency conflicts.
So instead of choosing a single definitive value to merge and distribute across peers like the last-write-wins strategy utilized by the REGISTER
and ATTACHMENT
data types, you can choose them all — even if the data is already contained within the MAP
object.
The MAP
data type in Ditto forms a tree-like, parent-child relationship within a document’s dataset. This is similar to a JSON object, which functions as a single unit. However, a MAP
allows:
REGISTER
, MAP
, and ATTACHMENT
as values for its keys.MAP
can be modified independently without affecting other pairs.MAP
.ATTACHMENT
data type lets you link extensive amounts of binary data to a document for on-demand sync, both online or offline, without any conflicts. For instance, you can link and asynchronously sync large files that change infrequently or deeply embedded documents.
When incorporating an attachment into a document, rather than inserting the entire resource-heavy ATTACHMENT object, internally Ditto inserts a pointer object, known as the attachment token.
Unlike documents that are always accessible and automatically synced according to sync subscriptions, peers do not automatically fetch attachment data associated with a sync subscription. Therefore, before accessing an attachment, you must explicitly fetch it.
For practical guidance on using attachments within your application, see Working with Attachments