I have distributed servers, how do I write a key if not exists atomically?

My requirement is In a N node distributed cloud I want to first query value for a given key (say any IP ADDRESS) Then If the key is absent or the value is NULL, I want to set some #MAGIC value (say 255.255.255.255) as the value So that other node does do not anything, if it gets the value as #MAGIC

Then the node which has just now set the value as #MAGIC, will own the real expensive operation (like traceroute to the IP), will get the actual value and replace #MAGIC with the actual value

If there are any other nodes which are working on the same key, the nodes will either get NULL or #MAGIC or real_data as the value and wont actually duplicate the real expensive operation like traceroute

– Is there any aerospike C API which will get and set atomatic, so that any other node (client lib) does not ‘explore the value for the same key’

thanks -Vikrant

If the read finds that the record doesn’t exist, the app can write using the create-only write-policy which will fail if the record already exists. If the read found the record, then the app can use the generation from the read in the write’s gen-equals policy, which will fail if the generation has move on since the read. This basically describes the typical read-modify-write pattern.

The read-modify-write pattern requires a minimum of two network round trips. This could be shortened to one round trip using either write-expressions or UDFs. For this purpose, I’d suggest looking into expressions first.

1 Like

Thanks KPorter, this info is what was looking for.