G-Counter Merge

easy · distributed-systems, crdt, conflict-resolution

G-Counter Merge

A G-Counter is a grow-only CRDT. Each replica maintains a vector of counts (one per node). The counter's value is the sum of the vector.

To merge two replicas, take the element-wise max of their vectors.

Given two counter vectors of equal length, return:

  • the merged vector
  • the total counter value (sum of merged vector)

Function signature

func MergeGCounter(a, b []int) (merged []int, total int)

Example

a = [1,2,0]
b = [2,1,4]
merged = [2,2,4]
total = 8

Constraints

  • len(a) == len(b)
  • 0 <= a[i], b[i]
Run tests to see results
No issues detected