## 分散式系統的 clock

Author here. Pleasantly surprised to see the article here.

Some context behind the article. I studied CRDTs for a few months, and noticed that different CRDT designs use logical clocks in different and clever ways. And I haven't seen anyone narrate all those ways of use in one article. My attempt with this article was to dredge up those flavors of logical clocks into one article and give them names for future reference.

(To respond to a couple of other comments, I ignored atomic (and gps-based) clocks in this discussion, as indicated in my footnote 3).

## 用 Automerge 處理 CRDT 問題

```(() => {
const Automerge = require('@automerge/automerge');

let doc1 = Automerge.init();

doc1 = Automerge.change(doc1, 'Init', doc => {
doc.text = new Automerge.Text();
doc.text.insertAt(0, 'h', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd', '.');
});

let doc2 = Automerge.clone(doc1);
let doc3 = Automerge.clone(doc1);

doc1 = Automerge.change(doc1, 'Capitalize', doc => {
doc.text.deleteAt(0);
doc.text.insertAt(0, 'H');
});
doc2 = Automerge.change(doc2, 'world => test', doc => {
delete doc.text.deleteAt(7, 5);
doc.text.insertAt(7, 'test');
});
doc3 = Automerge.change(doc3, 'world => example', doc => {
delete doc.text.deleteAt(7, 5);
doc.text.insertAt(7, 'example');
});

let finalDoc = Automerge.merge(doc1, doc2);
finalDoc = Automerge.merge(finalDoc, doc3);
console.log(finalDoc);
})();```

```{
text: Text {
elems: [
'H', 'e', 'l', 'l', 'o',
',', ' ', 't', 'e', 's',
't', 'e', 'x', 'a', 'm',
'p', 'l', 'e', '.'
]
}
}```

## ScyllaDB 1.7 要支援 Cassandra 的 Counter 了

Counter 是 Cassandra 裡一個特別的資料型態，總算是要 porting 進 ScyllaDB 了：「Scylla 1.7 introduces experimental support for counters」。

• once deleted, counter column value cannot be used again—this is a consequence of the fact that counters can only be incremented or decremented, they cannot be set to any specific value
• a table cannot contain both counter and regular columns—without this limitation it wouldn’t be possible to provide proper atomicity and isolation guarantees for updates that modify both counters and regular columns in a single row
• counter columns cannot be members of primary key
• updates are not idempotent – in case of a write failure the client cannot safely retry the request
counter columns cannot have time-to-live set

Scylla implements counters using state-based conflict-free replicated data types (CRDT), which allow atomic operations, like increment or decrement, to be performed locally without the need for synchronization between nodes.