What happens when all servers simultaneously tries to write data on the same key at the same time?

I want to make a system that allows for every 6 day a certain global variable for the entire experience to change, but i realized that making such a system would require all the servers to modify one data from a singular profile store, isn’t that unconvenient to have like 1000 servers trying to write the same data at the same time how does the engine allow it to happen ?

1 Like

Honestly, just try. Get a friend/alt and yourself, two separate servers, schedule your event 1 minute in the future, and see what happens.

That aside, what exactly are you trying to change? Surely there are better architectural choices you could make.

I’d suggest instead of storing the value in datastore, you can check the date in runtime using os.date(“!*t”) to get current date in UTC time

let me explain what i want to do fundamently, basically i want to have some sort of election system that allows every player every week to vote for a special candidate to rule over a city for 1 week irl, when the election is about to end and the code needs to stock the result, however when the elections is about to end a lot of players (i hope xd) are gonna be present and a lot of servers are going to be open, if they all are going to execute the code thats going to rewrite the exact same data thats supposed to represent which mayor is in activity at the exact same time whats going to happen ??? I think its more ergonomical if it happens one time from one server, but idk how to do it

^
|‎
here is a better explanation of what im trying to do

In this case, I’d suggest using IncrementAsync() as this should automatically handle multiple servers trying to read/write a datastore.

1 Like

easily.

There are two methods:

  1. SetAsync, with this method it’s simple: last writer “wins”
  2. UpdateAsync, this is what you need for your case, please read documentation, it’s well explained.

documentation explains it.

if you use SetAsync, the server that called the function latest, wins.
if you use UpdateAsync, the server to first call this function, locks every other server from calling this UpdateAsync function until the data has been updated

thank you, im gonna use the profile store module, i just wanted to see if someone had the “right awnser” because technically theres many possibilites, theres message service, params etc…

1 Like

I would recommend using MemoryStore (specifically a HashMap) for this, as it can handle high frequency updates to data. If you prefer to continue using ProfileStore, it uses UpdateAsync under the hood already.