question

odistagon avatar image
odistagon asked

Racing conditions in cloud script

Hi,


I am planning to have daily reward on our games and have a concern to implement it on cloudscript.

I found an article about daily reward:
https://community.playfab.com/hc/en-us/community/posts/205460028-Cloudscript-for-daily-reward

In this case, I suspect there's a chance having racing condition issue. (between checking the timestamp in UserInternalData and update it)

So my question is:
- does cloudscript limit number of threads to one per a user? (or a game?)
- if no, is there a way to make the operation atomic? or CAS operation or user lock?

I did some search on Playfab documents but cannot find those functions.
What is the best practice to make it safe?

(I came up an idea to use item count as a lock object but I thought it is a bit too expensive for the use)

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
brendan avatar image
brendan answered

You are correct - a user who manages to send two of the exact same call to ExecuteCloudScript at the same time, and assuming that those calls reach the service within a a very narrow window (milliseconds), could have two calls run simultaneously such that both compare the UserInternalData before each updates it.

Locking is not really the way to manage this, as it can cause problems at scale. We will be providing a compare-and-swap (CAS) operation later this year, in order to provide for this type of safety mechanism in Cloud Scripts (as well as Client sourcing calls in general).

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.