Search code examples
optimistic-lockingtarantool

Is there a way in Tarantool to check state of tuple before performing update (optimistic lock check / CAS pattern)?


I try to find information about way to implement optimistic locks in Tarantool DB. This case doesn't covered in documentation so I can't get possible way for such action.

My goal is to find a way to resolve potential data collisions for concurent update of same tuples from multipal clients ( application servers ). For such load there is always lag between reading tuple and updating it - so there is room for race conditions. I try to avoid pessimistic locks in distributed system - for such locking we need additional component - and any addition of new component mast be done with many considerations in mind.


Solution

  • That would be a stored procedure similar to the following (code is simplified):

    function update_cas(key, tuple, version)
       local old = space:get(key)
       if old.version ~= version then error('Oops!') end
       tuple[VERSION_FIELD_NUMBER] = version + 1
       space:replace(tuple)
    end
    

    I hope it gives you the idea.