In our previous example, we checked how a simple CRDT with optimized conflict resolution can work in theory. Now, let's apply this knowledge to a more advanced scenario.
Consider you're a senior engineer in a global tech company, like Netflix or YouTube, and you have operations that need to be executed on different servers located all around the world. In this case, movies or videos uploaded on one server need to be replicated on the rest, and users should view and interact with the same content despite their physical location.
To achieve that, we can leverage CRDTs optimized conflict resolution techniques. Upon receiving an update, a typical CRDT would apply that update, then introduce a mechanism to replicate it to other servers. If two updates happen on two different servers almost simultaneously, that's when our optimized conflict resolution kicks in! By maintaining some metadata, such as version vectors, the CRDT can merge the two updates effectively and keep the system in a consistent state.
The following Python code demonstrates a simplified version of that process where we're representing our servers as the elements of a list. Observing the output, you'll notice the mechanism keeps the whole system in an eventual consistent state.
xxxxxxxxxx
if __name__ == "__main__":
# This list represents our various servers
servers = [
{'data': {}, 'vector': [0, 0, 0]},
{'data': {}, 'vector': [0, 0, 0]},
{'data': {}, 'vector': [0, 0, 0]}
]
for i in range(len(servers)):
servers[i]['data'][f'video_{i+1}'] = 'Available'
servers[i]['vector'][i] += 1
print('Before conflict resolution:')
for i, server in enumerate(servers):
print(f'Server {i+1} state: {server}')
print('After conflict resolution:')
# Merging Function
for i in range(len(servers)):
for j in range(len(servers)):
if i != j:
servers[i]['vector'][j] = max(servers[i]['vector'][j], servers[j]['vector'][j])
servers[i]['data'].update(servers[j]['data'])
for i, server in enumerate(servers):
print(f'Server {i+1} state: {server}')