I’m currently using Google Cloud Platform’s 2nd generation Cloud Functions to handle some processing tasks. My function needs to call several external APIs, and I’m noticing significant performance issues with these calls.
Environment:
- Platform: GCP Cloud Functions (2nd Gen)
- Runtime: Node.js 20
- Resource Allocation: Tried increasing memory and CPU, but no improvement
- External API Call Duration: Ranges from 100 ms to 3000 ms; occasionally takes 20-30 seconds
Problem:
I expected API calls to be fast and stable given that GCP is a data center, but I’m encountering highly variable response times. Here’s a brief overview of the setup:
-
Network Latency:
- Calls to a public API have very inconsistent response times, from milliseconds to several seconds.
-
Code Structure:
- The script itself is straightforward and doesn’t involve complex logic or blocking operations that could contribute to the delay.
-
Resource Management:
- I’ve tried increasing memory and CPU allocation, but this didn’t resolve the latency issues.
-
Observations:
- No consistent pattern in the latency spikes; they occur seemingly at random intervals.
Example Code:
Here’s a simplified version of the code I’m using to make the API calls:
export const runTest = async (req: Request, res: Response) => {
res.status(200).send({})
const scheduledTask = req.body as ScheduledTask
const runner = new ScheduledTaskRunner()
await runner.run(scheduledTask)
for (let i = 0; i < 4; i++) {
await pingWithAxios()
await pingWithFetch()
await pingWithKy()
}
console.log('done')
}
export async function pingWithFetch() {
const start = Date.now()
await fetch('https://api-prod.reapi.com/ping').then((response) => {
const end = Date.now()
console.log('fetch ping response:', response.status, 'time:', end - start, 'ms')
})
}
export async function pingWithKy() {
const start = Date.now()
await ky.get('https://api-prod.reapi.com/ping').then((response) => {
const end = Date.now()
console.log('ky ping response:', response.status, 'time:', end - start, 'ms')
})
}
export async function pingWithAxios() {
const start = Date.now()
await axios.get('https://api-prod.reapi.com/ping').then((response) => {
const end = Date.now()
console.log('axios ping response:', response.status, 'time:', end - start, 'ms')
})
}
Screenshot:
Questions:
- Network Configuration: Could there be any network configuration settings within GCP that might cause such variability?
- Best Practices: Are there any best practices for optimizing external API calls from GCP Cloud Functions?
Any insights or suggestions on how to diagnose and improve the performance of these external API calls would be greatly appreciated.
Thank you for your help!