I have implemented several counters in my project using the new increment
API. There are two functions whose implementation is a simple one line:
exports.MY_FUNC = functions.firestore.document('/PATH_TO_DOC/{id}').onCreate((snapshot, context) => {
return admin.firestore().collection('COUNTERS').doc('DOC').update({COUNTER: admin.firestore.FieldValue.increment(1)});
});
(everything in CAPS is a generic replacement for the actual values which I have).
This function works perfectly well. However, each day, there are instances when it fails with "Error: memory limit exceeded. Function invocation was interrupted." It puzzles me as it works most of the time correctly.
I do know how to increase the memory limit, and I am going to increase it to 512MB right now. However, I don't like the unpredictability of this setup. Undoubtedly, the same function operating a counter on the same doc should either work correctly with 256MB or should fail every time.
I am also quite stunned that an operation which fits into one line may require more than 256MB. I have other functions which perform 3-4 different steps (reads, writes) and they work correctly with 256MB.
Given the unpredictability, I now worry that 512MB may also not be enough. Unfortunately, it doesn't seem that docs mention the memory requirements for increment
API, so it looks like my only option is to deploy and see if the new memory limit is sufficient.
I would appreciate if anyone had any suggestions on any points below:
Is there a way to enforce consistent behaviour? I want the function to either fail every time if 256MB is not enough or succeed every time if it is enough.
How can developers estimate in advance the memory requirement of a particular function? I honestly do not think that it is OK to have a working function with 256MB during debugging only to find out that it may exceed memory limit in production.
Thanks for any input.