Dependent UpdateAsyncs aka Datastore failure atomicity

As a Roblox developer, it is currently impossible to safely update two data store keys simultaneously. This prohibits creating trading systems in a safe and sane way. Let’s look at a swap routine with current data stores

function SwapKeys(DS,A,B)
    local a, b = DS:GetAsync(A), DS:GetAsync(B)
    DS:UpdateAsync(A, function()
        return b
    end)
    DS:UpdateAsync(B, function()
        return a
    end)
end

Either of these UpdateAsyncs may fail. Additionally both a and b must be known before either of these UpdateAsyncs are run, and the function may not block so you can’t UpdateAsync inside another. If either of these UpdateAsyncs fail one would hope that no change is made to either key A or B. One could “solve” the problem by adding a rollback catch to our SwapKeys routine, however all you end up with is even more dependent updates which can themselves fail. The fundamental issue is that there are four transactions occuring here (Get, Get, Update, Updatel, when only one is desired (Updates). If this one transaction fails no change should happen to the underlying datastore. A method such as the following would solve this issue:

GlobalDataStore:UpdatesAync(KeyList, Mutators...)

Where
KeyList is a list of strings
and
Mutators is a tuple of functions, ordered by the key list, who accept the initial values of those keys as parameters, again ordered by the key list, returning the new value for their corresponding key. If any of these functions error then no change should be made to the underlying datastore. This should be seen as a single atomic operation which succeeds completely or not at all.

function SwapKeys(DS, A, B)
    DS:UpdatesAsync(
        {A, B},
        function(a,b)
            return b;
        end,
        function(a,b)
            return a;
        end
    )
end

If Roblox is able to address this issue, it would improve my game because it would allow for safe, sane transactions between multiple keys such as a trading system or in game economy without the necessity of an external database.

13 Likes