Version Vector
Maintain a list of counters, one per cluster node, to detect concurrent updates.
Problem
If multiple servers allow the same key to be updated, it’s important to detect when the values are concurrently updated across a set of replicas.
Solution
Each key value is associated with a version vector that maintains a number for each cluster node.
In essence, a version vector is a set of counters, one for each node. A version vector for three nodes (blue, green, black) would look something like [blue: 43, green: 54, black: 12] . Each time a node has an internal update, it updates its own counter, so an update in the green node would change the vector to [blue: 43, green: 55, black: 12] . Whenever two nodes communicate, they synchronize their vector stamps, allowing them to detect any simultaneous updates.
The Difference with Vector Clock
Vector Clock implementation is similar. But vector clocks are used to track every event occurring on the server. In contrast, version vectors are used to detect concurrent updates to the same key across a set of replicas. So an instance of a version vector is stored per key and not per server. Databases like Riak 52 use the term version vector instead of vector clock for their implementation. 53
-
[https://riak.com](https://riak.com) -
[https://riak.com/posts/technical/vector-clocks-revisited/index.html?p=9545.html](https://riak.com/posts/technical/vector-clocks-revisited/index.html?p=9545.html)
A typical version vector implementation is as follows:
Click here to view code image
class VersionVector...
private final TreeMap<String, Long> versions;
public VersionVector() {
this(new TreeMap<>());
}
public VersionVector(TreeMap<String, Long> versions) {
this.versions = versions;
}
public VersionVector increment(String nodeId) {
TreeMap<String, Long> versions = new TreeMap<>();
versions.putAll(this.versions);
Long version = versions.get(nodeId);
if(version == null) {
version = 1L;
} else {
version = version + 1L;
}
versions.put(nodeId, version);
return new VersionVector(versions);
}
Each value stored on the server is associated with a version vector:
Click here to view code image
class VersionedValue...
String value;
VersionVector versionVector;
public VersionedValue(String value, VersionVector versionVector) {
this.value = value;
this.versionVector = versionVector;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
VersionedValue that = (VersionedValue) o;
return Objects.equal(value, that.value)
&& Objects.equal(versionVector, that.versionVector);
}
@Override
public int hashCode() {
return Objects.hashCode(value, versionVector);
}
Comparing Version Vectors
Version vectors are compared by comparing version number for each node. A version vector A is considered higher than the version vector B if both have version number for the same cluster nodes and each version number is higher in A than the one in B. If neither vector has all of the version numbers higher, or if they have version numbers for different cluster nodes, they are considered concurrent.
Here are some example comparisons:
{blue:2, green:1} | is greater than | {blue:1, green:1} |
{blue:2, green:1} | is concurrent with | {blue:1, green:2} |
{blue:1, green:1, red: 1} | is greater than | {blue:1, green:1} |
{blue:1, green:1, red: 1} | is concurrent with | {blue:1, green:1, pink: 1} |
The comparison in database like Voldemort 54 is implemented as follows:
-
[https://www.project-voldemort.com/voldemort](https://www.project-voldemort.com/voldemort)
public enum Ordering {
Before,
After,
Concurrent
}
Click here to view code image
class VersionVector...
public static Ordering compare(VersionVector v1, VersionVector v2) {
validateNotNull(v1, v2);
SortedSet<String> v1Nodes = v1.getVersions().navigableKeySet();
SortedSet<String> v2Nodes = v2.getVersions().navigableKeySet();
SortedSet<String> commonNodes = getCommonNodes(v1Nodes, v2Nodes);
// Determine if v1 or v2 has more nodes than common nodes
boolean v1Bigger = v1Nodes.size() > commonNodes.size();
boolean v2Bigger = v2Nodes.size() > commonNodes.size();
// Compare versions for common nodes
for (String nodeId : commonNodes) {
if (v1Bigger && v2Bigger) {
break; // No need to compare further
}
long v1Version = v1.getVersions().get(nodeId);
long v2Version = v2.getVersions().get(nodeId);
if (v1Version > v2Version) {
v1Bigger = true;
} else if (v1Version < v2Version) {
v2Bigger = true;
}
}
return determineOrdering(v1Bigger, v2Bigger);
}
private static Ordering determineOrdering(boolean v1Bigger,
boolean v2Bigger) {
if (!v1Bigger && !v2Bigger) {
return Ordering.Before;
} else if (v1Bigger && !v2Bigger) {
return Ordering.After;
} else if (!v1Bigger && v2Bigger) {
return Ordering.Before;
} else {
return Ordering.Concurrent;
}
}
private static void validateNotNull(VersionVector v1, VersionVector v2) {
if (v1 == null || v2 == null) {
throw new IllegalArgumentException(
"Can't compare null vector clocks!");
}
}
private static SortedSet<String> getCommonNodes(SortedSet<String> v1Nodes,
SortedSet<String> v2Nodes) {
// get clocks(nodeIds) that both v1 and v2 has
SortedSet<String> commonNodes = Sets.newTreeSet(v1Nodes);
commonNodes.retainAll(v2Nodes);
return commonNodes;
}
Using Version Vector in a Key-Value Store
The version vector can be used in a key-value storage as follows. A list of versioned values is needed, as there can be multiple values which are concurrent.
Click here to view code image
class VersionVectorKVStore...
public class VersionVectorKVStore {
Map<String, List<VersionedValue>> kv = new HashMap<>();
When a client wants to store a value, it first reads the latest known version for the given key. It then picks up the cluster node to store the value, based on the key. While storing the value, the client passes back the known version. The request flow is shown in Figure 18.1 . There are two servers named blue and green. For the key "name" , blue is the primary server.
Figure 18.1 Version counter for the primary node is incremented when value is stored.
In the leaderless replication scheme, the client or a coordinator node picks up the node to write data based on the key. The version vector is updated based on the primary cluster node that the key maps to. A value with the same version vector is copied on the other cluster nodes for replication. If the cluster node mapping to the key is not available, the next node is chosen. The version vector is only incremented for the first cluster node the value is saved to. All the other nodes save a copy of the data. The code for incrementing version vector in databases like Voldemort looks like this:
Click here to view code image
class ClusterClient...
public void put(String key, String value, VersionVector existingVersion) {
List<Integer> allReplicas = findReplicas(key);
int nodeIndex = 0;
List<Exception> failures = new ArrayList<>();
VersionedValue valueWrittenToPrimary = null;
for (; nodeIndex < allReplicas.size(); nodeIndex++) {
try {
ClusterNode node = clusterNodes.get(nodeIndex);
//the node which is the primary holder of the key value is
// responsible for incrementing version number.
valueWrittenToPrimary = node.putAsPrimary(key,
value, existingVersion);
break;
} catch (Exception e) {
//if there is exception writing the value to the node,
// try other replica.
failures.add(e);
}
}
if (valueWrittenToPrimary == null) {
throw new NotEnoughNodesAvailable("No node succeeded " +
"in writing the value.", failures);
}
//Succeeded in writing the first node, copy the same to other nodes.
nodeIndex++;
for (; nodeIndex < allReplicas.size(); nodeIndex++) {
ClusterNode node = clusterNodes.get(nodeIndex);
node.put(key, valueWrittenToPrimary);
}
}
The node acting as a primary is the one which increments the version number.
Click here to view code image
public VersionedValue putAsPrimary(String key, String value, VersionVector existingVersion) {
VersionVector newVersion = existingVersion.increment(nodeId);
VersionedValue versionedValue = new VersionedValue(value, newVersion);
put(key, versionedValue);
return versionedValue;
}
public void put(String key, VersionedValue value) {
versionVectorKvStore.put(key, value);
}
As can be seen in the above code, it is possible for different clients to update the same key on different nodes, for instance when a client cannot reach a specific node. This creates a situation where different nodes have different values which are considered concurrent according to their version vector.
As shown in Figure 18.2 , both Client1 and Client2 are trying to write to the key "name" . If Client1 cannot write to server green, the green server will be missing the value written by Client1. When Client2 tries to write, but fails to connect to server blue, it will write on server green. The version vector for the key "name" will reflect that the servers blue and green have concurrent writes.
Figure 18.2 Concurrent updates on different replicas
Therefore, when the versions are considered concurrent, the version-vector-based storage keeps multiple versions for any key.
Click here to view code image
class VersionVectorKVStore...
public void put(String key, VersionedValue newValue) {
List<VersionedValue> existingValues = kv.get(key);
if (existingValues == null) {
existingValues = new ArrayList<>();
}
rejectIfOldWrite(key, newValue, existingValues);
List<VersionedValue> newValues = merge(newValue, existingValues);
kv.put(key, newValues);
}
//If the newValue is older than the existing one, reject it.
private void rejectIfOldWrite(String key,
VersionedValue newValue,
List<VersionedValue> existingValues) {
for (VersionedValue existingValue : existingValues) {
if (existingValue.descendsVersion(newValue)) {
throw new ObsoleteVersionException(
"Obsolete version for key '" + key
+ "': " + newValue.versionVector);
}
}
}
//Merge new value with existing values. Remove values with
// lower version than the newValue. If the old value is neither
// before or after (concurrent) with the newValue. It will be preserved
private List<VersionedValue> merge(VersionedValue newValue,
List<VersionedValue> existingValues) {
var retainedValues = removeOlderVersions(newValue, existingValues);
retainedValues.add(newValue);
return retainedValues;
}
private List<VersionedValue> removeOlderVersions(VersionedValue newValue,
List<VersionedValue> existingValues) {
//keep versions which are not directly dominated by newValue.
return existingValues
.stream()
.filter(v -> !newValue.descendsVersion(v))
.collect(Collectors.toList());
}
If concurrent values are detected while reading from multiple nodes, an error is thrown, allowing the client to do possible conflict resolution.
Resolving Conflicts
If multiple versions are returned from different replicas, version vector comparison allows the latest value to be detected.
Click here to view code image
class ClusterClient...
public List<VersionedValue> get(String key) {
List<Integer> allReplicas = findReplicas(key);
List<VersionedValue> allValues = new ArrayList<>();
for (Integer index : allReplicas) {
ClusterNode clusterNode = clusterNodes.get(index);
List<VersionedValue> nodeVersions = clusterNode.get(key);
allValues.addAll(nodeVersions);
}
return latestValuesAcrossReplicas(allValues);
}
private List<VersionedValue>
latestValuesAcrossReplicas(List<VersionedValue> allValues) {
var uniqueValues = removeDuplicates(allValues);
return retainOnlyLatestValues(uniqueValues);
}
private List<VersionedValue>
retainOnlyLatestValues(List<VersionedValue> versionedValues) {
for (int i = 0; i < versionedValues.size(); i++) {
var v1 = versionedValues.get(i);
versionedValues.removeAll(getPredecessors(v1, versionedValues));
}
return versionedValues;
}
private List<VersionedValue> getPredecessors(VersionedValue v1,
List<VersionedValue> versionedValues) {
var predecessors = new ArrayList<VersionedValue>();
for (VersionedValue v2 : versionedValues) {
if (!v1.sameVersion(v2) && v1.descendsVersion(v2)) {
predecessors.add(v2);
}
}
return predecessors;
}
private List<VersionedValue>
removeDuplicates(List<VersionedValue> allValues) {
return allValues
.stream()
.distinct()
.collect(Collectors.toList());
}
Just doing conflict resolution based on version vectors is not enough when there are concurrent updates. So it’s important to allow clients to provide application-specific conflict resolvers. A conflict resolver can be provided by the client while reading a value.
Click here to view code image
public interface ConflictResolver {
VersionedValue resolve(List<VersionedValue> values);
}
class ClusterClient...
public VersionedValue getResolvedValue(String key, ConflictResolver resolver) {
List<VersionedValue> versionedValues = get(key);
return resolver.resolve(versionedValues);
}
For example, Riak allows applications to provide conflict resolvers. 55
-
[https://docs.riak.com/riak/kv/latest/developing/usage/conflict-resolution/java/index.html](https://docs.riak.com/riak/kv/latest/developing/usage/conflict-resolution/java/index.html)
Last Write Wins (LWW) Conflict Resolution
Cassandra and LWW
Apache Cassandra, while architecturally the same as Riak or Voldemort, does not use version vectors at all, and supports only the last-write-wins conflict resolution strategy. Cassandra, being a column-family database rather than a simple key-value store, stores a timestamp with each column, as opposed to a value as a whole. While this takes the burden of doing conflict resolution away from the users, you need to make sure that the NTP service is configured and working correctly across Cassandra nodes. In the worst case scenario, some latest values can get overwritten by older values because of clock drift.
While version vectors allow detection of concurrent writes across a set of servers, by themselves they do not provide any help to clients in figuring out which value to choose in case of conflicts. The burden is on the client to do the resolution. Sometimes, clients want the key-value store to do conflict resolution based on timestamps. While there are known issues with timestamps across servers, the simplicity of this approach makes it a good choice for clients, even with the risk of losing some updates because of clock drift. This relies on time services like NTP to be configured and working across the cluster. Databases like Riak and Voldemort allow users to select the last-write-wins conflict resolution strategy.
To support LWW conflict resolution, a timestamp is stored with each value while its written.
Click here to view code image
class TimestampedVersionedValue...
class TimestampedVersionedValue {
String value;
VersionVector versionVector;
long timestamp;
public TimestampedVersionedValue(String value, VersionVector versionVector,
long timestamp) {
this.value = value;
this.versionVector = versionVector;
this.timestamp = timestamp;
}
When reading the value, the client can use the timestamp to pick up the latest value. The version vector is completely ignored in this case.
Click here to view code image
class ClusterClient...
public Optional<TimestampedVersionedValue>
getWithLWW(List<TimestampedVersionedValue> values) {
return values.stream().max(Comparator.comparingLong(v -> v.timestamp));
}
Read Repair
Allowing any cluster node to accept write requests improves availability. However, it’s important that eventually all of the replicas have the same data. One of the common methods to repair replicas is applied when the client reads the data.
When conflicts are resolved, it’s also possible to detect which nodes have older versions. The nodes with older versions can be sent the latest versions as part of the read request handling from the client. This is called read repair.
Consider a scenario shown in Figure 18.3 . Two nodes, blue and green, have values for a key "name" . The green node has the latest version with version vector [blue: 1, green: 1] . When the values are read from both replicas, blue and green, they are compared to find out which node is missing the latest version, and a put request with the latest version is sent to that cluster node.
Figure 18.3 Read repair
Allowing Concurrent Updates on the Same Cluster Node
There is a possibility of two clients writing concurrently to the same node. In the default implementation shown above, the second write will be rejected. The basic implementation with the version number per cluster node is not enough in this case.
Consider the following scenario. With two clients trying to update the same key, the second client will get an exception, as the version it passes in its put request is stale ( Figure 18.4 ).
Figure 18.4 Two clients concurrently updating the same key with server ID
A database like Riak gives flexibility to clients who can allow this kind of concurrent writes without getting error responses.
Using Client IDs Instead of Server IDs
If each cluster client can have a unique ID, client ID can be used. A version number is stored per client ID. Every time a client writes a value, it first reads the existing version, increments the number associated with the client ID, and writes it to the server.
Click here to view code image
class ClusterClient...
private VersionedValue putWithClientId(String clientId,
int nodeIndex,
String key,
String value,
VersionVector version) {
var node = clusterNodes.get(nodeIndex);
var newVersion = version.increment(clientId);
var versionedValue = new VersionedValue(value, newVersion);
node.put(key, versionedValue);
return versionedValue;
}
Since each client increments its own counter, concurrent writes create sibling values on the servers, but concurrent writes never fail.
The scenario discussed in the previous section, which gives error to the second client, works as shown in Figure 18.5 .
Figure 18.5 Two clients concurrently updating the same key with client ID
Dotted Version Vectors
One of the major problems with client-ID-based version vectors is that the size of the version vector directly depends on the number of clients. This causes cluster nodes to accumulate too many concurrent values for a given key over time. The problem is called sibling explosion. 56 To solve this issue and still allow cluster-node-based version vectors, Riak uses a variant of version vector called dotted version vector. 57
-
[https://docs.riak.com/riak/kv/2.2.3/learn/concepts/causal-context/index.html#sibling-explosion](https://docs.riak.com/riak/kv/2.2.3/learn/concepts/causal-context/index.html#sibling-explosion) -
[https://riak.com/posts/technical/vector-clocks-revisited-part-2-dotted-version-vectors/index.html](https://riak.com/posts/technical/vector-clocks-revisited-part-2-dotted-version-vectors/index.html)
Examples
-
Voldemort uses version vector as described here. It allows timestamp-based last-write-wins conflict resolution.
-
Riak started by using client-ID-based version vectors, but moved to cluster-node-based version vectors and eventually to dotted version vectors. Riak also supports last-write-wins conflict resolution based on the system timestamp.
-
Apache Cassandra does not use version vectors. It supports only last-write-wins conflict resolution based on system timestamp.