Datastore race condition problem

I am attempting to create a system where there is an ordered, global array of information that is shared by each server and updated semi-regularly. This array is currently stored in a datastore, and each server periodically appends data to the array. Here’s a potential problem I see happening:

  • Server 1 reads the array {“item1”,“item2”,“item3”} and caches it.
  • Server 2 reads the array {“item1”,“item2”,“item3”} and caches it.
  • Server 1 appends an item to the array and writes it to the datastore. The array is now {“item1”,“item2”,“item3”,“Foo”}
  • Server 2 appends an item to the array and writes it to the datastore. The array is now {“item1”,“item2”,“item3”,“Baz”}

Since Server 2 did not see that Server 1 had appended something to the array, server 1’s update was overwritten, resulting in data loss. Is this a real problem or am I just overthinking this? I have a few possible solutions in mind but none of them seem very elegant.

1 Like

This is a real issue, and I don’t think Roblox has anything in store to fight it. As far as I know, you just have to hope that it sees the new data.

This is the exact problem that UpdateAsync solves. Your callback receives the latest version in the datastore, then you manipulate as desired, and the API ensures that the underlying has not changed since fetching it. If it did change, it runs the callback again.

5 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.