firebase / firebase-tools

The Firebase Command Line Tools
MIT License
4.02k stars 937 forks source link

NextJS - Extreme latency and out of memory cloud functions on Firebase after upgrading next to >13.4.12 #6349

Open heliohm opened 1 year ago

heliohm commented 1 year ago

[REQUIRED] Environment info

firebase-tools: 12.5.2

Platform: MacOS 13.2.1 , MBP Pro M2 Pro,

[REQUIRED] Test case

https://github.com/heliohm/next-firebase-debug-latency

[REQUIRED] Steps to reproduce

[REQUIRED] Expected behavior

NextJS SSR cloud function latency is typically 1-200ms after cold start

[REQUIRED] Actual behavior

heliohm commented 1 year ago

I edited the firebase-tools frameworks/index.js build script and added the Google Cloud Profiler and Trace Agent before deploying again.

image

The tracing is set up according to this guide: https://nextjs.org/docs/app/building-your-application/optimizing/open-telemetry.

Findings are:

  1. Profiling shows that for most of the delay, the cloud function is just sitting idle. image
  2. Heap usage is <10MB worst case image
  3. Tracing shows the API endpoints at <1ms and the whole execution round at seconds as expected image image
  4. Logs show that cloud function runs out of memory almost every call, but not all. image
  5. After increasing memory to 1GiB in firebase.json and re-deploying - latency is just as bad, but out of memory log messages are gone and profiler now only shows Idle for 600ms and nothing more. image
heliohm commented 1 year ago

Addition: GCP tripped the billing budget set up throughout development without any usage beyond a developer. Metrics Explorer show Cloud Run Revision -> Container -> Billable Instance Time going at 16 mins/min (whatever that means) and ramping up almost 10$/day, with zero active instances. Seems something is hanging beyond the user's control here?

image
mrjackyliang commented 1 year ago

Not really a resolution to this issue, but I've also spent hours figuring this out. The cloud function memory is being exhausted, even if I increase it.

It's also annoying to realize that the documentation stated that the latest supported Next.js version is 13.4.7, and it wasn't even shown during deployment.

https://firebase.google.com/docs/hosting/frameworks/nextjs

emarcotte commented 11 months ago

Might be related to https://github.com/FirebaseExtended/firebase-framework-tools/pull/122.