Can Cloud Function executions conflict with each other?

Viewed 26

In my project only Cloud Functions can write to Firestore database, clients never.
Clients only call Cloud Functions like this one:

InflictDamageToEnemy()
{
1."Read Player attack from database"
2."Read Enemy health from database"
3."Enemy health -= Player attack"
4."Write new Enemy health to database"
}

I wonder what happens when multiple users call this cloud function at roughly same time? Will reads and writes conflict with each other? Specifically. Read and Write of Enemy health. Like this:

InflictDamageToEnemy()
{
1."Read Player attack from database"
2."Read Enemy health from database"


Enemy health changed here to a different value as this cloud function was called from different client and executed


3."Enemy health -= Player attack"
4."Write new Enemy health to database"
}

Can this happen? If so. How to fix this? Do I have to use transactions in cloud functions?

Thanks!

1 Answers

In Cloud Functions v1 each function invocation runs on a separate container where no other invocations are running at the same time. So if two functions invocations are running in parallel, they'll be running on separate containers.

That said: for any database interactions, you'll want to use transactions, batched writes, and or security rules to ensure that no conflicts occur on that level.

Related