Understanding Cloud Functions: Dealing with parallel execution (Firecasts)
Why Stack Overflow recommends it
Stack Overflow users link it when answering questions about Cloud Functions scaling, concurrent requests and race conditions with fast database updates.
Recommended by 2 people across 2 different months, 2019 to 2021.
What it is
A short Firebase Firecasts episode explaining how Cloud Functions for Firebase can run in parallel across multiple server instances. It follows an episode on global memory and temp disk reuse, and shows how to write functions that behave safely under concurrent execution. Presented on the official Firebase channel.
What people said
Recommends Harif Velarde · 2019-11 · Stack Overflow · on “Firebase Cloud Function and multiple requests”
I don't think this is related with your code, seems more related how you can manage the Cloud Functions service. Cloud functions will automatically scale up when it determines that more capacity is needed. The instances will be scaled down when they are no longer needed. I recommend you take a look at this document where you can find useful information about how to use Pub/Sub triggers on Cloud Functions, Pub/Sub is a globally distributed message bus that automatically scales as you need it [1]. Also I suggest you check these videos where is explained how works the function scaling [2] and how to deal with parallel execution [3]. [1] [LINK] [2] [LINK] [3] [this video]
Recommends RJC · 2021-11 · Stack Overflow · on “How to force test a Firestore parallel Transaction?”
According to this documentation [LINK] , you can implement parallel reads as you migrate traffic to a new collection, read from the old collection first. You can also watch this video, to understand cloud functions in dealing with parallel executions [this video] .
Cites Dharmaraj · 2021-06 · Stack Overflow · on “Firebase cloud functions not catching with fast database updates”
Cloud functions do not guarantee order of execution. There is a Firecast [this video] explaining parallel execution. You can take a look at transactions [LINK] but your use case doesn't seem to be straightforward as incrementing or decrementing a value. Cloud functions (any serverless functions) may not be the best choice for all the cases. You will have to make sure the user is deleted from the node immediately once is player is matched. Using transactions you can match and remove 2 players immediately. So when a third user who was about to match as well will be added to queue instead of in the queue. You may need queues running on your server (if the reads and writes are so high speed) or refac