Lock two request concurrently

Hi Admin,
Could I lock transaction when multi request call in cloud funtion in the same time. Like this: https://docs.mongodb.com/manual/faq/concurrency/

User 1 (request 1) -> read request start
User 1 (request 2) -> read request start
User 1 (request 1) -> read request success (withdraw available)
User 1 (request 2) -> read request success (withdraw available)
User 1 (request 1) -> write a new record to the db (withdraw success)
User 1 (request 2) -> write a new record to the db (withdraw fail because request 1 done ).

Thank you,

You mean that you want some kind of muted or it is something else?

E.x:
User A has 300 money which stored in mongoDB ( moralis ) => I create a cloud function withdraw => User A sent 2 request in same time for withdrawing his money, each request wants get 300 money => so if two requests sent in same time User A can take 600 money not 300.

Ok, it looks like you want a mutex so what only one request succeeds

yes, i just want only one request succeeds. So how can I do that ?
Thank you

On upgraded sever instances there is a redis that can be accessed and used for that, there isn’t documentation about how to use it.

So now I can not know exactly how to do it on Moralis, is it right ?

client = redis.createClient({ host: "moralis-redis", port: 6379 })

but this works only on upgraded instances, not also on free instances

1 Like

thank you @cryptokid