Skip to main content

Register

Ditto documents support all primitive JSON compatible types like string, boolean, number, etc.

All primitive types in Ditto are internally represented as so called Registers with "Last Writer Wins" semantics.

When a device updates a document, the last write to the document will win. A timestamp is attached to each update, and updates with later timestamps always win. To learn more about how this works, read about Ditto's Hybrid Logical Clock.

Data TypeAllowed Values
BooleanEither true or false.
StringA utf-8 encodable string value.
Unsigned integerAn unsigned 64-bit integer value.
Signed integerA signed 64-bit integer value.
FloatA 64-bit floating point value.
ArrayArrays are an ordered list of values. Arrays can contain all primitive values as well as nested collection types like other Arrays or Maps.
MapSometimes referred to as a dictionary, a Map represents a nested object within the overall document. Comparing values at the map level for equality first checks that each key and each value match.
nullThis represents an absence of a value.
do {    // Insert JSON-compatible data into Ditto    try ditto.store["foo"].upsert([        "boolean": true,        "string": "Hello World",        "number": 10,        "map": ["key": "value"],        "array": [1,2,3],        "null": nil    ])}catch {    //handle error    print(error)}

How it works

Ditto uses a temporal timestamp called an HLC (hybrid logical clock) to associate each register value with a time. When two registers are merged the value with the highest timestamp wins.

For example, one attendant updates a customer's seat number to '6', and another to seat '9'. When the two conflicting versions merge, the edit with the highest timestamp wins.

In that case that two updates occur at exactly the same time, we use the unique ID of the Ditto SDK to tie-break, preferring the highest ID.

In the case of a number, like a seat number, a last-write-wins register is a good choice. Some numbers represent quantities over time, and that is when a Counter is useful.

Read more about how Ditto's CRDT works.

New and Improved Docs

Ditto has a new documentation site at https://docs.ditto.live. This legacy site is preserved for historical reference, but its content is not guaranteed to be up to date or accurate.