Consistency Models
The article provides an overview of the concepts of eventual and causal consistency in Ditto.
The following scenario provides a walkthrough of the mechanics of version vectors, their role in determining merging behavior, and how different peers contribute to data replication:
The HLC uses the UInt128 data type to represent the Site_ID and 64bit timestamp in Ditto; however, for simplicity, the following scenario uses basic string and number types instead.
Local Peer A document: 123abc links to a version vector that indicates:
- Its locally-stored document is currently at vector version 5.
- The most recent incoming Remote Peer B changes were incorporated and merged at version 1.
- The Remote Peer C changes were incorporated and merged at vector version 4.
Local Peer A receives document changes from remote Peer B. The incoming document's version vector indicates:
- The remote Peer B incoming version vector is value 4.
- The most recent incoming Remote Peer B changes were of version 1; a value less than the incoming document version vector value of 4.
Since 4 is greater than 1, the local Peer A determines that the changes are new and should be incorporated and merged in order to remain consistent.
Causal Consistency is guaranteed by Ditto. It is causally consistent across any number of related changes, across many documents and different collections, as long as they are within the same Ditto appID.
To give an intuition about causal consistency, imagine the following scenario:
On a social network, Bob posts a message:
Then after some time, he posts:
To which Sue replies:
In contrast, an eventually consistent database shows messages in any order:
Ditto's Causal Consistency ensures that if a new message is written after seeing some prior message, then the new message is not visible unless that prior message is also visible.
To help differentiate Causal Consistency from stronger consistency models, imagine that Alice replies:
Causal Consistency allows the two concurrent messages "Great news!" from Sue and "Wonderful!" from Alice to appear in any order. Both of the following results are valid with Causal Consistency:
And: