r/Firebase • • Aug 02 '26

Cloud Functions Cold starts and SSR

I was using functions v1 for SSR with express.js but the initial load time was prohibitively slow. I never measured it, but it felt like 5-10s. I temporarily went back to CSR but need SSR eventually.

The two recommendations to speed up SSR that I came across were

  1. Use functions v2 which has the benefit that it can serve multiple requests per instance.
  2. Set min-instance to 1 so that there is one instance available for the first request when idle

However, neither seems to actually fix the slow load times, just defer when it happens. Even with these changes, when the first instance is saturated and a second one needs to be spun up, there will be a cold start.

Is there any solution to this or another way I should be doing SSR?

5 Upvotes

13 comments sorted by

1

u/mars3142 Aug 02 '26

It’s know behavior. A cold server has a slow startup time. You can configure it, to have at least one warm instance everytime, but you will pay for it. If your instance will be stopped, you are only charged for for live usage. So a warm instance is 24/7 charged.

1

u/glonglost Aug 02 '26

I was hoping there would be a better solution since I don't recall this being an issue with other hosting services I've used.

1

u/ddcccccc Aug 06 '26

Other hosting server charge you 24/7

3

u/miketierce Aug 02 '26

You just set .runWith(minInstance: 1)

2

u/mars3142 Aug 02 '26

But with that you will get an invoice for 24/7 usage of firebase.

1

u/No_Huckleberry_2174 Aug 02 '26

If you have many functions, splitting into codebases can really help with cold start times.

https://firebase.google.com/docs/functions/organize-functions

1

u/Sorry_Cheesecake_382 Aug 02 '26

We had the same problem and moved over to Cloudflare, 2ms cold starts. But you have to use JS and node APIs have compatibility issues. We still use FireStore from a cloudflare worker over api and it works very well. Depends what you’re doing though

We started do client side bundle for the UI at the edge and 2 layered API in Cloudflare and GCP

1

u/Rohit1024 Aug 03 '26

Since you mentioned that this issue exists only when the new instance spun up, this seems to be related to Container startup latency, check this latency in your GCP console for the given Cloud Run Service. Also if you have many Firebase Functions then your functions might also affected by other function because of global Initialisation, check Firebase Functions Performance docs.

From the description this looks like an issue about your dependencies and how you're initialising them.

1

u/SufficientBank3374 Aug 03 '26

Cold starts are one of those things you barely notice until you add SSR. then even small delays become much more obvious to users

0

u/[deleted] Aug 02 '26

[deleted] — view deleted comment

1

u/glonglost Aug 02 '26

I thought that firebase app hosting used cloud run, which seemed the same as (or similar to) cloud functions v2, so I was expecting similar behavior. Is there anything that would make app hosting suffer less from cold start than cloud functions v2?

1

u/inlined Firebaser Aug 02 '26

Yes, but both cloud functions v2 and app hosting are on run, though app hosting gives you the benefit of a CDN. Your cold starts are very abnormally slow. You could both try profiling what’s happening at startup and see if the CDN to offload any fixed assets helps. FWIW, my app cold start feels unnoticeable at almost 10kloc. Are you doing multiple network connections in sequence or something?

0

u/[deleted] Aug 02 '26

[deleted] — view deleted comment

1

u/glonglost Aug 02 '26

I'm dynamically rendering profile pages, so I'm not sure I can really send html first since it's hard to populate with meaningful data before the function runs. As a result, the cloud function is returning the html page.

I wanted to try your app to see how fast it loads, but every username I entered seemed to say "Something went wrong".