I am running an application in .NET 6 as a function. I am having performance issues on the first start. When the application goes online, the first request is slow (up to 5.5s), but any subsequent is faster (100 ms). I narrowed my scope to two things that are slow on startup. Microsoft.Identity and EF Core. My actual .NET startup time is very low, but Microsoft.Identity takes ~2 seconds to respond, and EF Core takes ~2.5 seconds to respond which is in total 80% of the startup time. Is there a way to speed up the Identity and EF Core libraries on a first request?
I have read many articles on "create ping endpoint your server is putting functions to sleep" I do understand that problem, but pinging will only help if I have a traffic of few requests. If I have a spike of 1000 requests, the system will spin up 50 containers to respond to 1000 requests. Thus every 50 containers will have a 5.5s startup time. (this is just an example, I don't have exact number)
Another suggestion is ahead-of-time compilation (AOT), but from what I read online not all libraries support AOT, EF Core for example doesn't.
Additional possibility is to move authorization out of my function, but I am here in kind of a problem, if I move authorization out of function, I am limited on what idp I can use.
What may also help is that that in my program.cs
I do map controllers (I have two), this I wonder if this line increases startup time app.MapControllers();
? I do not need to do it this way, this was just an easy thing to do
Except those ideas for AOT and authorization outside of function, any way to speed up startup time for my minimal api?