kusumoto_teruya
kusumoto_teruya

Reputation: 2475

Performance of Cloud Functions for Firebase with outbound networking

When I run the following code in Cloud Functions, it takes more than 2 seconds.
When I run it locally, it takes about 600 milliseconds.
What are the possible causes?

import * as functions from 'firebase-functions'
import axios from 'axios'

const headers = { 'accept': 'application/json', 'x-access-key': '...', 'x-access-secret': '...' }

exports.functionName = functions.https.onRequest(async (req, res) => {
  try {
    console.log('request 1 start')
    const response1 = await axios.get(`https://api.sample.com/users/${req.body.userId}`, { headers })
    console.log('request 1 completed')
    const response2 = await axios.post(`https://api.sample.com/contents1/${response1.data.id}`, {}, { headers })
    console.log('request 2 completed')
    const response3 = await axios.post(`https://api.sample.com/contents2/${response2.data.id}`, {}, { headers })
    console.log('request 3 completed')
    res.send(response3)
  } catch (error) {
    res.send(error)
  }
})

Metrics

In Cloud, each asynchronous request (axios.get/post) is taking up to almost 1 second. enter image description here

Hypothesis

What I tried

I think I tried all the methods described in the official Firebase documentation.

const httpAgent = new http.Agent({ keepAlive: true })
await axios.get(`https://api.sample.com/users/${req.body.userId}`, { headers, httpAgent })

Upvotes: 2

Views: 463

Answers (1)

pagep
pagep

Reputation: 3629

Important thing to consider:

  • Where is your API located?
  • Where are you located?
  • Where are the functions located?

They could be deployed on the other side of the planet than you and your API is. You can control where you want your functions deployed by specifying the datacenters. https://firebase.google.com/docs/functions/locations

All our functions code looks like this:

const DEFAULT_FUNCTIONS_LOCATION = "us-east4";
const runtimeOpts: Record<string, "512MB" | any> = {
  timeoutSeconds: 120,
  memory: "512MB",
};

const getCustomAnalysis = functions
  .region(DEFAULT_FUNCTIONS_LOCATION)
  .runWith(runtimeOpts)

You can have functions deployed in different datacenters / in one project. Btw you will need to specify this when you call them.

We do have this in one of our projects. The whole project is deployed in EU (Because of legal). One function is in US - calling US API. Once the data are in the GCP function and they travel to another GCP datacenter, they utilize Google premium tier networking. But if you are not constrained, just deploy your whole project closes to your users / your API.

Also is your GCP project clean? You don't utilize any VPC networking? Networking can be messy, they could be also "issue" between the GCP datacenter and your API datacenter.

As a tip for more testing: Try out different urls and measure the speed. But I don't think this is general CF issues.


Btw I have run into similar issue with retrieving larger amount of data from Firestore. However we noticed difference when we speed up functions (using more Memory give you more MHz).

Upvotes: 1

Related Questions