dmuensterer
dmuensterer

Reputation: 1885

Can I use Google CloudFunctions for reliable application purposes?

I remember to have read an article where it was explained that Cloud Functions are not guaranteed to be executed and especially in the right order. I can't find any sources on this anymore. Is this still recent information?

I am aware that the start of a function can take a couple seconds, especially when cold starting the function.

  1. Could I reliably increment a number each time a document is created in a specific Firestore collection without getting my numbers mixed up? I know this is done often but I've never seen information on whether or not it is safe to do.

Following up on question one, are there red flags when using Cloud Functions for payment backend services?

  1. Can I be sure that Cloud Functions are executed in the order that they were triggered i.e. are they queued or executed in parallel?

Upvotes: 1

Views: 181

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317467

Could I reliably increment a number each time a document is created in a specific Firestore collection without getting my numbers mixed up?

You can certainly write code to do that. You will need to keep track of a running count of documents in another document, and use a transaction to keep it up to date.

I don't recommend doing this. It's kind an anti-pattern in Firestore to impose sequentially increasing numbers for documents in a collection. If you want time-based ordering, you should consider using a timestamp instead.

Can I be sure that Cloud Functions are executed in the order that they were triggered i.e. are they queued or executed in parallel?

Cloud Functions provides absolutely no guarantee that functions invocations will happen in any order. They are asynchronous and can execute in parallel on multiple server instances, depending on the load applied to the function.

I strongly suggest reading through the documentation to understand the execution environment provided by Cloud Functions.

Upvotes: 1

Related Questions