Managing Instance Resources with Cloud Functions for Firebase (Firecasts)

Firebase · 28K views · 2019 · 4:47

Open on YouTube

Cloud, DevOps & reliabilityWeb developmentexplainer

Why Stack Overflow recommends it

Stack Overflow users link it when answering questions about /tmp usage, memory-limit errors, and whether global state or temp files persist between Cloud Function invocations.

Recommended by 4 people across 5 different months, 2019 to 2023.

What it is

A short Firecasts episode from the Firebase channel in which Doug Stevenson (@codingdoug) explains how to manage instance resources in Cloud Functions for Firebase. It covers how /tmp is backed by memory, why files written there should be cleaned up, and how global variables persist across invocations on the same instance.

What people said

Recommends Doug Stevenson · 2019-02 · Stack Overflow · on “Creating Folders in the tmp Directory in Firebase Cloud Functions”

Cloud Functions only allows one function to run at a time in a particular server instances. Functions running in parallel run on different server instances, which have different /tmp spaces. Each function invocation runs in complete isolation from each other. You should always clean up files you write in /tmp so they don't accumulate and cause a server instance to run out of memory over time. You may find it helpful to watch this video about function isolation: [LINK] And this video about managing resources, especially /tmp: [this video]

Recommends Dennis Alund · 2019-03 · Stack Overflow · on “Function killed. Error: memory limit exceeded [Thumbnail example]”

Apart from increasing your memory limit on the cloud function, you should also delete temporary files before your cloud function finishes. Please watch this recent official Firebase video on the subject: [this video] Files that are written to the temp file location is counted towards the memory usage of your cloud function. The temporary files are not automatically cleaned up after a function finishes. The files will remain for as long as the server instance is around. So a cloud function that is frequently triggered can cause your server instance to run out of memory regardless of how much memory you allocated in your configuration. At the top of your file, add a requirement for node filesystem

Recommends abraham · 2019-03 · Stack Overflow · on “504 Time-out when deploying node.js app to Firebase”

Cloud Functions have a read-only file system [LINK] except for /tmp which is backed by the memory of your function. 1:30 of this Firecast [this video] has a good example of writing to /tmp . import * as os from 'os'; const tmp = os.tmpdir(); If you want to persist files you should look at using Cloud Storage [LINK] .

Recommends Doug Stevenson · 2019-07 · Stack Overflow · on “Is file system shared between cloud function instances?”

Nothing is shared between server instances allocated for Cloud Functions. However, server instances will be reused between function invocations that happen to hit the same instance. This implies that you should clean up memory and disk space before a function terminates, else the instance could run out of memory before it's eventually deallocated. Watch my video [this video] on exactly this topic for more discussion.

Recommends Doug Stevenson · 2019-07 · Stack Overflow · on “Where to place initialisation code in Firebase cloud functions?”

No, that's not what it means. You should watch this video series to understand how Cloud Functions and other serverless backends work: [this video]

Recommends Doug Stevenson · 2019-12 · Stack Overflow · on “Can global scoped variables be modified by other Cloud Function invocations?”

The other answer here by marian.vladoi is not entirely correct. Global variables WILL be retained and reused between multiple invocations of the same function on the same server instance . However, functions could be running on multiple server instances, so those global variables are not shared between the server instances. It's perfect valid to use global variables as a way to cache or retain data for future invocations, but you don't want to use them to store application state, as there is no guarantee how long they will retain their values. You might want to watch this video in a series that discusses how Cloud Functions works. [this video]

Recommends Rohit Kharche · 2023-07 · Stack Overflow · on “How to lock a google cloud function concurrency?”

Can you explain more what you are trying to do where you stuck as per guild lines [LINK] If you are asking about sharing the same memory then your functions can use shared memory between invocations but that memory could also get saturated if you don't clean off the global memory for all functions you can get more info in this video [this video] which also recommends when and where to use catching.

Cites cbdeveloper · 2019-03 · Stack Overflow · on “Trying to do fs.writefile() inside an async cloud function (node v8)”

I have managed to do it. According to this Firebase youtube video [this video] , the os.tmpdir() only one directory that is writable by your function's code. So did the following and now it works: index.js const fs = require('fs'); const os = require('os'); const path = require('path'); const tmpdir = os.tmpdir(); const filePath = path.join(tmpdir,'response.html'); // PROMISIFY WRITEFILE TO SAVE URL RESPONSE const promisifiedWriteFile = function(text) { return new Promise((resolve, reject) => { fs.writeFile(filePath, text, err => { if (err) { return reject(err); } return resolve(); }); }); }; And from inside the function I'm calling: await promisifiedWriteFile(response.body); Found some related d

Cites chevian · 2020-04 · Stack Overflow · on “Firebase Cloud Functions: Error 500 despite successful results”

It is difficult to suggest a proper fix from my standpoint. Maybe somebody from firebase will come along and shed better insight. In the meantime though, you can use a workaround by reconfiguring the schedule interval and storing scraped data to a container or temp storage until next successful fetch. You can then pipe that buffer as a backup from your catch block. Checkout managing firebase resources [this video] to make sure you don't run out of disk storage, given you're scraping hundreds of pages.

Cites Renaud Tarnec · 2020-09 · Stack Overflow · on “is it necessary to free memory in Firebase Cloud Function”

Yes, you do need to delete the temporary files created in the temporary directory (which is an in-memory filesystem), because "files that you write consume memory available to your function, and sometimes persist between invocations". There is a specific doc section and a video about that: [LINK] and [this video]