Conflict-free replicated data types v4
Conflict-free replicated data types (CRDT) support merging values from concurrently modified rows instead of discarding one of the rows as traditional resolution does.
Each CRDT type is implemented as a separate PostgreSQL data type with
an extra callback added to the bdr.crdt_handlers
catalog. The merge
process happens inside the BDR writer on the apply side without any user
action needed.
CRDTs require the table to have column-level conflict resolution enabled, as documented in CLCD.
The only action you need to take is to use a particular data type in CREATE/ALTER TABLE rather than standard built-in data types such as integer. For example, consider the following table with one regular integer counter and a single row:
Suppose you issue the following SQL on two nodes at same time:
After both updates are applied, you can see the resulting values using this query:
This code shows that you lost one of the increments due to the update_if_newer
conflict resolver. If you use the CRDT counter data type instead,
the result looks like this:
Again issue the following SQL on two nodes at same time, and then wait for the changes to be applied:
This example shows that CRDTs correctly allow accumulator columns to work, even in the face of asynchronous concurrent updates that otherwise conflict.
The crdt_gcounter
type is an example of state-based CRDT types that
work only with reflexive UPDATE SQL, such as x = x + 1
, as the example shows.
The bdr.crdt_raw_value
configuration option determines whether queries
return the current value or the full internal state of the CRDT type. By
default, only the current numeric value is returned. When set to true
,
queries return representation of the full state. You can use the special hash operator
(#
) to request only the current numeric value without using the
special operator (the default behavior). If the full state is
dumped using bdr.crdt_raw_value = on
, then the value can
reload only with bdr.crdt_raw_value = on
.
Note
The bdr.crdt_raw_value
applies formatting only of data returned
to clients, that is, simple column references in the select list. Any column
references in other parts of the query (such as WHERE
clause or even
expressions in the select list) might still require use of the #
operator.
Another class of CRDT data types is referred to delta CRDT types. These are a special subclass of operation-based CRDTs.
With delta CRDTs, any update to a value is compared to the previous value on the same node. Then a change is applied as a delta on all other nodes.
Suppose you issue the following SQL on two nodes at same time:
After both updates are applied, you can see the resulting values using this query:
With a regular integer
column, the result is 2
. But
when you update the row with a delta CRDT counter, you start with the OLD
row version, make a NEW row version, and send both to the remote node.
There, compare them with the version found there (e.g.,
the LOCAL version). Standard CRDTs merge the NEW and the LOCAL version,
while delta CRDTs compare the OLD and NEW versions and apply the delta
to the LOCAL version.
The CRDT types are installed as part of bdr
into the bdr
schema.
For convenience, the basic operators (+
, #
and !
) and a number
of common aggregate functions (min
, max
, sum
, and avg
) are
created in pg_catalog
. This makes them available without having to tweak
search_path
.
An important question is how query planning and optimization works with these
new data types. CRDT types are handled transparently. Both ANALYZE
and
the optimizer work, so estimation and query planning works fine without
having to do anything else.
State-based and operation-based CRDTs
Following the notation from [1], both operation-based and state-based CRDTs are implemented.
Operation-based CRDT types (CmCRDT)
The implementation of operation-based types is trivial because the operation isn't transferred explicitly but computed from the old and new row received from the remote node.
Currently, these operation-based CRDTs are implemented:
crdt_delta_counter
—bigint
counter (increments/decrements)crdt_delta_sum
—numeric
sum (increments/decrements)
These types leverage existing data types (for example, crdt_delta_counter
is
a domain on a bigint
) with a little bit of code to compute the delta.
This approach is possible only for types for which the method for computing the delta is known, but the result is simple and cheap (both in terms of space and CPU) and has a couple of additional benefits. For example, it can leverage operators/syntax for the underlying data type.
The main disadvantage is that you can't reset this value reliably in an asynchronous and concurrent environment.
Note
Implementing more complicated operation-based types by creating custom data types is possible, storing the state and the last operation. (Every change is decoded and transferred, so multiple operations aren't needed). But at that point, the main benefits (simplicity, reuse of existing data types) are lost without gaining any advantage compared to state-based types (for example, still no capability to reset) except for the space requirements. (A per-node state isn't needed.)
State-based CRDT types (CvCRDT)
State-based types require a more complex internal state and so can't use the regular data types directly the way operation-based types do.
Currently, four state-based CRDTs are implemented:
crdt_gcounter
—bigint
counter (increment-only)crdt_gsum
—numeric
sum/counter (increment-only)crdt_pncounter
—bigint
counter (increments/decrements)crdt_pnsum
—numeric
sum/counter (increments/decrements)
The internal state typically includes per-node information, increasing the on-disk size but allowing added benefits. The need to implement custom data types implies more code (in/out functions and operators).
The advantage is the ability to reliably reset the values, a somewhat self-healing nature in the presence of lost changes (which doesn't happen in a cluster that operates properly), and the ability to receive changes from other than source nodes.
Consider, for example, that a value is modified on node A, and the change gets replicated to B but not C due to network issue between A and C. If B modifies the value and this change gets replicated to C, it includes even the original change from A. With operation-based CRDTs, node C doesn't receive the change until the A-C network connection starts working again.
The main disadvantages of CvCRDTs are higher costs in terms of disk space and CPU usage. A bit of information for each node is needed, including nodes that were already removed from the cluster. The complex nature of the state (serialized into varlena types) means increased CPU use.
Disk-space requirements
An important consideration is the overhead associated with CRDT types, particularly the on-disk size.
For operation-based types, this is trivial, because the types are merely domains on top of other types and so have the same disk space requirements no matter how many nodes are there.
crdt_delta_counter
— Same asbigint
(8 bytes)crdt_delta_sum
— Same asnumeric
(variable, depending on precision and scale)
There's no dependency on the number of nodes because operation-based CRDT types don't store any per-node information.
For state-based types, the situation is more complicated. All the types
are variable-length (stored essentially as a bytea
column) and consist
of a header and a certain amount of per-node information for each node
that modified the value.
For the bigint
variants, formulas computing approximate size are (N
denotes the number of nodes that modified this value):
crdt_gcounter
—32B (header) + N * 12B (per-node)
crdt_pncounter
-