Consistency Model (Strongest Satisfied)

medium · distributed-systems, consistency, ordering

Consistency Model (Strongest Satisfied)

For a single key, you are given a sequence of writes and reads with vector clocks. For each read, return the strongest consistency model it satisfies among:

  • linearizable
  • causal
  • eventual

Rules:

  • Let latest be the write with the greatest Time such that write.Time <= read.Time.
  • Linearizable if read.ValueVC equals latest.VC.
  • Causal if read.ValueVC dominates read.Context (element-wise >=).
  • Otherwise the read is only eventual.

Return the label for each read in order.

Types

type Write struct {
    Time int
    VC   []int
}

type Read struct {
    Time     int
    Context  []int
    ValueVC  []int
}

Function signature

func StrongestConsistency(writes []Write, reads []Read) []string

Example

writes = [{Time:1, VC:[1,0]}, {Time:5, VC:[2,0]}]
reads  = [{Time:6, Context:[2,0], ValueVC:[2,0]}, {Time:6, Context:[1,0], ValueVC:[1,0]}]

output = ["linearizable", "causal"]

Constraints

  • 0 <= len(writes), len(reads) <= 100000
  • writes and reads are sorted by Time ascending

Notes

  • If no latest write exists, treat latest.VC as all zeros.
  • When comparing vector clocks of different lengths, missing entries are treated as 0.
Run tests to see results
No issues detected